使用Sentinel API

Sentinel主要有三个核心Api:

SphU:定义资源,让资源收到监控,保护资源

SphU 包含了 try-catch 风格的 API。用这种方式,当资源发生了限流之后会抛出 BlockException。这个时候可以捕捉异常,进行限流之后的逻辑处理。

String resourceName = "test_sentinel_api";
Entry test_sentinel_api = null;
// 定义一个Sentinel保护的资源
try {
test_sentinel_api = SphU.entry(resourceName);
...
}
// 若被保护的资源被限流或者降级
catch (BlockException e) {
e.printStackTrace();
return "限流,或者降级了";
}
finally {
if (test_sentinel_api != null) {
test_sentinel_api.exit();
}
}

PS:SphU.entry(xxx) 需要与 entry.exit() 方法成对出现,匹配调用,否则会导致调用链记录异常,抛出 ErrorEntryFreeException 异常。

若 entry 的时候传入了热点参数,那么 exit 的时候也一定要带上对应的参数(exit(count, args)),否则可能会有统计错误。这个时候不能使用 try-with-resources 的方式。

Tracer:异常统计

// 针对来源
String resourceName = "test_sentinel_api";
Entry test_sentinel_api = null;
// 定义一个Sentinel保护的资源
try {
test_sentinel_api = SphU.entry(resourceName);
if (StringUtils.isBlank(a)) {
throw new IllegalArgumentException("a不能为空");
}
return a;
}
// 若被保护的资源被限流或者降级
catch (BlockException e) {
e.printStackTrace();
return "限流,或者降级了";
}
catch (IllegalArgumentException e2) {
// 统计IllegalArgumentException
Tracer.trace(e2);
return "参数非法";
}
finally {
if (test_sentinel_api != null) {
test_sentinel_api.exit();
}
}

通过 Tracer.trace(ex) 来统计异常信息时,由于 try-with-resources 语法中 catch 调用顺序的问题,会导致无法正确统计异常数,因此统计异常信息时也不能在 try-with-resources 的 catch 块中调用 Tracer.trace(ex)。

ContextUtil:实现调用来源,实现调用

// 针对来源
String resourceName = "test_sentinel_api";
ContextUtil.enter(resourceName, "study01");

通过SentinelResource注解的方式

Sentinel 支持通过 @SentinelResource 注解定义资源并配置 blockHandler 和 fallback 函数来进行限流之后的处理。

@GetMapping("test_sentinel_resource")
@SentinelResource(
value = "test_sentinel_resource",
blockHandler = "block",
fallback = "fallback"
)
public String testSentinelResource(@RequestParam(required = false) String a) {
if (StringUtils.isBlank(a)) {
throw new IllegalArgumentException("a不能为空");
}
return a;
} /**
* 处理限流或者降级
*/
public String block(String a, BlockException e) {
log.warn("限流,或者降级了", e);
return "限流,或者降级了 block";
} public String fallback(String a, BlockException e) {
return "限流,或者降级了 fallback";
}

@SentinelResource同样支持通过配置blockHandlerClass和fallbackClass配置类来进行限流后的处理

RestTemplate整合Sentinel

在初始化restTemplate的时候添加@SentinelRestTemplate注解就可以为RestTemplate整合Sentinel,代码如下:

@Bean
@LoadBalanced
@SentinelRestTemplate
public RestTemplate restTemplate() {
return new RestTemplate();
}

可以通过配置resttemplate. sentinel.enabled来开启或关闭整合,代码如下:

resttemplate:
sentinel:
# 关闭、打开SentinelRestTemplate注解
enabled: true

Feign整合Sentinel

在配置文件中添加如下配置便可以为Feign整合Sentinel:

feign:
# feign整合sentinel
sentinel:
enabled: true

如果服务被限流或降级时,如果想要定制定制自己的处理逻辑,可以使用@FeignClient注解的fallback属性;如果定制处理逻辑的同时,还想拿到异常的具体细节,可以使用fallbackFactory属性,示例代码如下:

@FeignClient(
name = "study02",
// 拿不到异常
fallback = CommentFeignClientFallback.class,
// 拿到异常
fallbackFactory = CommentFeignClientFallbackFactory.class
)
public interface CommentFeignClient {
@GetMapping("/find")
DemoComment find();
} @Component
public class CommentFeignClientFallback implements CommentFeignClient { /**
* 一旦被限流,就会进入这个方法
* @return
*/
@Override
public DemoComment find() {
DemoComment demoComment = new DemoComment();
return demoComment;
}
} @Component
@Slf4j
public class CommentFeignClientFallbackFactory implements FallbackFactory<CommentFeignClient> {
@Override
public CommentFeignClient create(Throwable throwable) {
return new CommentFeignClient() {
@Override
public DemoComment find() {
DemoComment demoComment = new DemoComment();
return demoComment;
}
};
}
}

