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的应用名称@Resourceprivate 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 违法规则后的兜底方法是blockHandlerTestpublic 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();}}
后面发现:添加名称限流名称问题 需要改成