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方法处理

相关推荐
兩尛几秒前
c++的数组和Java数组的不同
java·开发语言·c++
roman_日积跬步-终至千里9 分钟前
【Java并发】多线程/并发问题集
java·开发语言
それども30 分钟前
什么是MalformedStreamException,和WebKitFormBoundary有什么关系
java
思想在飞肢体在追1 小时前
Springboot项目配置Nacos
java·spring boot·后端·nacos
cyforkk1 小时前
09、Java 基础硬核复习:异常处理(容错机制)的核心逻辑与面试考点
java·数据库·面试
??(lxy)1 小时前
java高性能无锁队列——MpscLinkedQueue
java·开发语言
数研小生1 小时前
Full Analysis of Taobao Item Detail API taobao.item.get
java·服务器·前端
Wang15302 小时前
Java编程基础与面向对象核心概念
java
毕设源码-郭学长2 小时前
【开题答辩全过程】以 康复管理系统为例,包含答辩的问题和答案
java
毅炼2 小时前
hot100打卡——day17
java·数据结构·算法·leetcode·深度优先