**导读:**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.git
cd nacos/
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
ls -al distribution/target/
// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin

下载编译后压缩包方式

您可以从 最新稳定版本 下载 nacos-server-$version.zip 包。

unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gzcd 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.0github.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  *int64block *int64total *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-gocc := 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}//注册流控规则Handlerh := datasource.NewFlowRulesHandler(datasource.FlowRuleJsonArrayParser)//创建NacosDataSource数据源//sentinel-go 对应在nacos中创建配置文件的group//flow 对应在nacos中创建配置文件的dataIdnds, 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 流控配置文件中的resourcee, 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 print
func 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 - oldTotaloldTotal = globalTotalglobalPass := atomic.LoadInt64(counter.pass)oneSecondPass := globalPass - oldPassoldPass = globalPassglobalBlock := atomic.LoadInt64(counter.block)oneSecondBlock := globalBlock - oldBlockoldBlock = globalBlockfmt.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 等技术领域、聚焦云原生流行技术趋势、云原生大规模的落地实践,做最懂云原生开发者的公众号。”

Sentinel-Go 集成 Nacos 实现外部动态数据源相关推荐

  1. nacos windows部署_Sentinel-Go 集成 Nacos 实现外部动态数据源

    导读:2020年,Sentinel 推出 Go 原生版本Sentinel-Golang,在云原生领域继续突破.本文将从实际出发 结合案例说明 在Sentinel-Golang中如何集成Nacos,使其 ...

  2. SpringCloudGateway 集成 nacos 整合实现动态路由_04

    接上一篇:SpringCloud Gateway 集成 oauth2 实现统一认证授权 文章目录 一.目前存在的问题 1. 问题简述 2. 集成nacos前配置 3. 前言简述 二.网关模块改造集成n ...

  3. angularjs 让当前路由重新加载_Spring Cloud Gateway的动态路由怎样做?集成Nacos实现很简单...

    一.说明 网关的核心概念就是路由配置和路由规则,而作为所有请求流量的入口,在实际生产环境中为了保证高可靠和高可用,是尽量要避免重启的,所以实现动态路由是非常有必要的:本文主要介绍 Spring Clo ...

  4. nacos动态配置数据源_Jasper 怎么配置动态数据源

    Jasper 本身是不支持动态数据源的,能用的解决方式是通过 api 自定义数据源,实际操作就是根据条件判断后动态设定 jdbc 的 url.用户名及密码等连接属性.比如: String userNa ...

  5. SentinelDashboard-Nacos动态数据源-SpringCloudGateway

    友情提示:要下载源码进行改造,本文基于Sentinel-1.8.2. 以下内容摘自个人的技术文档,相关资料主要来自SpringCloudAlibaba和Github-Sentinel.正文是部分节选内 ...

  6. Spring Boot 动态数据源(多数据源自己主动切换)

    本文实现案例场景: 某系统除了须要从自己的主要数据库上读取和管理数据外.另一部分业务涉及到其它多个数据库,要求能够在不论什么方法上能够灵活指定详细要操作的数据库. 为了在开发中以最简单的方法使用,本文 ...

  7. 微服务框架 Go-Micro 集成 Nacos 实战之服务注册与发现

    作者 | 张斌斌 导读:本文主要介绍如何使用 Golang 生态中的微服务框架 Go-Micro(v2) 集成 Nacos 进行服务注册与发现.(Go-Micro 目前已经是 v3 版本,但由于某些原 ...

  8. synamic-datasource-spring-boot-starter实现动态数据源Mysql和Sqlserver

    场景 若依前后端分离版本地搭建开发环境并运行项目的教程: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662 若依微 ...

  9. 微服务框架Go-Micro集成Nacos实战之服务注册与发现

    简介:本文主要介绍如何使用 Golang 生态中的微服务框架 Go-Micro(v2) 集成 Nacos 进行服务注册与发现.(Go-Micro 目前已经是 v3 版本,但由于某些原因项目已经更名为 ...

最新文章

  1. mysql数据库查询全部信息_mysql 查询怎么获取数据库下所有表的信息?
  2. RxJava 教程第一部分:入门之 生命周期管理
  3. 计算机设备安全检查表,信息安全检查表
  4. Dreamweaver使用详解
  5. SAP ECC EHP7 RFC 发布成WebService
  6. 【转】30种MySQL索引优化的方法
  7. 聊一聊开发常用小工具
  8. 注意力公式步骤每一步的含义,总共三步
  9. Socket编程:之TCP案例
  10. 系统无声音,播放器有声音
  11. MVC5 Entity Framework学习参加排序、筛选和排序功能
  12. 【服务器环境搭建-Centos】tmpfs,【转载】
  13. 删除链表倒数第N个节点
  14. 海康sdk java示例_调用海康SDK
  15. CVPR2021 | 视频超分辨率中时空蒸馏方案
  16. 全球最快下载工具 XDM
  17. MySQL参数 之 innodb_buffer_pool_size
  18. 丁香园php岗_你为什么从丁香园离职?
  19. BMS(电池管理系统)第四课 ——核心!!!SOC算法开发
  20. linux下dbus的理解学习

热门文章

  1. VMP分析之VM解码循环与基本架构(一)
  2. android 数据库sqlite的使用
  3. 3、创建用户(3种方式)
  4. 爬取京东淘宝商品销量并可视化处理数据
  5. Codeforces Beta Round #16 (Div. 2 Only)【未完结】
  6. 三人三鬼过河(野人与传教士过河)问题c/c++代码
  7. MySQL中的分页查询
  8. Redis根据是否存在设置值
  9. innerhtml js执行_JS 中 DOM 操作
  10. RocketMQ入门到入土(一)新手也能看懂的原理和实战!