[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 ...
随机推荐
- 论文阅读计划2(Deep Joint Rain Detection and Removal from a Single Image)
Deep Joint Rain Detection and Removal from a Single Image[1] 简介:多任务全卷积从单张图片中去除雨迹.本文在现有的模型上,开发了一种多任务深 ...
- Android应用开机自启动问题
本文主要介绍Android应用如何实现开机自启动.自启动失败的原因以及通过ADB命令模拟发送BOOT_COMPLETED开机广播. 1.Android应用如何实现开机自启动 (1) 实现一个广播类,接 ...
- Google三驾马车:GFS、MapReduce和Bigtable
谈到分布式系统,就不得不提Google的三驾马车:Google fs[1],Mapreduce[2],Bigtable[3]. 虽然Google没有公布这三个产品的源码,但是他发布了这三个产品的详细设 ...
- VS Code真机测试步骤
VS Code真机测试步骤 前提:你的电脑跟你的手机是在同一个网络环境下.电脑连手机热点: 1. 在扩展里搜索live server,下载安装: 2. 打开cmd 命令窗口(快捷键是win+r): 输 ...
- 3023Java_控制语句
控制语句 0.前定义 语句块(有时叫做复合语句),是用花括号扩起的任意数量的简单Java语句. 块确定了局部变量的作用域.块中的程序代码,作为一个整体,是要被一起执行的. 块可以被嵌套在另一个块中,但 ...
- Centos 7上安装Python3.x(单版本)
Centos7默认安装的是2.7,这里选择安装使用Python3.6.3 安装Python3.6.3 1.安装python3 需要的依赖包 yum install -y openssl-devel b ...
- Spring Boot:快速入门教程
什么是Spring Boot? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...
- kubernetes实战篇之创建密钥自动拉取私服镜像
系列目录 前面我们讲解了如何搭建nexus服务器,以及如何使用nexus搭建docker私有镜像仓库,示例中我们都是手动docker login登陆私服,然后通过命令拉取镜像然后运行容器.然而这种做法 ...
- 推荐三个学习git的网站或教程
廖雪峰官方教程:https://www.liaoxuefeng.com/wiki/896043488029600/900388704535136 ProGit中文版:https://git-scm.c ...
- Linux下Flume的安装部署
一.前置条件 Flume需要依赖JDK 1.8+,JDK安装方式见本仓库: Linux环境下JDK安装 二 .安装步骤 2.1 下载并解压 下载所需版本的Flume,这里我下载的是CDH版本的Flum ...