Spring Cloud Alibaba学习笔记(6) - Sentinel使用总结
使用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使用总结的更多相关文章
- Spring Cloud Alibaba学习笔记(1) - 整合Spring Cloud Alibaba
Spring Cloud Alibaba从孵化器版本毕业:https://github.com/alibaba/spring-cloud-alibaba,记录一下自己学习Spring Cloud Al ...
- Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway
Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...
- Spring Cloud Alibaba学习笔记
引自B站楠哥:https://space.bilibili.com/434617924 一.创建父工程 创建父工程hello-spring-cloud-alibaba Spring Cloud Ali ...
- Spring Cloud Alibaba学习笔记(3) - Ribbon
1.手写一个客户端负载均衡器 在了解什么是Ribbon之前,首先通过代码的方式手写一个负载均衡器 RestTemplate restTemplate = new RestTemplate(); // ...
- Spring Cloud Alibaba学习笔记(22) - Nacos配置管理
目前业界流行的统一配置管理中心组件有Spring Cloud Config.Spring Cloud Alibaba的Nacos及携程开源的Apollo,本文将介绍Nacos作为统一配置管理中心的使用 ...
- Spring Cloud Alibaba学习笔记(2) - Nacos服务发现
1.什么是Nacos Nacos的官网对这一问题进行了详细的介绍,通俗的来说: Nacos是一个服务发现组件,同时也是一个配置服务器,它解决了两个问题: 1.服务A如何发现服务B 2.管理微服务的配置 ...
- Spring Cloud Alibaba学习笔记(7) - Sentinel规则持久化及生产环境使用
Sentinel 控制台 需要具备下面几个特性: 规则管理及推送,集中管理和推送规则.sentinel-core 提供 API 和扩展接口来接收信息.开发者需要根据自己的环境,选取一个可靠的推送规则方 ...
- Spring Cloud Alibaba学习笔记(5) - 整合Sentinel及Sentinel规则
整合Sentinel 应用整合Sentinel 在dependencies中添加依赖,即可整合Sentinel <dependency> <groupId>com.alibab ...
- Spring Cloud Alibaba学习笔记(18) - Spring Cloud Gateway 内置的过滤器工厂
参考:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-clou ...
- Spring Cloud Alibaba学习笔记(23) - 调用链监控工具Spring Cloud Sleuth + Zipkin
随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请求可能最终需要调用很多次后端服务才能完成,当整个请求陷入性能瓶颈或不可用时,我们是无法得知该请求是由某个或某些后端服务引起的,这时就需要解决如何 ...
随机推荐
- sql中order by和group by的区别
order by 和 group by 的区别: 1,order by 从英文里理解就是行的排序方式,默认的为升序. order by 后面必须列出排序的字段名,可以是多个字段名. 2,group b ...
- 人脸替换(FaceSwap)的一些思考
本文链接:https://blog.csdn.net/cy1070779077/article/details/85224347人脸替换(FaceSwap)的一些思考 最一开始,我使用了openCV( ...
- redis-cli 使用密码登录
#./redis-cli 输入auth +空格+ 刚才设置的密码 成功
- Python中__new__和__init__的区别与联系
__new__ 负责对象的创建而 __init__ 负责对象的初始化. __new__:创建对象时调用,会返回当前对象的一个实例 __init__:创建完对象后调用,对当前对象的一些实例初始化,无返回 ...
- 深入理解JVM+G1+GC.pdf (中文版带书签)
目录 序 VII前言 IX 第1章 JVM & GC基础知识 11.1 引言 21.2 基本术语 31.2.1 Java相关术语 41.2.2 JVM/GC通用术语 241.2.3 G1涉及术 ...
- python web开发Django连接mysql
需要MYSQL-python,一般情况下直接pip install window系统会报错,所以要去 https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml ...
- DEBUG技巧里的问题1 双击某个变量不能显示
DEBUG模式 双击 ls_return-type 变量不能显示,提示警告消息 好像说明的不是这个问题, 把字段复制到右边的变量框里可以显示 这个确实有点奇怪了
- tp 执行sql 语句
$User = D('User'); $User->query('select * from think_user where status=1'); $User->execute('up ...
- keepalived通过飘移ip实现高可用配置步骤
环境:两台虚拟机即可 centos7.3虚拟机A 10.0.3.46 centos7.3虚拟机B 10.0.3.110 对外开放的虚拟ip 10.0.3.96(这个ip只需要在keepalived里面 ...
- text-align:justify 两端对齐
今天看页面发现一个以前没用过的css属性text-align:justify,查了一下非常实用,是一个实现文本两端对齐的属性. 使用前: 使用后: 看了一些文章还有结合inline-block+tex ...