Spring Cloud Alibaba学习笔记(6) - Sentinel使用总结的更多相关文章

  1. Spring Cloud Alibaba学习笔记(1) - 整合Spring Cloud Alibaba

    Spring Cloud Alibaba从孵化器版本毕业:https://github.com/alibaba/spring-cloud-alibaba,记录一下自己学习Spring Cloud Al ...

  2. Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway

    Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...

  3. Spring Cloud Alibaba学习笔记

    引自B站楠哥:https://space.bilibili.com/434617924 一.创建父工程 创建父工程hello-spring-cloud-alibaba Spring Cloud Ali ...

  4. Spring Cloud Alibaba学习笔记(3) - Ribbon

    1.手写一个客户端负载均衡器 在了解什么是Ribbon之前,首先通过代码的方式手写一个负载均衡器 RestTemplate restTemplate = new RestTemplate(); // ...

  5. Spring Cloud Alibaba学习笔记(22) - Nacos配置管理

    目前业界流行的统一配置管理中心组件有Spring Cloud Config.Spring Cloud Alibaba的Nacos及携程开源的Apollo,本文将介绍Nacos作为统一配置管理中心的使用 ...

  6. Spring Cloud Alibaba学习笔记(2) - Nacos服务发现

    1.什么是Nacos Nacos的官网对这一问题进行了详细的介绍,通俗的来说: Nacos是一个服务发现组件,同时也是一个配置服务器,它解决了两个问题: 1.服务A如何发现服务B 2.管理微服务的配置 ...

  7. Spring Cloud Alibaba学习笔记(7) - Sentinel规则持久化及生产环境使用

    Sentinel 控制台 需要具备下面几个特性: 规则管理及推送,集中管理和推送规则.sentinel-core 提供 API 和扩展接口来接收信息.开发者需要根据自己的环境,选取一个可靠的推送规则方 ...

  8. Spring Cloud Alibaba学习笔记(5) - 整合Sentinel及Sentinel规则

    整合Sentinel 应用整合Sentinel 在dependencies中添加依赖,即可整合Sentinel <dependency> <groupId>com.alibab ...

  9. Spring Cloud Alibaba学习笔记(18) - Spring Cloud Gateway 内置的过滤器工厂

    参考:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-clou ...

  10. Spring Cloud Alibaba学习笔记(23) - 调用链监控工具Spring Cloud Sleuth + Zipkin

    随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请求可能最终需要调用很多次后端服务才能完成,当整个请求陷入性能瓶颈或不可用时,我们是无法得知该请求是由某个或某些后端服务引起的,这时就需要解决如何 ...

随机推荐

  1. 使用FCKeditor编辑器上传文件时中文文件名乱码

    修改:editor\filemanager\browser\default\frmupload.html 文件的编码改为UTF-8 实在不行:fckeditor/editor/filemanager/ ...

  2. P1310 表达式的值

    P1310 表达式的值 题解 1.假设有两个布尔变量 x , y  x0表示使得x=0的方案数 x1表示使得x=1的方案数 y0表示使得y=0的方案数 y1表示使得y=1的方案数 | 按位或 & ...

  3. arcpy arcgis python实例教程--原点夹角距离定义线(坐标正算)

    arcpy arcgis python实例教程--原点夹角距离定义线(坐标正算) 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 此地理处理工具 ...

  4. springboot配置Filter的两种方法

    一.使用注解1. 假设Filter类的路径为com.sanro.filter @Slf4j @WebFilter(filterName = "authFilter", urlPat ...

  5. git---主分支同步到子分支

    在进行git项目协同开发的时候,每个分支的代码会被合并到主分支 master 分支上,但是如何将master分支上的代码合并到子分支上呢? 第一步:切换到本地的仓库,更新为最新的代码. 第二步:切换到 ...

  6. Ionic4.x ion-infinite-scroll 上拉分页加载更多

    <ion-header> <ion-toolbar> <ion-title> Tab One </ion-title> </ion-toolbar ...

  7. 【转】用python读写excel的强大工具:openpyxl

    最近看到好几次群里有人问xlwt.wlrd的问题,怎么说呢,如果是office2007刚出来,大家用xlsx文件用不习惯,还可以理解,这都10年过去了喂,就算没有进化到office2016,还在用of ...

  8. 【linux】gitlab 的安装以及数据迁移

    一 .安装; 1  下载相应版本rpm包并安装 https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/ wget https://mirrors ...

  9. Qt编写气体安全管理系统5-数据监控

    一.前言 本项目对设备的监控有四种视图模式,可以任意切换,数据监控.地图监控.设备监控.曲线监控,其中数据监控是最常用的,所以在主界面导航中也排在第一位,综合观察分析了很多气体安全或者组态监控软件,大 ...

  10. android多图选择器 图片/视频 单选or多选,以及视频录制。

    PictureSelector 最近项目中用到多图选择上传的需求,考虑到android机型众多问题就自己花时间写了一个,测试了大概60款机型,出现过一些问题也都一一修复了,基本上稳定了特分享出来,界面 ...