物联网架构成长之路(53)-Sentinel流量控制中间件入门

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/

 

posted @ 2020-03-07 13:38  无脑仔的小明  阅读(1253)  评论(0编辑  收藏  举报