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

相关推荐
极客先躯2 小时前
高级java每日一道面试题-2025年11月24日-容器与虚拟化题[Dockerj]-runc 的作用是什么?
java·oci 的命令行工具·最小可用·无守护进程·完全标准·创建容器的核心流程·runc 核心职责思维导图
用户60648767188962 小时前
AI 抢不走的技能:用 Claude API 构建自动化工作流实战
java
我命由我123452 小时前
Kotlin 开发 - lateinit 关键字
android·java·开发语言·kotlin·android studio·android-studio·android runtime
aXin_ya2 小时前
微服务第八天 Sentinel 四种分布式事务模式
java·数据库·微服务
Halo_tjn2 小时前
Java Set集合相关知识点
java·开发语言·算法
Linsk2 小时前
Java和JavaScript的关系真是雷峰和雷峰塔的关系吗?
java·javascript·oracle
许彰午2 小时前
我手写了一个 Java 内存数据库(二):B+ 树的插入与分裂
java·开发语言·面试
zhouwy1132 小时前
Java 快速入门笔记:从基础语法到 Spring Boot 实战
java
极创信息3 小时前
信创产品认证怎么做?信创产品测试认证的主要流程
java·大数据·数据库·金融·软件工程
SamDeepThinking3 小时前
并发量就算只有2,该上锁还得上呀
java·后端·架构