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来开启

  1. 编写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;
}
};
}
}
  1. 应用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);
}
  1. 启动应用,设置流控规则,结果展示如下
   {
id: 1,
...
wxNickName: "默认用户"
}

源码:org.springframework.cloud.alibaba.sentinel.feign.SentinelFeign

[Spring-Cloud-Alibaba] Sentinel 整合RestTemplate & Feign的更多相关文章

  1. Spring Cloud Alibaba Sentinel对RestTemplate的支持

    Spring Cloud Alibaba Sentinel 支持对 RestTemplate 的服务调用使用 Sentinel 进行保护,在构造 RestTemplate bean的时候需要加上 @S ...

  2. Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现

    作者 | Spring Cloud Alibaba 高级开发工程师洛夜 来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(Hystrix 停止开发...Spring Cloud ...

  3. Spring Cloud Alibaba Sentinel对Feign的支持

    Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常 ...

  4. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  5. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战 在阅读本文前,建议先阅读<Spring Cloud Alibaba | Sentinel:分布式系 ...

  6. Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探

    目录 Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探 1. Sentinel 是什么? 2. Sentinel 的特征: 3. Sentinel 的开源生 ...

  7. Spring Cloud Alibaba | Sentinel: 服务限流基础篇

    目录 Spring Cloud Alibaba | Sentinel: 服务限流基础篇 1. 简介 2. 定义资源 2.1 主流框架的默认适配 2.2 抛出异常的方式定义资源 2.3 返回布尔值方式定 ...

  8. Spring Cloud Alibaba | Sentinel: 服务限流高级篇

    目录 Spring Cloud Alibaba | Sentinel: 服务限流高级篇 1. 熔断降级 1.1 降级策略 2. 热点参数限流 2.1 项目依赖 2.2 热点参数规则 3. 系统自适应限 ...

  9. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则 前面几篇文章较为详细的介绍了Sentinel的使用姿势,还没看过的小伙伴可以访问以下链接查看: &l ...

  10. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战 Springboot: 2.1.8.RELEASE SpringCloud: Greenwich.SR2 ...

随机推荐

  1. 微信小程序把玩(三十八)获取设备信息 API

    原文:微信小程序把玩(三十八)获取设备信息 API 获取设备信息这里分为四种, 主要属性: 网络信息wx.getNetWorkType, 系统信息wx.getSystemInfo, 重力感应数据wx. ...

  2. WCF研究-中篇

    中篇 5.托管于宿主 6.消息模式 7.WCF行为-实例管理和并发控制 8.安全 5.托管于宿主 托管 宿主Host Ø承载WCF Service运行的环境 自承载方式 系统服务方式 IIS方式 WA ...

  3. 查看哪些redis命令拖慢了redis

    Redis提供了一个下面这样的命令统计工具: 127.0.0.1:6379> INFO commandstats # Commandstatscmdstat_get:calls=11352126 ...

  4. php PAC 安装

    LIUNX wget http://pecl.php.net/get/APC-3.1.8.tgz tar -zxvf APC-3.1.8.tgz cd APC-3.1.8 /usr/local/php ...

  5. 面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)

    一.概述 面向过程:根据业务逻辑从上到下写代码 函数式:将具有一些功能的代码封装到函数中,需要的时候调用即可 面向对象:对函数进行分类和封装,让开发更方便,更快捷 Java和C#只支持面型对象编程,, ...

  6. react-redux的Provider和Connect的引发的思考

    react是当下非常流行的JS框架,react秉承的设计原则是一切皆组件:react-redux是react中使用redux的桥接工具,react-redux也继承react的设计原则,使用组件的形式 ...

  7. 高并发 Nginx+Lua OpenResty系列(10)——商品详情页

    本章以京东商品详情页为例,京东商品详情页虽然仅是单个页面,但是其数据聚合源是非常多的,除了一些实时性要求比较高的如价格.库存.服务支持等通过AJAX异步加载加载之外,其他的数据都是在后端做数据聚合然后 ...

  8. DFS(一):深度优先搜索的基本思想

    采用搜索算法解决问题时,需要构造一个表明状态特征和不同状态之间关系的数据结构,这种数据结构称为结点.不同的问题需要用不同的数据结构描述. 根据搜索问题所给定的条件,从一个结点出发,可以生成一个或多个新 ...

  9. 微信小程序在ios下Echarts图表不能滑动的解决方案

    问题现象 这个问题的现象说起来很简单. 小程序页面中有一篇很长的文章,内部有一个Echarts图表,手指上下滑动观看内容. 但是手指滑动区域在Echarts图表上时,页面却不能滑动了. 如下图: 追踪 ...

  10. 配置Windows server 用户和组权限实验详解

    目录 操作步骤如下: 在Windows Server开始菜单下点击管理工具下的计算机管理 新建用户 用户创建完毕 新建文件夹 配置技术部读取"技术资料"和"常用软件&qu ...