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

相关推荐
DKPT28 分钟前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
好奇的菜鸟2 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
DuelCode3 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
优创学社23 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
幽络源小助理3 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
猴哥源码3 小时前
基于Java+springboot 的车险理赔信息管理系统
java·spring boot
YuTaoShao4 小时前
【LeetCode 热题 100】48. 旋转图像——转置+水平翻转
java·算法·leetcode·职场和发展
Dcs4 小时前
超强推理不止“大”——手把手教你部署 Mistral Small 3.2 24B 大模型
java
东阳马生架构4 小时前
订单初版—1.分布式订单系统的简要设计文档
java
万物皆字节4 小时前
spring cloud负载均衡之FeignBlockingLoadBalancerClient、BlockingLoadBalancerClient
spring cloud