@SentinelResource 用於定義資源,並提供可選的異常處理和 fallback 配置項。 @SentinelResource 註解包含如下屬性:前端
注:1.6.0 以前的版本 fallback 函數只針對降級異常(DegradeException)進行處理,不能針對業務異常進行處理。spring
特別地,若 blockHandler 和 fallback 都進行了配置,則被限流降級而拋出 BlockException 時只會進入 blockHandler 處理邏輯。若未配置 blockHandler、fallback 和 defaultFallback,則被限流降級時會將 BlockException 直接拋出。bootstrap
@SentinelResource 註解不僅僅用於controller的接口流控。同時也能夠用於方法上面。若是看過實現方式代碼。能夠知道他底層是基於cglib動態代理實現的。進行切面處理。注意點:api
blockHandler 和 blockHandlerClass 的使用app
blockHandler 是可選的。若是使用blockHandlerClass,必須搭配blockHandler使用, blockHandler指定blockHandlerClass類中對應的方法名稱。方法名稱、參數、返回值、static 必須按照上述文檔描述同樣。官方文檔沒有強調要必需要搭配使用。ide
同理 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。
如何喜歡能夠關注分享本公衆號。