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. CLSRSC-400: A system reboot is required to continue installing.

    I try to install oracle database 12c RAC on the RedHat 7.3,when I execute the script '/u01/app/12.2. ...

  2. enum 枚举一般用法 dotnet

    public enum Demo { [Description("Moning描述")] Moning = , [Description("Afternoon描述&quo ...

  3. 使用百度网盘+Git,把版本控制托管到云端,附精彩评论

    http://www.cnblogs.com/vajoy/p/3929675.html 我试过多个这种双向同步的网盘,在网络状况不好.系统卡顿以及某些程序BUG的情况下,同步会有错乱现象,尤其是多个电 ...

  4. RocketMQ配置

    安装&配置 1.Clone&Build git clone -b develop https://github.com/apache/incubator-rocketmq.git cd ...

  5. qlineedit设置背景颜色(使用QPalette的方法不行,必须使用QSS)

    使用QPalette的方法不行, ui->le_text->setAutoFillBackground(true);qDebug() << ui->le_text-> ...

  6. Vista之前的版本,默认本地登陆用户都以管理员权限启动程序

    Vista之前的版本,默认本地登陆用户都以管理员权限启动程序,之后的OS版本默认都没有管理员权限,需要用户提权才能做某些操作,否则需要管理员权限的操作都会失败MSSQL是用户名账号连接,Socket方 ...

  7. .NET Core RC2在Linux下部署

    前言 目前ASP.NET Core RC2已经正式发布了,可以参考如下链接: https://blogs.msdn.microsoft.com/dotnet/2016/05/06/net-core-r ...

  8. Hadoop集群(第5期)SecureCRT使用

    1.SecureCRT简介   SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,同时支持Telnet和rlogin协议.SecureCRT是一款用于连接运行包括Windows. ...

  9. SYN4104型 数字网同步时钟

    SYN4104型 数字网同步时钟 产品概述 SYN4104型数字网同步时钟是由西安同步电子科技有限公司精心设计.自行研发生产的一款高精度锁相时钟频率源,接收GPS信号,使恒温晶振输出频率同步于GPS卫 ...

  10. Spring Boot的学习之路(01):缘起

    有人说,Spring Boot的出现,让Java迎来了又一春,它是Java应用开发的颠覆者,彻底改变了Java应用开发的模式. 2017年,SpringBoot闯入我的生活, 也让我迎来了又一春 我开 ...