标签:int ali cti lombok param end 函数 depend pac
@SentinelResource 用于定义资源,并提供可选的异常处理和 fallback 配置项。 @SentinelResource 注解包含以下属性:
注:1.6.0 之前的版本 fallback 函数只针对降级异常(DegradeException)进行处理,不能针对业务异常进行处理。
特别地,若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。若未配置 blockHandler、fallback 和 defaultFallback,则被限流降级时会将 BlockException 直接抛出。
@SentinelResource 注解不单单用于controller的接口流控。同时也可以用于方法上面。如果看过实现方式代码。可以知道他底层是基于cglib动态代理实现的。进行切面处理。注意点:
blockHandler 和 blockHandlerClass 的使用
blockHandler 是可选的。如果使用blockHandlerClass,必须搭配blockHandler使用, blockHandler指定blockHandlerClass类中对应的方法名称。方法名称、参数、返回值、static 必须按照上述文档描述一样。官方文档没有强调要必须要搭配使用。
同理 fallback 和 fallbackClass也是上面讲述的注意点。
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
bootstrap.yml 配置文件
spring:
cloud:
sentinel:
filter:
# sentienl 默认生效,本地调试false
enabled: true
transport:
dashboard: localhost:8890
port: 8719
# 饥饿加载
eager: true
datasource:
# Sentinel基于nacos存储获取配置信息
na:
nacos:
server-addr: 47.99.209.72:8848
groupId: DEFAULT_GROUP
dataId: ${spring.application.name}-${spring.profiles.active}-sentinel
# 类型
# FLOW("flow", FlowRule.class),
# DEGRADE("degrade", DegradeRule.class),
# PARAM_FLOW("param-flow", ParamFlowRule.class),
# SYSTEM("system", SystemRule.class),
# AUTHORITY("authority", AuthorityRule.class),
# GW_FLOW("gw-flow", "com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule"),
# GW_API_GROUP("gw-api-group", "com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition");
rule-type: flow
nacos 创建 cloud-discovery-client-dev-sentinel 配置文件
[
{
"resource": "client:log:save",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
},
{
"resource": "client:fegin:test",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
},
{
"resource": "user:service:saveTx",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
},
{
"resource": "user:service:save:test",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
创建 BackHandlerClass DiscoveryClientControllerBackHandler
package com.xian.cloud.common.handler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xian.cloud.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
/**
* 对应处理 BlockException 的函数名称 服务限流
* @Author: xlr
* @Date: Created in 9:08 PM 2019/11/16
*/
@Slf4j
public class DiscoveryClientControllerBackHandler {
public static String defaultMessage(BlockException e){
log.warn( "DiscoveryClientControllerBackHandler defaultMessage BlockException : {}",e );
return "defaultMessage 服务限流,请稍后尝试";
}
public static String saveTx(UserEntity entity,BlockException e) {
log.warn( "DiscoveryClientControllerBackHandler saveTx BlockException : {}",e );
return "saveTx 服务限流,请稍后尝试";
}
}
创建 FallBackHandlerClass
package com.xian.cloud.common.handler;
import com.xian.cloud.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
/**
* 仅针对降级功能生效(DegradeException)
* @Author: xlr
* @Date: Created in 9:13 PM 2019/11/16
*/
@Slf4j
public class DiscoveryClientControllerFallBackHandler {
public static String defaultMessage(Throwable t){
log.warn( "DiscoveryClientControllerFallBackHandler defaultMessage Throwable : {}",t );
return "defaultMessage 服务降级,请稍后尝试";
}
public static String saveTx(UserEntity entity,Throwable t) {
log.warn( "DiscoveryClientControllerFallBackHandler saveTx Throwable : {}",t );
return "saveTx 服务降级,请稍后尝试";
}
}
对外接口DiscoveryClientController 添加接口
@SentinelResource(
value = "client:fegin:test",
blockHandler = "defaultMessage",
fallback = "defaultMessage",
blockHandlerClass = DiscoveryClientControllerBackHandler.class,
fallbackClass = DiscoveryClientControllerFallBackHandler.class
)
@RequestMapping(value = "fegin/test",method = RequestMethod.GET)
public String feginTest() {
String result = serverService.hello( "fegin" );
return " 返回 : " + result;
}
@GetMapping("/log/save")
@SentinelResource(
value = "client/log/save",
blockHandler = "defaultMessage",
fallback = "defaultMessage",
blockHandlerClass = DiscoveryClientControllerBackHandler.class,
fallbackClass = DiscoveryClientControllerFallBackHandler.class
)
public String save(){
UserEntity entity = new UserEntity();
entity.setUsername("tom");
entity.setPassWord("1232131");
entity.setEmail("222@qq.com");
userService.saveTx(entity);
return "success";
}
@GetMapping("user/service/save")
public String userServiceSaveTx(){
UserEntity entity = new UserEntity();
String result = userService.saveTx( entity );
return result;
}
UserServiceImpl 方法
@Override
@Transactional
@SentinelResource(
value = "user:service:saveTx",
blockHandler = "saveTx",
fallback = "saveTx",
blockHandlerClass = DiscoveryClientControllerBackHandler.class,
fallbackClass = DiscoveryClientControllerFallBackHandler.class
)
public String saveTx(UserEntity entity) {
return "success";
}
以上就配置完毕。然后进行测试在页面疯狂刷新
http://localhost:9011/client/user/service/save
http://localhost:9011/client/fegin/test
停止 server服务 再次调用 fegin、test
服务降级和服务限流来回切换提示在前端页面。blockHandlerClass、fallbackClass。
如何喜欢可以关注分享本公众号。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。转载请附带公众号二维码
Spring Cloud gateway 七 Sentinel 注解方式使用
标签:int ali cti lombok param end 函数 depend pac
原文地址:https://www.cnblogs.com/cloudxlr/p/11886343.html