sentinel dashboard 版本 1.7.1
spring cloud 版本 Hoxton.SR3
spring cloud alibaba 版本2.2.1.RELEASE
Java 代码
@GetMapping("/test-sentinel-api")
public String testSentinelApi(
@RequestParam(required = false) String a
){
String resourceName="test-sentinel-api";
Entry entry=null;
try {
//定义 sentinel 保护的资源,设置资源名
entry= SphU.entry(resourceName);
// 被保护的业务逻辑
if(StringUtils.isBlank(a)){
throw new IllegalArgumentException("a is null");
}
return a;
} catch (BlockException e) {
// 资源访问,被限流或被降级
log.warn("限流,或者降级了",e);
return "限流,或者降级了";
}finally {
// 务必保证 exit,务必保证每个 entry 与 exit 配对
if(entry != null){
entry.exit();
}
}
}
设置了降级规则,频繁刷新 http://localhost:8010/test-sentinel-api,也会被降级,跟老师讲的不一样!!
有时候刷新30几次才出现降级,有时候刷新10来次就出现降级。
面向未来微服务:熟练掌握Spring Cloud Alibaba
了解课程