Sentinel API Github : "WIKI " Sphu (指明要保護的資源名稱) Tracer (指明調用來源,異常統計介面) ContextUtil(標示進入調用鏈入口) 流控規則(針對來源屬性) 降級規則 Sentinel Annotation 源碼: & 使用該註解重構上述方法 ...
Sentinel API
Github : WIKI
- Sphu (指明要保護的資源名稱)
- Tracer (指明調用來源,異常統計介面)
- ContextUtil(標示進入調用鏈入口)
- 流控規則(針對來源屬性)
@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();
}
}
- 降級規則
@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();
}
}
}
Sentinel Annotation
源碼:com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect
& com.alibaba.csp.sentinel.annotation.aspectj.AbstractSentinelAspectSupport
SentinelResource
使用該註解重構上述方法
@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 對應《降級規則》";
}
RestTemplate 整合Sentinel
使用 @SentinelRestTemplate
.
resttemplate.sentinel.enabled
可以開關是否啟用該註解。(開發階段很有意義。)
源碼:com.springframework.cloud.alibaba.sentinel.custom.SentinelBeanPostProcessor
@Bean
@LoadBalanced
@SentinelRestTemplate
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
...
Feign整合 Sentinel
配置文件中添加 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:[email protected]">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