导包
<!--springcloud alibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--springcloud alibaba sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
写yml
sentinel:
transport:
#配置sentinel dashboard地址
dashboard: 192.168.209.52:8858
#默认8719端口,假如被占用自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
写降级类
public class AccountBlockHandler {
public static CommonResult handleIncomeException(BlockException e){
return new CommonResult(1010,"当前访问用户较多,请稍后再试!");
}
}
在控制层方法上写注解
@SentinelResource(value = "income", blockHandlerClass = AccountBlockHandler.class,blockHandler = "handleIncomeException")
特别注意
:降级方法必须和被降级方法的返回值类型、形参一样,并且多一个BlockException
类型的形参,不然无法识别。