sentinel blockHandler不生效:
package org.bc.sentinel.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController // @RestController注解是@Controller+@ResponseBody
public class TestController {
private final String SERVER_URL = "http://sentinel-provider"; // 这里的服务地址填写注册到Nacos的应用名称
@Resource
private RestTemplate restTemplate;
@RequestMapping("/test") // 标记是该方法是接口请求
public String test() {
return restTemplate.getForObject(SERVER_URL + "/test", String.class);//调用提供者/test接口
}
@RequestMapping("/sentinelTest")
public String sentinelTest() { // sentinel组件测试方法
// int i = 1 / 0; // 除数不能为0 ,此处必报错
return "TestController#sentinelTest 1---" + RandomUtils.nextInt(0, 10000);
}
@RequestMapping("/sentinelTestB")
public String sentinelTestB() { // sentinel组件测试方法
return "TestController#sentinelTestB " + RandomUtils.nextInt(0, 10000);
}
@RequestMapping("/sentinelTestC")
public String sentinelTestC() { // sentinel组件测试方法
return "TestController#sentinelTestC " + RandomUtils.nextInt(0, 10000);
}
/* @RequestMapping("/sentinelTest")
public String sentinelTest() { // sentinel组件测试方法
return "TestController#sentinelTest " + RandomUtils.nextInt(0, 10000);
}*/
/* @RequestMapping("/sentinelTest")
public String sentinelTest() { // sentinel组件测试方法
try {
Thread.sleep(1000); // 睡眠1 秒
} catch (InterruptedException e) {
e.printStackTrace();
}
return "TestController#sentinelTest " + RandomUtils.nextInt(0, 10000);
}*/
@RequestMapping("/blockHandlerTest")
@SentinelResource(value = "blockHandlerTest", blockHandler = "blockHandlerTestHandler") // 资源名称为blockHandlerTest 违法规则后的兜底方法是blockHandlerTest
public String blockHandlerTest(String params) { // 测试blockHandler接口的方法
//int i = 1 / 0; // 除数不能为0 ,此处必报错
return "原方法结果TestController#blockHandlerTest" + RandomUtils.nextInt(0, 1000);
}
public String blockHandlerTestHandler(String params, BlockException blockException) { // 接口/blockHandlerTest 兜底方法
return "兜底方法TestController#blockHandlerTestHandler" + RandomUtils.nextInt(0, 1000) + " " + blockException.getMessage();
}
}
后面发现:添加名称限流名称问题 需要改成
文章来源:https://www.toymoban.com/news/detail-699548.html
文章来源地址https://www.toymoban.com/news/detail-699548.html
到了这里,关于sentinel blockHandler不生效的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!