SpringCloud Sentinel 使用restTemplate的两种配置介绍
@SentinelResource 主要属性介绍
value:自定义的资源名称。不设置默认为当前类全名.方法名。
blockHandler:降级的处理方法名,默认在当前类中匹配。如果指定了blockHandlerClass(降级处理的类名) ,则在指定类中匹配。
fallback:异常处理的处理方法名,默认在当前类中匹配。如果指定了fallbackClass(异常处理的类名) ,则在指定类中匹配。
一、RestTemplate使用@SentinelResource 接入sentinel
在接口方法上使用@SentinelResource注解,并指定其降级处理方法,以及异常处理方法。
当设置控流阈值之后,请求场景超过阈值则超过的部分将进入降级处理方法。
当服务接口发生异常时,没有超过降级配置的阈值时进入异常处理方法。如果超过降级配置的阈值时,则全部进入降级处理方法。
package com.xiaohui.springcloud.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xiaohui.springcloud.entities.CommonResult;
import com.xiaohui.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
@RequestMapping("/consumer")
public class RestOrderController {@Autowiredprivate RestTemplate restTemplate;@SentinelResource(value = "consumer/product/get4",blockHandler = "getPaymentBlockHandler",fallback = "getPaymentFallback")@GetMapping("/product/get4")public CommonResult<Payment> getPaymentById4(@RequestParam(value = "id",required = true) Long id){CommonResult<Payment> commonResult = null;try{Thread.sleep(200);}catch (Exception e){e.printStackTrace();}commonResult = restTemplate.getForObject("http://cloud-payment-service/payment/get/"+id,CommonResult.class);return commonResult;}public CommonResult<Payment> getPaymentBlockHandler(Long id, BlockException be){return new CommonResult<>(402,"熔断降级方法",null);}public CommonResult<Payment> getPaymentFallback(Long id, Throwable te){return new CommonResult<>(403,"异常降级方法",null);}
}
二、RestTemplate使用@SentinelRestTemplate接入sentinel
改造文件:
- controller 不使用@SentinelResource 注解
- 调整 RestTemplate配置文件
- 创建fallbackClass 编写失败处理函数
- 创建blockHandlerClass 编写降级处理函数
如果测试后唯有进入到降级方法中,有可能需要将RestTemplate类配置对象放在主启动类中,在配置文件中添加
resttemplate:sentinel:enabled: true
如果在不行,有可能为SpringBoot、SpringCloud、SpringCloud Alibaba 版本不匹配导致。
1,com.xiaohui.springcloud.controller.RestOrderController2.java
package com.xiaohui.springcloud.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xiaohui.springcloud.entities.CommonResult;
import com.xiaohui.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
@RequestMapping("/consumer2")
public class RestOrderController2 {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/product/get")public CommonResult<Payment> getPaymentById4(@RequestParam(value = "id",required = true) Long id){CommonResult<Payment> commonResult = null;try{Thread.sleep(200);}catch (Exception e){e.printStackTrace();}commonResult = restTemplate.getForObject("http://cloud-payment-service/payment/get/"+id,CommonResult.class);return commonResult;}
}
2,com.xiaohui.springcloud.config.ApplicationContextConfig.java
package com.xiaohui.springcloud.config;import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.xiaohui.springcloud.exception.ExceptionUtil;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;/**** 相当于 spring的 application.xml bean注解相当于 bean 标签*/
@Configuration
public class ApplicationContextConfig {@Bean@LoadBalanced@SentinelRestTemplate(fallbackClass = ExceptionUtil.class,fallback = "fallBack",blockHandlerClass = ExceptionUtil.class,blockHandler = "handleBlock")public RestTemplate getRestTemplate(){return new RestTemplate();}
}
3, 降级与异常处理类编写在一个类中。
package com.xiaohui.springcloud.exception;import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.xiaohui.springcloud.entities.CommonResult;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;public class ExceptionUtil {public static SentinelClientHttpResponse handleBlock(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException be){CommonResult commonResult = new CommonResult(500,"降级处理函数。。。。。");return new SentinelClientHttpResponse(JSON.toJSONString(commonResult));}public static SentinelClientHttpResponse fallBack(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException be){CommonResult commonResult = new CommonResult(500,"异常处理函数。。。。。");return new SentinelClientHttpResponse(JSON.toJSONString(commonResult));}
}
SpringCloud Sentinel 使用restTemplate的两种配置介绍相关推荐
- mysql写什么不同_mysql - 编译配置PHP时,两种配置写法有什么不同
在编译PHP时, --with-扩展库=DIR --enable-扩展库 这两种配置有什么不同 回复内容: 在编译PHP时, --with-扩展库=DIR --enable-扩展库 这两种配置有什么不 ...
- Java框架篇---spring aop两种配置方式
Java框架篇---spring aop两种配置方式 第一种:注解配置AOP 注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中 ...
- QQ空间迁移_【Frame_relay子接口的两种配置方法】
Frame relay子接口的两种配置方法 2009-05-18 17:31:39 配置Frame relay子接口 R1:R3 103:301 R2:R3 203:302 1,多点子接口R1 con ...
- Solaris IPMP两种配置方法
Solaris IPMP两种配置方法 更换华为防火墙后,发现Solaris基于ip探测方式配置的IPMP 呈现failed状态.经过分析,更改为基于link的IPMP配置方式后,解决了这次问题. 1. ...
- 基本磁盘和动态磁盘是硬盘的两种配置类型,Win10可识别动态磁盘
今天小编将为小伙伴们分享Windows 10操作系统磁盘类型与文件系统. 理解磁盘类型和分区形式.分区和卷 基本磁盘和动态磁盘是硬盘在Windows操作系统中的两种配置类型.所有版本的Windows操 ...
- java 配置hibernate_Hibernate实现有两种配置,xml配置与注释配置
hibernate实现有两种配置,xml配置与注释配置. (1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) /p& ...
- SpringBoot的properties和yml两种配置方式, 配置注入参数, 以及配置文件读取失效的问题
SpringBoot支持两种配置方式,一种是properties文件,一种是yml 首先在pom文件中添加依赖: <dependency><groupId>org.spring ...
- dhcpd option43的两种配置方法
最近在研究dhcpd的配置,发现config文件支持好多种表达式,挺有意思的.项目要用到DHCP的option 43(厂商特定信息选项)字段,用来给客户端下发ACS URL.本文就基于该需求给出了两种 ...
- Servlet 过滤器两种配置方法
Servlet 过滤器两种配置方法 方法一(注解) /** * 使用注解标注过滤器 * @WebFilter将一个实现了javax.servlet.Filte接口的类定义为过滤器 * 属性filter ...
最新文章
- Oracle Client安装
- php处理html5文件上传代码,HTML5中文件上传的代码
- 闭环思维之follow through和及时反馈
- nodejs 快速搭建接口
- ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的方法...
- 一个值得收藏的小工具
- win32 htmlayout dom操作demo
- netstat命令详解Linux,Linux netstat命令详解
- Docker部署AI算法教程
- C语言的应用之单片机学习
- light动名词_英语里有些动词有名词形式,那还用不用它的动名词?怎么区分?...
- mac系统下修改usr/bin文件夹权限问题
- 精选教程:来亲手开发个问答社区,干掉知乎!
- 小米 12 Ultra 搭载 3D ToF 摄像头和 Surge C2 ISP
- 中国四大发明原理//2021-2-6
- 技术管理进阶——你了解成长的全貌吗?
- Linux学习笔记之tail命令显示最后n行
- 婚恋交友app的开发解决方案
- 转一个非常好的晶体学学习网站
- 10进制与26进制转化
热门文章
- [转载]windows2003上IIS+PyISAPIe1.1..0部署成功
- golang mysql大量连接_golang mysql 如何设置最大连接数和最大空闲连接数
- ES基础概念和集群概念
- php通过js发送请求数据,使用原生javascript发送ajax请求数据的步骤
- 25马5跑道,求最快的五匹马的需要比赛的次数
- mysql数据库从删库到跑路之mysq索引
- 《微信小程序开发入门精要》——第2章,第2.8节带边距的水平等间隔排列
- MFC之处理消息映射的步骤...
- PHP 制作通讯录(六)
- 网络设计分层设计的原理