Sentinel(一)--限流基本应用

Sentinel

Sentinel控制台安装

  1. 下载sentinel-dashboard-1.8.0.jar

  2. 在cmd中运行下面的命令

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.0.jar
  • -Dserver.port=8080 用于指定 Sentinel 控制台端口为 8080

  • csp.sentinel.dashboard.server,控制台的地址,指定控制台后客户端会自动向该地址发送心跳包

  • project.name,指定应用名称

更多参数,详见Sentinel启动参数配置表

  1. 访问http://localhost:8080,输入sentinel/sentinel帐号密码即可访问

Sentinel 集成到Spring Cloud

Sentinel集成到Spring Cloud中,其实本质上是一样的,都是基于Sentinel-Core来完成流量控制等功能。

不管是集成到Spring Cloud Netflix、还是Dubbo、亦或者像Spring Cloud Gateway、gRpC等框架中,其原理都是一样。

这里使用版本:

Spring Boot版本为2.3.13.RELEASE, spring Cloud Alibaba版本为:2.2.6.RELEASE,Spring Cloud版本为: Hoxton.SR12。

具体实现代码

  1. 添加jar包依赖
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
  1. TestService
@Service
public class TestService {
    
    @SentinelResource(value = "doTest",blockHandler = "handerException")
    public String doTest(){
    	return "Hello,"+new Date();
    }
    
    public String handerException(BlockException e){
    	return "被限流了";
    }
}
  1. SentinelController
@RestController
public class SentinelController {
    
    @Autowired
    TestService testService;
    
    @GetMapping("/hi")
    public String doTest(){
   		return testService.doTest();
    }
}
  1. FlowRuleInitFunc
public class FlowRuleInitFunc implements InitFunc {
    
    @Override
    public void init() throws Exception {
        List<FlowRule> rules=new ArrayList<>();
        FlowRule rule=new FlowRule();
        rule.setResource("doTest");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(2);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}

在META-INF/services/com.alibaba.csp.sentinel.init.InitFunc文件中,添加自定义扩展点的全路径

限流规则添加到sentinel中:

按照上述步骤配置,即可完成Spring Cloud的整合。

在Sentinel官方文档有详细说明,文档地址如下:

主流框架适配文档:https://github.com/alibaba/Sentinel/wiki/主流框架的适配#feign

Dubbo集成Sentinel实现限流

Sentinel 提供 Dubbo 的相关适配 Sentinel Dubbo Adapter,主要包括针对 Service Provider 和 Service Consumer 实现的 Filter。相关模块:

  • sentinel-apache-dubbo-adapter(兼容 Apache Dubbo 2.7.x 及以上版本,自Sentinel 1.5.1 开始支持)

  • sentinel-dubbo-adapter(兼容 Dubbo 2.6.x 版本)

对于 Apache Dubbo 2.7.x 及以上版本,使用时需引入以下模块(以 Maven 为例):

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-apache-dubbo-adapter</artifactId>
    <version>x.y.z</version>
</dependency>

对于 Dubbo 2.6.x 及以下版本,使用时需引入以下模块(以 Maven 为例):

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-dubbo-adapter</artifactId>
    <version>x.y.z</version>
</dependency>

引入此依赖后,Dubbo 的服务接口和方法(包括调用端和服务端)就会成为 Sentinel 中的资源,在配置了规则后就可以自动享受到 Sentinel 的防护能力。

  1. 添加jar包依赖
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-apache-dubbo-adapter</artifactId>
    <version>1.8.1</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>1.8.1</version>
</dependency>
  1. 设置资源访问规则:FlowRuleInitFunc
public class FlowRuleInitFunc implements InitFunc {
	@Override
	public void init() throws Exception {
        List<FlowRule> rules=new ArrayList<>();
        FlowRule rule=new FlowRule();
        rule.setResource("com.example.userserviceapi.IHelloService");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(2);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}
  1. 在META-INF/services/com.alibaba.csp.sentinel.init.InitFunc文件中,添加自定义扩展点的全路径

  2. 修改application.properties文件,增加监控上报

spring.cloud.sentinel.transport.dashboard=localhost:8080

限流粒度可以是服务接口和服务方法两种粒度:

  • 服务接口:resourceName 为 接口全限定名,如com.alibaba.csp.sentinel.demo.dubbo.FooService

  • 服务方法:resourceName 为 接口全限定名:方法签名,如com.alibaba.csp.sentinel.demo.dubbo.FooService:sayHello(java.lang.String)

Dubbo限流异常处理

Dubbo目标服务被限流后,Dubbo会把异常信息抛到调用段,控制台中看到如下异常。

java.lang.RuntimeException: SentinelBlockException: FlowException
	at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.1.jar:1.8.1]

服务调用端可以捕获该异常,提供限流后的处理方法!

posted @ 2022-10-19 21:49  snail灬  阅读(237)  评论(0编辑  收藏  举报