Github : WIKI java
Sphu (指明要保護的資源名稱)git
Tracer (指明調用來源,異常統計接口)github
ContextUtil(標示進入調用鏈入口)spring
流控規則(針對來源屬性)json
@GetMapping("/test-sentinel-api") public String testSentinelAPI(@RequestParam(required = false) String a) { String resourceName = "test-sentinel-api"; ContextUtil.enter(resourceName, "user-center-service"); // 定義一個sentinel 保護的資源,名稱是test-sentinel-api Entry entry = null; try { entry = SphU.entry(resourceName); // ...被保護的業務邏輯處理 if (StringUtils.isEmpty(a)) { // Sentinel 默認只會統計BlockException & BlockException的子類,若是想統計其餘異常信息,添加Tracer throw new IllegalArgumentException("A is not empty."); } return a; // block Exception: 若是被保護的資源被限流或者降級了,就會拋異常出去 } catch (BlockException e) { log.error("我被限流啦!!{}", e); return "我被限流啦!!"; } catch (IllegalArgumentException argEx) { // 統計當前異常發生次數 / 佔比 Tracer.trace(argEx); return "非法參數信息"; } finally { if (entry != null) { entry.exit(); } ContextUtil.exit(); } }
降級規則api
@GetMapping("/test-sentinel-api") public String testSentinelAPI(@RequestParam(required = false) String a) { // 定義一個sentinel 保護的資源,名稱是test-sentinel-api Entry entry = null; try { entry = SphU.entry("test-sentinel-api"); // ...被保護的業務邏輯處理 if (StringUtils.isEmpty(a)) { // Sentinel 默認只會統計BlockException & BlockException的子類,若是想統計其餘異常信息,添加Tracer throw new IllegalArgumentException("A is not empty."); } return a; // block Exception: 若是被保護的資源被限流或者降級了,就會拋異常出去 } catch (BlockException e) { log.error("我被限流啦!!{}", e); return "我被限流啦!!"; } catch (IllegalArgumentException argEx) { // 統計當前異常發生次數 / 佔比 Tracer.trace(argEx); return "非法參數信息"; } finally { if (entry != null) { entry.exit(); } } }
源碼:com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect
& com.alibaba.csp.sentinel.annotation.aspectj.AbstractSentinelAspectSupport
app
SentinelResource
使用該註解重構上述方法ide
@GetMapping("/test-sentinel-resource") @SentinelResource(value = "test-sentinel-api", blockHandler = "blockException", fallback = "fallback") public String testSentinelResource(@RequestParam(required = false) String a) { // ...被保護的業務邏輯處理 if (StringUtils.isEmpty(a)) { // Sentinel 默認只會統計BlockException & BlockException的子類,若是想統計其餘異常信息,添加Tracer throw new IllegalArgumentException("A is not empty."); } return a; } /** * testSentinelResource BlockException method */ public String blockException(String a, BlockException e) { log.error("限流了,{}", e); return "blockHandler 對應《限流規則》"; } /** * testSentinelResource fallback method * {@link SentinelResource} #fallback 在< 1.6的版本中,不能補貨BlockException */ public String fallback(String a) { return "fallback 對應《降級規則》"; }
使用 @SentinelRestTemplate
.ui
resttemplate.sentinel.enabled
能夠開關是否啓用該註解。(開發階段頗有意義。)rest
源碼:com.springframework.cloud.alibaba.sentinel.custom.SentinelBeanPostProcessor
@Bean @LoadBalanced @SentinelRestTemplate public RestTemplate restTemplate() { return new RestTemplate(); } @Autowired private RestTemplate restTemplate; ...
配置文件中添加 feign.sentinel.enabled: true
來開啓
編寫fallback 類,實現feign client
@Component public class UserCenterFeignClientFallback implements IUserCenterFeignClient { @Override public UserDTO findById(Long userId) { UserDTO userDTO = new UserDTO(); userDTO.setWxNickname("默認用戶"); return userDTO; } } @Slf4j @Component public class UserCenterFeignClientFallbackFactory implements FallbackFactory<IUserCenterFeignClient> { @Override public IUserCenterFeignClient create(Throwable cause) { return new IUserCenterFeignClient() { @Override public UserDTO findById(Long userId) { log.warn("遠程調用被限流/降級,{}", cause); UserDTO userDTO = new UserDTO(); userDTO.setWxNickname("默認用戶"); return userDTO; } }; } }
應用fallback class
/** * IUserCenterFeignClient for 定義 user-center feign client * fallbackFactory 能夠拿到異常信息 * fallback 沒法拿到異常信息 * * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a> * @since 2019/7/15 */ @FeignClient(name = "user-center", // fallback = UserCenterFeignClientFallback.class, fallbackFactory = UserCenterFeignClientFallbackFactory.class ) public interface IUserCenterFeignClient { @GetMapping(path = "/users/{userId}") public UserDTO findById(@PathVariable Long userId); }
啓動應用,設置流控規則,結果展現以下
{ id: 1, ... wxNickName: "默認用戶" }
源碼:org.springframework.cloud.alibaba.sentinel.feign.SentinelFeign