当前位置: 首页 > news >正文

Sentinel整合RestTemplate

  1. resttemplate开启sentinel保护配置
    resttemplate.sentinel.enabled=true
    
  2. 配置sentinel-dashboard地址
    spring.cloud.sentinel.transport.dashboard=localhost:8858\
    spring.cloud.sentinel.transport.dashboard.port=8739 
    
  3. 实例化RestTemplate并加入@SentinelRestTemplate注解
    @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
    @Slf4j
    @RestController
    @RequestMapping("/test")
    public class TestController {@Autowiredprivate 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方法处理
http://www.lryc.cn/news/167875.html

相关文章:

  • 微前端学习(下)
  • Android Splash实现
  • FPGA projet : VGA
  • JDK8 升级至JDK19
  • Python3.10 IDLE更换主题
  • C# OpenVino Yolov8 Pose 姿态识别
  • 北邮22级信通院数电:Verilog-FPGA(1)实验一“跑通第一个例程” 过程中遇到的常见问题与解决方案汇总(持续更新中)
  • CSS - 鼠标移入整行高亮显示,适用于会员套餐各参数对比页面(display: table,div 转表格形式)
  • 无涯教程-JavaScript - ATAN2函数
  • Tomcat 下部署 jFinal
  • 【Spatial-Temporal Action Localization(二)】论文阅读2017年
  • 二维码智慧门牌管理系统:数据现势性,满足应用需求的根本保证
  • BF算法(C++)简单讲解
  • JVM 虚拟机 ----> Java 类加载机制
  • 《protobuf》基础语法2
  • 利用 SOAR 加快事件响应并加强网络安全
  • uni-app:通过ECharts实现数据可视化-如何引入项目
  • string 模拟与用法
  • [NLP] LLM---<训练中文LLama2(一)>训练一个中文LLama2的步骤
  • 华为云云耀云服务器L实例使用教学 | 利用华为云服务器搭建--> 基于Spring Boot+WebSocket+WebRtc实现的多人自习室
  • Postman应用——接口请求(Get和Post请求)
  • k8s pod概念、分类及策略
  • C++系列-左移运算符重载
  • 【Vue】vue中v-if的用法
  • 企业架构LNMP学习笔记54
  • C【函数】
  • 【简单教程】利用Net2FTP构建免费个人网盘,实现便捷的文件管理
  • 05-Flask-Flask查询路由方式
  • lua环境搭建数据类型
  • c++11的一些新特性