0. 前言
1. 運行啟動sentinel-dashboard
到這里 https://github.com/alibaba/Sentinel/releases/download/1.7.1/sentinel-dashboard-1.7.1.jar 下載sentinel-dashboard
運行 java -jar sentinel-dashboard-1.7.1.jar --server.port=8858
提供Docker方式部署
➜ sentinel cat docker-compose.yml
1 version: '3.7' 2 services: 3 sentinel: 4 image: bladex/sentinel-dashboard:latest 5 ports: 6 - "8858:8858" 7 environment: 8 - auth.username=sentinel 9 - auth.password=123456
2. Sentinel 客戶端編碼
pom.xml 增加
1 <dependency> 2 <groupId>com.alibaba.cloud</groupId> 3 <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> 4 </dependency>
application.properties
1 spring.application.name=demo 2 spring.cloud.sentinel.transport.dashboard=127.0.0.1:8858
Java代碼提供了代碼方式和注解方式集成
HelloProviderService.java
1 package com.wunaozai.demo; 2 3 import org.springframework.stereotype.Service; 4 5 import com.alibaba.csp.sentinel.annotation.SentinelResource; 6 7 @Service 8 public class HelloProviderService { 9 10 @SentinelResource(value="say") 11 public String sayHello(String msg) { 12 return "Hello " + msg; 13 } 14 }
HelloConsumerController.java
1 package com.wunaozai.demo; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import com.alibaba.csp.sentinel.Entry; 9 import com.alibaba.csp.sentinel.EntryType; 10 import com.alibaba.csp.sentinel.SphU; 11 import com.alibaba.csp.sentinel.annotation.SentinelResource; 12 import com.alibaba.csp.sentinel.slots.block.BlockException; 13 14 @RestController 15 @RequestMapping(value="/hello") 16 public class HelloConsumerController { 17 18 @Autowired 19 private HelloProviderService helloproviderService; 20 21 @SentinelResource(value="testResource") 22 @RequestMapping(value="/say") 23 public String sayHello(String msg) { 24 String ret = helloproviderService.sayHello(msg); 25 return ret; 26 } 27 28 @GetMapping(value="/testSentinel") 29 public String testSentinel() { 30 String resName = "testSentinel"; 31 Entry entry = null; 32 String retVal; 33 try { 34 entry = SphU.entry(resName, EntryType.IN); 35 retVal = "passed"; 36 } catch (BlockException e) { 37 retVal = "blocked"; 38 }finally { 39 if(entry != null) { 40 entry.exit(); 41 } 42 } 43 return retVal; 44 } 45 }
3. 運行及配置
由於是簡單的應用,所以,在沒有配置Sentinel的前提下,基本可以訪問
1 http://127.0.0.1:8080/hello/testSentinel 2 http://127.0.0.1:8080/hello/say?msg=yyy
這兩個路徑,沒有發生錯誤。
配置流控規則
配置流控后,再快速訪問上面兩個路徑。
1 http://127.0.0.1:8080/hello/testSentinel 超出QPS=2時,會出現 blocked 2 http://127.0.0.1:8080/hello/say?msg=yyy 超出QPS=5時,會出現程序異常
從下圖可以看到,超出QPS=5時,出現一次異常。
4. 持久化到Nacos
Sentinel Dashboard中添加的規則是存儲在內存中的,只要項目一重啟規則就丟失了
此處將規則持久化到nacos中,在nacos中添加規則,然后同步到dashboard中;
pom.xml 增加
1 <dependency> 2 <groupId>com.alibaba.csp</groupId> 3 <artifactId>sentinel-datasource-nacos</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>com.google.guava</groupId> 7 <artifactId>guava</artifactId> 8 <version>15.0</version> 9 <scope>compile</scope> 10 </dependency>
application.properties 增加
1 spring.cloud.sentinel.datasource.ds.nacos.server-addr=127.0.0.1:8848 2 spring.cloud.sentinel.datasource.ds.nacos.data-id=mz-sentinel 3 spring.cloud.sentinel.datasource.ds.nacos.group-id=DEFAULT_GROUP 4 spring.cloud.sentinel.datasource.ds.nacos.data-type=json 5 spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow
nacos 增加
1 [ 2 { 3 "resource": "/hello", 4 "limitApp": "default", 5 "grade": 1, 6 "count": 5, 7 "strategy": 0, 8 "controlBehavior": 0, 9 "clusterMode": false 10 } 11 ]
啟動后,過一會,就會出現
5. 其他
通過上面,配置基本實現Sentinel規則持久化,每次重啟Sentinel后能恢復。但是這個時候,出現一個問題,就是在Sentinel-dashboard控制台修改時,不會保存到Nacos。但是Nacos在其客戶端總有Listener監聽器實現自動更新。所以在Nacos重新發布配置更新時,對應的Sentinel流控也會更新。基本實現動態化。
Sentinel控制台中修改規則:僅存在於服務的內存中,不會修改Nacos中的配置值,重啟后恢復原來的值。
Nacos控制台中修改規則:服務的內存中規則會更新,Nacos中持久化規則也會更新,重啟后依然保持。
參考資料:
https://www.cnblogs.com/gyli20170901/p/11279576.html
http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-1/
https://github.com/alibaba/Sentinel/blob/master/sentinel-dashboard/src/main/resources/application.properties
https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
本文地址:https://www.cnblogs.com/wunaozai/p/12404712.html
本系列目錄: https://www.cnblogs.com/wunaozai/p/8067577.html
個人主頁:https://www.wunaozai.com/