[Spring-Cloud-Alibaba] Sentinel 整合RestTemplate & Feign
Sentinel API
Github : WIKI
- Sphu (指明要保护的资源名称)
- Tracer (指明调用来源,异常统计接口)
- ContextUtil(标示进入调用链入口)
- 流控规则(针对来源属性)
@GetMapping("/test-sentinel-api")
public String testSentinelAPI(@RequestParam(required = false) String a) {
String resourceName = "test-sentinel-api";
ContextUtil.enter(resourceName, "user-center-service");
// 定义一个sentinel 保护的资源,名称是test-sentinel-api
Entry entry = null;
try {
entry = SphU.entry(resourceName);
// ...被保护的业务逻辑处理
if (StringUtils.isEmpty(a)) {
// Sentinel 默认只会统计BlockException & BlockException的子类,如果想统计其他异常信息,添加Tracer
throw new IllegalArgumentException("A is not empty.");
}
return a;
// block Exception: 如果被保护的资源被限流或者降级了,就会抛异常出去
} catch (BlockException e) {
log.error("我被限流啦!!{}", e);
return "我被限流啦!!";
} catch (IllegalArgumentException argEx) {
// 统计当前异常发生次数 / 占比
Tracer.trace(argEx);
return "非法参数信息";
} finally {
if (entry != null) {
entry.exit();
}
ContextUtil.exit();
}
}
- 降级规则
@GetMapping("/test-sentinel-api")
public String testSentinelAPI(@RequestParam(required = false) String a) {
// 定义一个sentinel 保护的资源,名称是test-sentinel-api
Entry entry = null;
try {
entry = SphU.entry("test-sentinel-api");
// ...被保护的业务逻辑处理
if (StringUtils.isEmpty(a)) {
// Sentinel 默认只会统计BlockException & BlockException的子类,如果想统计其他异常信息,添加Tracer
throw new IllegalArgumentException("A is not empty.");
}
return a;
// block Exception: 如果被保护的资源被限流或者降级了,就会抛异常出去
} catch (BlockException e) {
log.error("我被限流啦!!{}", e);
return "我被限流啦!!";
} catch (IllegalArgumentException argEx) {
// 统计当前异常发生次数 / 占比
Tracer.trace(argEx);
return "非法参数信息";
} finally {
if (entry != null) {
entry.exit();
}
}
}
Sentinel Annotation
源码:com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect
& com.alibaba.csp.sentinel.annotation.aspectj.AbstractSentinelAspectSupport
SentinelResource
使用该注解重构上述方法
@GetMapping("/test-sentinel-resource")
@SentinelResource(value = "test-sentinel-api", blockHandler = "blockException", fallback = "fallback")
public String testSentinelResource(@RequestParam(required = false) String a) {
// ...被保护的业务逻辑处理
if (StringUtils.isEmpty(a)) {
// Sentinel 默认只会统计BlockException & BlockException的子类,如果想统计其他异常信息,添加Tracer
throw new IllegalArgumentException("A is not empty.");
}
return a;
}
/**
* testSentinelResource BlockException method
*/
public String blockException(String a, BlockException e) {
log.error("限流了,{}", e);
return "blockHandler 对应《限流规则》";
}
/**
* testSentinelResource fallback method
* {@link SentinelResource} #fallback 在< 1.6的版本中,不能补货BlockException
*/
public String fallback(String a) {
return "fallback 对应《降级规则》";
}
RestTemplate 整合Sentinel
使用 @SentinelRestTemplate
.
resttemplate.sentinel.enabled
可以开关是否启用该注解。(开发阶段很有意义。)
源码:com.springframework.cloud.alibaba.sentinel.custom.SentinelBeanPostProcessor
@Bean
@LoadBalanced
@SentinelRestTemplate
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
...
Feign整合 Sentinel
配置文件中添加 feign.sentinel.enabled: true
来开启
- 编写fallback 类,实现feign client
@Component
public class UserCenterFeignClientFallback implements IUserCenterFeignClient {
@Override
public UserDTO findById(Long userId) {
UserDTO userDTO = new UserDTO();
userDTO.setWxNickname("默认用户");
return userDTO;
}
}
@Slf4j
@Component
public class UserCenterFeignClientFallbackFactory implements FallbackFactory<IUserCenterFeignClient> {
@Override
public IUserCenterFeignClient create(Throwable cause) {
return new IUserCenterFeignClient() {
@Override
public UserDTO findById(Long userId) {
log.warn("远程调用被限流/降级,{}", cause);
UserDTO userDTO = new UserDTO();
userDTO.setWxNickname("默认用户");
return userDTO;
}
};
}
}
- 应用fallback class
/**
* IUserCenterFeignClient for 定义 user-center feign client
* fallbackFactory 可以拿到异常信息
* fallback 无法拿到异常信息
*
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
* @since 2019/7/15
*/
@FeignClient(name = "user-center",
// fallback = UserCenterFeignClientFallback.class,
fallbackFactory = UserCenterFeignClientFallbackFactory.class
)
public interface IUserCenterFeignClient {
@GetMapping(path = "/users/{userId}")
public UserDTO findById(@PathVariable Long userId);
}
- 启动应用,设置流控规则,结果展示如下
{
id: 1,
...
wxNickName: "默认用户"
}
源码:org.springframework.cloud.alibaba.sentinel.feign.SentinelFeign
[Spring-Cloud-Alibaba] Sentinel 整合RestTemplate & Feign的更多相关文章
- Spring Cloud Alibaba Sentinel对RestTemplate的支持
Spring Cloud Alibaba Sentinel 支持对 RestTemplate 的服务调用使用 Sentinel 进行保护,在构造 RestTemplate bean的时候需要加上 @S ...
- Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现
作者 | Spring Cloud Alibaba 高级开发工程师洛夜 来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(Hystrix 停止开发...Spring Cloud ...
- Spring Cloud Alibaba Sentinel对Feign的支持
Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...
- Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战
Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战 在阅读本文前,建议先阅读<Spring Cloud Alibaba | Sentinel:分布式系 ...
- Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探
目录 Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探 1. Sentinel 是什么? 2. Sentinel 的特征: 3. Sentinel 的开源生 ...
- Spring Cloud Alibaba | Sentinel: 服务限流基础篇
目录 Spring Cloud Alibaba | Sentinel: 服务限流基础篇 1. 简介 2. 定义资源 2.1 主流框架的默认适配 2.2 抛出异常的方式定义资源 2.3 返回布尔值方式定 ...
- Spring Cloud Alibaba | Sentinel: 服务限流高级篇
目录 Spring Cloud Alibaba | Sentinel: 服务限流高级篇 1. 熔断降级 1.1 降级策略 2. 热点参数限流 2.1 项目依赖 2.2 热点参数规则 3. 系统自适应限 ...
- Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则
Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则 前面几篇文章较为详细的介绍了Sentinel的使用姿势,还没看过的小伙伴可以访问以下链接查看: &l ...
- Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战
Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战 Springboot: 2.1.8.RELEASE SpringCloud: Greenwich.SR2 ...
随机推荐
- 让bootstrap表格自动出现水平滚动条
<div class="table-responsive"><!--表格自动出现水平滚动条-> <table id="tb_departme ...
- PRML Chapter3
曲线拟合的几种方法 最大似然估计MLE,最大后验概率MAP:MLE和MAP MLE 给定一堆数据,假如我们知道它是从某一种分布中随机取出来的,可是我们并不知道这个分布具体的参,即"模型已定, ...
- MySQL 其它基本操作
索引 所谓索引,就是类似于书的目录,目的也类似,都是为了提高检索速度.ALTER TABLE <表名> ADD INDEX <索引名(列名)>;或者CREATE INDEX & ...
- HTML5 拖放并删除效果的简单实现
Html5 支持元素drag drop的功能需求,以后实现这类效果会简单很多.. 详细的文档说明在这里 代码如下所示:
- Delphi中动态调用TXMLDocument的经历
var vXMLDocument: TXMLDocument;begin vXMLDocument := TXMLDocument.Create('c:/temp/temp.xml'); Cap ...
- C语言程序的内存布局
C语言程序的内存布局 一:C语言程序的存储区域 C语言编写的程序经过编绎-链接后,将形成一个统一的文件,它由几个部分组成,在程序运行时又会产生几个其他部分,各个部分代表了不同的存储区域: 1.代码段( ...
- 关于Windows更新窗口内容的问题(作为一个实验,效果很明显)
Windows中的窗口在特定情况下会由系统进行重绘,如无效区域重新显现时,,会向窗口的处理过程发送VM_PAINT消息,但是,可能还有Windows自己的更新窗口处理,如在下面的代码中,将击键显式地转 ...
- redis python 操作 Python操作Redis数据库
原文章于此:https://www.cnblogs.com/cnkai/p/7642787.html 有个人修改与改正 Python操作Redis数据库 连接数据库 StrictRedisfrom ...
- Bitmap的读写和几个小儿科的滤镜效果~
闲来玩玩图像处理,拿破仑说过:“不想自己实现滤镜的美工不是好程序员~~#@!*^...#&!@......” 因为在学校做过很多美工的工作,而且从小就喜欢画画所以对图像相关的东西都还比较感兴 ...
- 对SpringMVC架构进行工程拆分遇到的问题总结
经过一个月的开发,一个单一的SpringMVC教育类创业项目正式上线,随着时间的推移,业务流量逐渐增大,最近对这个单一的工程从零进行SOA分布式改造,改造包括dubbo改造,集成化部署.高可用集群,负 ...