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

相关推荐
dhxhsgrx1 小时前
PYTHON训练营DAY25
java·开发语言·python
不知几秋2 小时前
数字取证-内存取证(volatility)
java·linux·前端
chxii5 小时前
5java集合框架
java·开发语言
yychen_java6 小时前
R-tree详解
java·算法·r-tree
JANYI20186 小时前
嵌入式设计模式基础--C语言的继承封装与多态
java·c语言·设计模式
xrkhy6 小时前
反射, 注解, 动态代理
java
Ten peaches7 小时前
Selenium-Java版(操作元素)
java·selenium·测试工具·html
lyw2056197 小时前
RabbitMQ,Kafka八股(自用笔记)
java
邹诗钰-电子信息工程7 小时前
嵌入式自学第二十一天(5.14)
java·开发语言·算法
有梦想的攻城狮7 小时前
spring中的@MapperScan注解详解
java·后端·spring·mapperscan