您当前的位置: 首页 >  阿里云云栖号

Sentinel-Go 集成 Nacos 实现外部动态数据源

阿里云云栖号 发布时间:2020-10-10 10:54:47 ,浏览量:1

简介: 2020年,Sentinel 推出 Go 原生版本Sentinel-Golang,在云原生领域继续突破。本文将从实际出发 结合案例说明 在Sentinel-Golang中如何集成Nacos,使其做为外部动态数据源,将流控规则存储在nacos中,并且实现动态实时更新规则。

9.28头图.png

导读:2020年,Sentinel 推出 Go 原生版本Sentinel-Golang,在云原生领域继续突破。本文将从实际出发 结合案例说明 在Sentinel-Golang中如何集成Nacos,使其做为外部动态数据源,将流控规则存储在nacos中,并且实现动态实时更新规则。

本文主要分为两个部分:

  1. 将sentinel流控规则定义在代码内部 实现限流效果。
  2. 将sentinel流控规则定义在nacos配置中心,实现限流效果以及在nacos中动态更新规则,实现动态流控。

下面将详细介绍一下相关的背景知识。

1. Sentinel

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

Sentinel 具有以下特征:

  • 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
  • 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
  • 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
  • 完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
1.1 Sentinel 的历史
  • 2012年,Sentinel 诞生,主要功能为入口流量控制。
  • 2013-2017年,Sentinel 在阿里巴巴集团内部迅速发展,成为基础技术模块,覆盖了所有的核心场景。Sentinel 也因此积累了大量的流量归整场景以及生产实践。
  • 2018年,Sentinel 开源,并持续演进。
  • 2019年,Sentinel 在多语言扩展的方向上逐步探索,陆续推出 C++ 原生版本、Envoy 集群流量控制支持。
  • 2020年,Sentinel 推出 Go 原生版本,期待在云原生领域继续突破。https://github.com/alibaba/sentinel-golang
2. Nacos

Nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理的平台,Nacos脱胎于阿里巴巴内部的ConfigServer和Diamond,是它们的开源实现。经历过双十一流量峰值和阿里巴巴经济体超大规模容量的考验,沉淀了阿里巴巴软负载团队在这个领域十年的经验,在稳定性和功能性上都有很好的保障。

1.png (Sentinel-Go集成Nacos动态数据源架构)

目前 Sentinel 内部的限流、熔断等策略都是基于规则来实现的,提供动态数据源扩展的目的,就是希望将规则数据的加载以及更新操作通过一些配置中心中间件(比如 nacos,etcd,conful,等等)来实现动态更新。

3. Sentinel-Go 限流 Demo

未集成nacos时 规则定义在代码内部,没有使用外部数据源。

3.1 安装

go get github.com/alibaba/sentinel-golang

3.2 Demo样例

使用 Sentinel 主要分为以下几步:

  1. 对 Sentinel 进行相关配置并进行初始化
  2. 埋点(定义资源)
  3. 配置规则
package main

import (
    "fmt"
    "log"
    "math/rand"
    "time"

    sentinel "github.com/alibaba/sentinel-golang/api"
    "github.com/alibaba/sentinel-golang/core/base"
    "github.com/alibaba/sentinel-golang/core/flow"
    "github.com/alibaba/sentinel-golang/util"
)

func main() {
    // We should initialize Sentinel first.
    err := sentinel.InitDefault()
    if err != nil {
        log.Fatalf("Unexpected error: %+v", err)
    }

    _, err = flow.LoadRules([]*flow.FlowRule{
        {
            Resource:        "some-test",
            MetricType:      flow.QPS,
            Count:           10,
            ControlBehavior: flow.Reject,
        },
    })
    if err != nil {
        log.Fatalf("Unexpected error: %+v", err)
        return
    }

    ch := make(chan struct{})

    for i := 0; i < 10; i++ {
        go func() {
            for {
                e, b := sentinel.Entry("some-test", sentinel.WithTrafficType(base.Inbound))
                if b != nil {
                    // Blocked. We could get the block reason from the BlockError.
                    time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
                } else {
                    // Passed, wrap the logic here.
                    fmt.Println(util.CurrentTimeMillis(), "passed")
                    time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)

                    // Be sure the entry is exited finally.
                    e.Exit()
                }

            }
        }()
    }
    400

5.png

总结

在sentinel-go中使用nacos作为外部动态数据源,只需要将原来声明Rule以及加载Rule的部分 变成从nacos数据源读取。

在本文中只介绍了流量控制的集成,熔断,warmup,热点参数的集成也是相同的,只要按需修改配置的内容即可

配置内容参考地址:https://github.com/alibaba/sentinel-golang/wiki

关键代码:

    h := datasource.NewFlowRulesHandler(datasource.FlowRulesJsonConverter)
    nds, err := nacos.NewNacosDataSource(client, "sentinel-go", "flow", h)
    if err != nil {
        fmt.Printf("Fail to create nacos data source client, err: %+v", err)
        return
    }
    err = nds.Initialize()
    if err != nil {
        fmt.Printf("Fail to initialize nacos data source client, err: %+v", err)
        return
    }
相关链接
  • Demo地址:https://github.com/alibaba/sentinel-golang/tree/master/example/nacos
  • Sentinel-golang:https://github.com/alibaba/sentinel-golang
  • Sentinel钉钉社区交流群: 30150716,23339422(Sentinel golang生态交流群)
  • Nacos:https://nacos.io/zh-cn/index.html
  • Nacos钉钉社区交流群:30438813, 23191211(Nacos golang生态交流群)
  • Nacos-SDK-Go项目地址:https://github.com/nacos-group/nacos-sdk-go
作者简介

张斌斌 Github 账号:sanxun0325,Nacos Commiter,Sentinel-Golang Contributor,现任职 OpenJaw 微服务团队。目前主要负责 Nacos、Sentinel-Golang 社区相关项目的开发工作,以及 Nacos 在 Golang 微服务生态中的推广集成工作。

 

 

原文链接 本文为阿里云原创内容,未经允许不得转载。

关注
打赏
1688896170
查看更多评论
0.0853s