模拟限流降级
下载控制台地址https://github.com/alibaba/Sentinel/releases
下载的1.8.3版本
启动
java -jar .sentinel-dashboard-1.8.3.jar
访问
http://localhost:8080/#/login
账号密码均为sentinel
搭建项目sentinel-8401
pomapplication.ymlcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.csp sentinel-datasource-nacos com.alibaba.cloud spring-cloud-starter-alibaba-sentinel org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true cn.hutool hutool-all 4.6.3 org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
server:
port: 8401
spring:
application:
name: sentinel-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: '*'
启动类
package com.xiong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class Sentinel8401 {
public static void main(String[] args) {
SpringApplication.run(Sentinel8401.class,args);
}
}
业务类
package com.xiong.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
// 测试自定义异常
@GetMapping("/testA")
//testAName 断流配置用的名称、handleException返回的自定义异常
@SentinelResource(value = "testAName",blockHandler = "handleException")
public String testA()
{
return "------testA";
}
// 自定义异常
public String handleException(BlockException exception)
{
return "自定义异常!!!!";
}
// 测试全局自定义异常
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handleException2")
public String customerBlockHandler()
{
return "正常访问~~";
}
}
全局异常管理
package com.xiong.controller;
import com.alibaba.csp.sentinel.slots.block.BlockException;
public class CustomerBlockHandler
{
public static String handleException2(BlockException exception){
return "自定义全局异常";
}
}
启动及测试
启动
启动 nacos
启动 sentinel
启动 8401
测试先访问
http://localhost:8401/rateLimit/customerBlockHandler
访问成功之后,sentinel中出现此资源的列表
点击其中的按钮可以进行配置
配置完成后再次点击进行测试



