导读: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,是它们的开源实现。经验过双十一流量峰值和阿里巴巴经济体超大规模容量的考验,积淀了阿里巴巴软负载团队在这个畛域十年的教训,在稳定性和功能性上都有很好的保障。


(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 mainimport (    "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()                }            }        }()    }    <-ch}

官网expmale:https://github.com/alibaba/sentinel-golang/tree/master/example

4. Sentinel-Go 集成Nacos

Sentinel-Go集成Nacos实现内部动静数据源性能.

4.1 部署Nacos

4.1.1 版本抉择

您能够在Nacos的release notes及博客中找到每个版本反对的性能的介绍,以后举荐的稳固版本为1.3.1。

4.1.2 准备环境筹备

Nacos 依赖 Java 环境来运行。如果您是从代码开始构建并运行Nacos,还须要为此配置 Maven环境,请确保是在以下版本环境中装置应用:

  1. 64 bit OS,反对 Linux/Unix/Mac/Windows,举荐选用 Linux/Unix/Mac。
  2. 64 bit JDK 1.8+;下载 & 配置。
  3. Maven 3.2.x+;下载 & 配置。

4.1.3 下载源码或者安装包

你能够通过源码和发行包两种形式来获取 Nacos。

从 Github 上下载源码形式

git clone https://github.com/alibaba/nacos.gitcd nacos/mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U  ls -al distribution/target/// change the $version to your actual pathcd distribution/target/nacos-server-$version/nacos/bin

下载编译后压缩包形式

您能够从 最新稳固版本 下载 nacos-server-$version.zip 包。

unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gz  cd nacos/bin

4.1.4 启动服务器

Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
如果您应用的是ubuntu零碎,或者运行脚本报错提醒[[符号找不到,可尝试如下运行:
bash startup.sh -m standalone

Windows
启动命令:
cmd startup.cmd
或者双击startup.cmd运行文件。

部署胜利拜访 http://127.0.0.1:8848/nacos  
用户名/明码:nacos/nacos

4.2 Sentinel限流配置到Nacos

  1. 登录到nacos web
  2. 在配置管理中,新建配置
  3. 输出dataId,group(dataId,group 创立时能够自定义,本文创立的dataId=flow,group=sentinel-go)
  4. 将数据源样例粘贴到配置内容中。

4.2.1 Nacos 内部数据源样例

此样例是流量管制的Demo配置。当流量并发数大于100间接回绝。

配置内容阐明可参考https://github.com/alibaba/sentinel-golang/wiki/流量管制

[    {        "resource": "some-test",        "metricType": 1,        "count": 100.0,        "controlBehavior":0    }]

创立实现后,在nacos配置列表中能够看到对应的限流配置。

4.3 Nacos数据源集成

4.3.1 创立我的项目

  1. 版本

    1. sentinel-golang 版本应用0.6.0,nacos-sdk-go 应用1.0.0
  2. go.mod
module sentinel-go-nacos-examplego 1.13require (    github.com/alibaba/sentinel-golang v0.6.0    github.com/nacos-group/nacos-sdk-go v1.0.0)
  1. main.go
package mainimport (    "fmt"    "math/rand"    "sync/atomic"    "time"    sentinel "github.com/alibaba/sentinel-golang/api"    "github.com/alibaba/sentinel-golang/core/base"    "github.com/alibaba/sentinel-golang/ext/datasource/nacos"    "github.com/alibaba/sentinel-golang/util"    "github.com/nacos-group/nacos-sdk-go/clients"    "github.com/alibaba/sentinel-golang/ext/datasource"    "github.com/nacos-group/nacos-sdk-go/common/constant")type Counter struct {    pass  *int64    block *int64    total *int64}func main() {    //流量计数器,为了流控打印日志更直观,和集成nacos数据源无关。    counter := Counter{pass: new(int64), block: new(int64), total: new(int64)}    //nacos server地址    sc := []constant.ServerConfig{        {            ContextPath: "/nacos",            Port:        8848,            IpAddr:      "127.0.0.1",        },    }    //nacos client 相干参数配置,具体配置可参考https://github.com/nacos-group/nacos-sdk-go    cc := constant.ClientConfig{        TimeoutMs: 5000,    }    //生成nacos config client(配置核心客户端)    client, err := clients.CreateConfigClient(map[string]interface{}{        "serverConfigs": sc,        "clientConfig":  cc,    })    if err != nil {        fmt.Printf("Fail to create client, err: %+v", err)        return    }    //注册流控规定Handler    h := datasource.NewFlowRulesHandler(datasource.FlowRuleJsonArrayParser)    //创立NacosDataSource数据源    //sentinel-go 对应在nacos中创立配置文件的group    //flow 对应在nacos中创立配置文件的dataId    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    }    //nacos数据源初始化    err = nds.Initialize()    if err != nil {        fmt.Printf("Fail to initialize nacos data source client, err: %+v", err)        return    }    //启动统计    go timerTask(&counter)    //模仿流量    ch := make(chan struct{})    for i := 0; i < 10; i++ {        go func() {            for {                atomic.AddInt64(counter.total, 1)                //some-test 对应在nacos 流控配置文件中的resource                e, b := sentinel.Entry("some-test", sentinel.WithTrafficType(base.Inbound))                if b != nil {                    atomic.AddInt64(counter.block, 1)                    // Blocked. We could get the block reason from the BlockError.                    time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)                } else {                    atomic.AddInt64(counter.pass, 1)                    time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)                    // Be sure the entry is exited finally.                    e.Exit()                }            }        }()    }    <-ch}//statistic printfunc timerTask(counter *Counter) {    fmt.Println("begin to statistic!!!")    var (        oldTotal, oldPass, oldBlock int64    )    for {        time.Sleep(1 * time.Second)        globalTotal := atomic.LoadInt64(counter.total)        oneSecondTotal := globalTotal - oldTotal        oldTotal = globalTotal        globalPass := atomic.LoadInt64(counter.pass)        oneSecondPass := globalPass - oldPass        oldPass = globalPass        globalBlock := atomic.LoadInt64(counter.block)        oneSecondBlock := globalBlock - oldBlock        oldBlock = globalBlock        fmt.Println(util.CurrentTimeMillis()/1000, "total:", oneSecondTotal, " pass:", oneSecondPass, " block:", oneSecondBlock)    }}

4.3.2 运行后果

4.3.3 动静更新限流配置

在我的项目启动过程中,在nacos中批改流控配置参数。将count 从100->400

能够看到打印了从新loadRule的日志,流量管制动静的由100->400

总结

在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 微服务生态中的推广集成工作。

“阿里巴巴云原生关注微服务、Serverless、容器、Service Mesh 等技术畛域、聚焦云原生风行技术趋势、云原生大规模的落地实际,做最懂云原生开发者的公众号。”