本文共 5959 字,大约阅读时间需要 19 分钟。
创建cloudalibaba-sentinel-service8401模块:
依赖:
cloud2020 pers.zhang.springcloud 1.0-SNAPSHOT 4.0.0 cloudalibaba-sentinel-service8401 pers.zhang.springcloud cloud-api-commons ${project.version} com.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
配置:application.yml
server: port: 8401spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: server-addr: localhost:8848 sentinel: transport: dashboard: localhost:8080 #控制台 port: 8719 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口management: endpoints: web: exposure: include: '*'
Controller:
@RestControllerpublic class RateLimitController { @GetMapping("/byResource") @SentinelResource(value = "byResource", blockHandler = "handlerException") public CommonResult byResource() { return new CommonResult(200, "按资源名称限流测试OK", new Payment(2020L, "serial001")); } public CommonResult handlerException(BlockException exception) { return new CommonResult(444, exception.getClass().getCanonicalName() + "\t服务不可用"); }}
启动类:
@SpringBootApplication@EnableDiscoveryClientpublic class MainApp8401 { public static void main(String[] args) { SpringApplication.run(MainApp8401.class, args); }}
localhost:8401/byResource
快速连续访问:触发限流,执行blockHandler
此时关闭8401微服务,再次查看Sentinel控制台,流控规则消失了...
结论:该流控规则是临时的,非持久。
通过访问的url来限流,会返回sentinel自带默认的限流处理信息
@GetMapping("/rateLimit/byUrl") @SentinelResource(value = "byUrl") public CommonResult byUrl() { return new CommonResult(200, "按url限流测试OK", new Payment(2020L, "serial002")); }
localhost:8401/rateLimit/byRul
,正常返回 创建CustomerBlockHandler类用于自定义限流处理逻辑:
public class CustomerBlockHandler { public static CommonResult handlerException(BlockException exception) { return new CommonResult(4444, "按客户自定义,global handlerException -----1"); } public static CommonResult handlerException2(BlockException exception) { return new CommonResult(4444, "按客户自定义,global handlerException -----2"); }}
Controller
@GetMapping("/rateLimit/customerBlockHandler") @SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class, //指定处理的类 blockHandler = "handlerException2") //指定方法 public CommonResult customerBlockHandler() { return new CommonResult(200, "按客户自定义", new Payment(2020L, "serial003")); }
快速访问:localhost:8401/rateLimit/customerBlockHandler
注意:注解方式埋点不支持 private 方法。
@SentinelResource 注解包含以下属性:
注:1.6.0 之前的版本 fallback 函数只针对降级异常(DegradeException)进行处理,不能针对业务异常进行处理。
特别地,若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。若未配置 blockHandler、fallback 和 defaultFallback,则被限流降级时会将 BlockException 直接抛出(若方法本身未定义 throws BlockException 则会被 JVM 包装一层 UndeclaredThrowableException)。
转载地址:http://hppqb.baihongyu.com/