sentinel blockHandler不生效

sentinel blockHandler不生效:
sentinel blockHandler不生效_第1张图片
sentinel blockHandler不生效_第2张图片
sentinel blockHandler不生效_第3张图片
sentinel blockHandler不生效_第4张图片

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();
    }



}

后面发现:添加名称限流名称问题 需要改成
sentinel blockHandler不生效_第5张图片
sentinel blockHandler不生效_第6张图片

sentinel blockHandler不生效_第7张图片

你可能感兴趣的:(读书笔记,sentinel)