參考官方文檔:https://github.com/alibaba/Sentinel/wiki/%E9%9B%86%E7%BE%A4%E6%B5%81%E6%8E%A7#%E7%A4%BA%E4%BE%8B
參考官方項(xiàng)目:https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-cluster
ncaos 參考文檔:https://www.bookstack.cn/read/sentinel-v1.7/39e61ead78232553.md
Sentinel 集群限流服務(wù)端有兩種啟動(dòng)方式:
1,獨(dú)立模式(Alone),即作為獨(dú)立的 token server 進(jìn)程啟動(dòng),獨(dú)立部署,隔離性好,但是需要額外的部署操作。獨(dú)立模式適合作為 Global Rate Limiter 給集群提供流控服務(wù)。
2,嵌入模式(Embedded),即作為內(nèi)置的 token server 與服務(wù)在同一進(jìn)程中啟動(dòng)。在此模式下,集群中各個(gè)實(shí)例都是對(duì)等的,token server 和 client 可以隨時(shí)進(jìn)行轉(zhuǎn)變,因此無需單獨(dú)部署,靈活性比較好。但是隔離性不佳,需要限制 token server 的總 QPS,防止影響應(yīng)用本身。嵌入模式適合某個(gè)應(yīng)用集群內(nèi)部的流控。
使用 第三章 nacosa項(xiàng)目,實(shí)現(xiàn)嵌入模式
本地啟動(dòng)nacos服務(wù)
本地啟動(dòng)sentinel控制臺(tái)服務(wù)
(1)pom

(2)將官方項(xiàng)目sentinel-demo-cluster有用的類拿過來,其中 com.alibaba.csp.sentinel.init.InitFunc 的內(nèi)容是? DemoClusterInitFunc 類所在的全路徑

(3)簡單修改DemoClusterInitFunc類,改為Properties 加載nacos里的 配置信息,且信息實(shí)時(shí)更新

(4)DemoClusterInitFunc類中四個(gè)常量
//限流規(guī)則
private final StringflowDataId =APP_NAME + DemoConstants.FLOW_POSTFIX;
//限流規(guī)則
private final StringparamDataId =APP_NAME + DemoConstants.PARAM_FLOW_POSTFIX;
//client配置
private final StringconfigDataId =APP_NAME +"-cluster-client-config";
//設(shè)置token server,client
private final StringclusterMapDataId =APP_NAME + DemoConstants.CLUSTER_MAP_POSTFIX;
(5)在nacos中配置

上面截圖,不要使用localhost,使用ip


(6)啟動(dòng)實(shí)例,啟動(dòng)配置項(xiàng)參考 https://www.bookstack.cn/read/sentinel-v1.7/27476bd330878602.md
-Dserver.port=9100
-Dproject.name=nacosa
-Dcsp.sentinel.log.use.pid=true
-Dcsp.sentinel.dashboard.server=localhost:8999
-Dcsp.sentinel.api.port=9101
-Dserver.port=9200
-Dproject.name=nacosa
-Dcsp.sentinel.log.use.pid=true
-Dcsp.sentinel.dashboard.server=localhost:8999
-Dcsp.sentinel.api.port=9201
(7)查看sentinel控制臺(tái)


(8)訪問 http://localhost:9000/nacosa/helloWorld3,返回?Blocked by Sentinel (flow limiting),說明已經(jīng)生效
(9)本地寫一個(gè)測試并發(fā)類,每秒200個(gè)請(qǐng)求,這個(gè)時(shí)候在查看 sentinel控制臺(tái)


本地啟用了2個(gè)nacosa 實(shí)例,對(duì)source資源?helloWorld3的qps配置是為1,所以通過的最多也就是2。
gateway服務(wù),發(fā)現(xiàn)并無拒絕,是因?yàn)樵趃ateway服務(wù)中,對(duì)rousce 資源nacosa 的qps配置比較大
項(xiàng)目地址:https://github.com/renzheyizhe/demo/tree/master/nacos-a