一、测试 fallBack
@SentinelResource(value = "fallback",fallback = "handlerFallBack")
fallBack 是负责所有异常的,如果没有配置blockHandler,那么超出sentinel设置阈值的请求也会进到fallback里面
- 修改 CCircleBreakerController
@RestController
public class CCircleBreakerController {
@Value("${service-url.nacos-provider-service}")
private String url;
@Resource
private RestTemplate restTemplate;
//1.FallBack 只负责业务异常
@SentinelResource(value = "fallback",fallback = "handlerFallBack")
@GetMapping(value = "/consumer/fallback/{id}")
public String testRestTemplate(@PathVariable("id") Integer id){
String result = restTemplate.getForObject(url + "/provider/fallback/" + id, String.class);
if(id==4){
throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
}else if(result==null){
throw new NullPointerException("NullPointerException,该id没有对应记录,空指针异常");
}
return "Consumer 远程调用 --> "+result;
}
/**
* 本例是 fallBack
* @param id
* @return
*/
public String handlerFallBack(@PathVariable("id") Integer id, Throwable e){
return "系统异常,Consumer 进行服务降级处理 handlerFallBack: "+e.getMessage();
}
}
-
配置流控:新建流控规则
新建流控规则 -
测试,当程序出现异常时,会调用服务降级方法,当sentinel 控制台配置 违规时,不会调用该方法。
====================================================================================
二、测试 blockHandler
@SentinelResource(value = "fallback",blockHandler = "blockHandler")
只负责 sentinel 控制台配置违规
blockHandler只是fallBack 的一个特殊情况,只捕捉BlockHandlerException
- 修改 CCircleBreakerController
@RestController
public class CCircleBreakerController {
@Value("${service-url.nacos-provider-service}")
private String url;
@Resource
private RestTemplate restTemplate;
//2.blockHandler 只负责 sentinel 控制台配置违规
@SentinelResource(value = "fallback",blockHandler = "blockHandler")
@GetMapping(value = "/consumer/fallback/{id}")
public String testRestTemplate(@PathVariable("id") Integer id){
String result = restTemplate.getForObject(url + "/provider/fallback/" + id, String.class);
if(id==4){
throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
}else if(result==null){
throw new NullPointerException("NullPointerException,该id没有对应记录,空指针异常");
}
return "Consumer 远程调用 --> "+result;
}
/**
* 本例是 blockHandler
* @param id
* @return
*/
public String blockHandler(@PathVariable("id") Integer id, BlockException e){
return "系统异常,Consumer 进行服务降级处理 BlockException : "+e.getMessage();
}
}
-
配置流控:新建流控规则
新建流控规则 -
测试,只负责 sentinel 控制台配置违规,不负责 Java程序异常
超过设定的QPS
====================================================================================
三、测试 fallBack + blockHandler
fallBack 负责程序异常,blockHandler负责 sentinel 控制台异常
当两者异常都有的话 只会进入 blockHandler 异常处理
- 修改 CCircleBreakerController
@RestController
public class CCircleBreakerController {
@Value("${service-url.nacos-provider-service}")
private String url;
@Resource
private RestTemplate restTemplate;
//3.两者都配置 fallBack 负责程序异常,blockHandler负责 sentinel 控制台异常
@SentinelResource(value = "fallback",fallback = "handlerFallBack",blockHandler = "blockHandler")
@GetMapping(value = "/consumer/fallback/{id}")
public String testRestTemplate(@PathVariable("id") Integer id){
String result = restTemplate.getForObject(url + "/provider/fallback/" + id, String.class);
if(id==4){
throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
}else if(result==null){
throw new NullPointerException("NullPointerException,该id没有对应记录,空指针异常");
}
return "Consumer 远程调用 --> "+result;
}
/**
* 本例是 fallBack
* @param id
* @return
*/
public String handlerFallBack(@PathVariable("id") Integer id, Throwable e){
return "系统异常,Consumer 进行服务降级处理 handlerFallBack: "+e.getMessage();
}
/**
* 本例是 blockHandler
* @param id
* @return
*/
public String blockHandler(@PathVariable("id") Integer id, BlockException e){
return "系统异常,Consumer 进行服务降级处理 BlockException : "+e.getMessage();
}
}
-
配置流控:新建流控规则
新建流控规则
3.测试, fallBack 负责程序异常,blockHandler负责 sentinel 控制台异常
超过设定的QPS Java程序异常 超过设定的QPS并且Java程序异常
四、程序忽略异常
exceptionsToIgnore = {IllegalArgumentException.class}),假如出现该异常,fallBack兜底,不没有降级效果
- Java代码修改,只修改注解
@SentinelResource(value = "fallback"
,fallback = "handlerFallBack"
,blockHandler = "blockHandler"
,exceptionsToIgnore = {IllegalArgumentException.class})//假如出现该异常,fallBack兜底,不没有降级效果
-
测试,发现不会做降级处理,直接抛出异常
不会做降级处理
网友评论