導讀:2020年,Sentinel 推出 Go 原生版本Sentinel-Golang,在雲原生領域繼續突破。本文將從實際出發 結合案例說明 在Sentinel-Golang中如何集成Nacos,使其做為外部動態數據源,將流控規則存儲在nacos中,並且實現動態實時更新規則。
本文主要分為兩個部分:
- 將sentinel流控規則定義在代碼內部 實現限流效果。
- 將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 主要分為以下幾步:
- 對 Sentinel 進行相關配置並進行初始化
- 埋點(定義資源)
- 配置規則
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()
}
}
}()
}
<-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環境,請確保是在以下版本環境中安裝使用:
- 64 bit OS,支持 Linux/Unix/Mac/Windows,推薦選用 Linux/Unix/Mac。
- 64 bit JDK 1.8+;下載 & 配置。
- 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.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
- 登錄到nacos web
- 在配置管理中,新建配置
- 輸入dataId,group(dataId,group 創建時可以自定義,本文創建的dataId=flow,group=sentinel-go)
- 將數據源樣例粘貼到配置內容中。
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 創建項目
- 版本
- sentinel-golang 版本使用0.6.0,nacos-sdk-go 使用1.0.0
- go.mod
module sentinel-go-nacos-example
go 1.13
require (
github.com/alibaba/sentinel-golang v0.6.0
github.com/nacos-group/nacos-sdk-go v1.0.0
)
- main.go
package main
import (
"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 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 - 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 等技術領域、聚焦雲原生流行技術趨勢、雲原生大規模的落地實踐,做最懂雲原生開發者的公眾號。”