-
resttemplate开启sentinel保护配置
propertiesresttemplate.sentinel.enabled=true
-
配置sentinel-dashboard地址
propertiesspring.cloud.sentinel.transport.dashboard=localhost:8858\ spring.cloud.sentinel.transport.dashboard.port=8739
-
实例化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)); } } }
-
编写测试代码,并使用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 ; } }
-
在dashboard上配置限流规则,再次通过postman调用url,能正常触发SentinelRestTemplate的blockHandler方法处理
-
在dashboard上配置熔断规则,再次通过postman调用url,能正常触发SentinelRestTemplate的fallBack方法处理
Sentinel整合RestTemplate
yicj2023-09-23 9:53
相关推荐
吃海鲜的骆驼5 分钟前
服务异步通讯与RabbitMQ羱滒8 分钟前
sql调优之数据库开发规范Forget the Dream37 分钟前
设计模式之责任链模式jonyleek40 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证Forget the Dream1 小时前
设计模式之代理模式帅的飞起来2 小时前
设计模式--spring中用到的设计模式張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题zr想努力3 小时前
Lua的table类型的增删改查操作