Sentinel整合RestTemplate

  1. resttemplate开启sentinel保护配置

    properties 复制代码
    resttemplate.sentinel.enabled=true
  2. 配置sentinel-dashboard地址

    properties 复制代码
    spring.cloud.sentinel.transport.dashboard=localhost:8858\
    spring.cloud.sentinel.transport.dashboard.port=8739 
  3. 实例化RestTemplate并加入@SentinelRestTemplate注解

    java 复制代码
    @Configuration
    public class RestTemplateConfig {
        @Bean
        @LoadBalanced
        @SentinelRestTemplate(
                fallbackClass = ExceptionUtil.class,fallback = "fallBack",
                blockHandlerClass = ExceptionUtil.class, blockHandler = "handleBlock")
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
        public static class ExceptionUtil {
            public static ClientHttpResponse handleBlock(
                    HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException be){
                RestResponse<Void> commonResult =  RestResponse.error("500","降级处理函数 block 。。。。。");
                return new SentinelClientHttpResponse(JSON.toJSONString(commonResult));
            }
    
            public static ClientHttpResponse fallBack(
                    HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException be){
                RestResponse<Void> commonResult = RestResponse.error("500","异常处理函数 fallback 。。。。。");
                return new SentinelClientHttpResponse(JSON.toJSONString(commonResult));
            }
        }
    }
  4. 编写测试代码,并使用postman访问对应的url

    java 复制代码
    @Slf4j
    @RestController
    @RequestMapping("/test")
    public class TestController {
        @Autowired
        private RestTemplate restTemplate ;
    
        @GetMapping("/index")
        public RestResponse<String> index(){
            String url = "http://hello-nacos-client/hello/index" ;
            RestResponse<String> retValue = restTemplate.getForObject(url, RestResponse.class);
            log.info("ret value : {}", retValue);
            return retValue ;
        }
         
        @GetMapping("/exception")
        public Object exception(){
            String url = "http://hello-nacos-client/hello/exception" ;
            RestResponse<String> retValue = restTemplate.getForObject(url, RestResponse.class);
            log.info("ret value : {}", retValue);
            return retValue ;
        }
    }
  5. 在dashboard上配置限流规则,再次通过postman调用url,能正常触发SentinelRestTemplate的blockHandler方法处理

  6. 在dashboard上配置熔断规则,再次通过postman调用url,能正常触发SentinelRestTemplate的fallBack方法处理

相关推荐
吃海鲜的骆驼5 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
羱滒8 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
Forget the Dream37 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek40 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
Forget the Dream1 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
帅的飞起来2 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar
zr想努力3 小时前
Lua的table类型的增删改查操作
java·开发语言·lua