我是靠谱客的博主 温柔长颈鹿,这篇文章主要介绍Sentinel 使用(1)基本使用1. 简述2. 基本使用3. 注解方式的使用5. 控制台的使用6. 主流框架支持,现在分享给大家,希望可以做个参考。

1. 简述

Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等

sentinel-features-overview

 

Sentinel 分为两个部分:

  • 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
  • 控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

2. 基本使用

1. 引入依赖

复制代码
1
2
3
4
5
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> <version>1.4.1</version> </dependency>

2. 定义规则

复制代码
1
2
3
4
5
6
7
8
9
10
private static void initFlowRules(){ List<FlowRule> rules = new ArrayList<>(); FlowRule rule = new FlowRule(); rule.setResource("HelloWorld"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS);//QPS // Set limit QPS to 20. rule.setCount(20); rules.add(rule); FlowRuleManager.loadRules(rules); }

3. 使用(抛异常)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) { // 配置规则. initFlowRules(); while (true) { Entry entry = null; try { entry = SphU.entry("HelloWorld"); // 资源中的逻辑. System.out.println("hello world"); } catch (BlockException e1) { System.out.println("blocked!"); } finally { if (entry != null) { entry.exit(); } } } }

4. 返回bool

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
if (SphO.entry("自定义资源名")) { // 务必保证finally会被执行 try { /** * 被保护的业务逻辑 */ } finally { SphO.exit(); } } else { // 资源访问阻止,被限流或被降级 // 进行相应的处理操作 }

3. 注解方式的使用

1. 加入依赖

复制代码
1
2
3
4
5
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-annotation-aspectj</artifactId> <version>x.y.z</version> </dependency>

2. 配置AOP

复制代码
1
2
3
4
<aspects> <aspect name="com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect"/> </aspects>

Springboot配置

复制代码
1
2
3
4
5
6
7
@Configuration public class SentinelAspectConfiguration { @Bean public SentinelResourceAspect sentinelResourceAspect() { return new SentinelResourceAspect(); } }

3. 使用

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class TestService { // 对应的 `handleException` 函数需要位于 `ExceptionUtil` 类中,并且必须为 static 函数. @SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class}) public void test() { System.out.println("Test"); } // 原函数 @SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback") public String hello(long s) { return String.format("Hello at %d", s); } // Fallback 函数,函数签名与原函数一致. public String helloFallback(long s) { return String.format("Halooooo %d", s); } // Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致. public String exceptionHandler(long s, BlockException ex) { // Do some log here. ex.printStackTrace(); return "Oops, error occurred at " + s; } }

4. 注解说明

  • value: 资源名称,必需项(不能为空)
  • entryType: 入口类型,可选项(默认为 EntryType.OUT
  • blockHandler blockHandlerClassblockHandler 对应处理 BlockException 的函数名称,可选项。若未配置,则将 BlockException 直接抛出。blockHandler 函数访问范围需要是 public,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException。blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
  • fallback: fallback 函数名称,可选项,仅针对降级功能生效(DegradeException)。fallback 函数的访问范围需要是 public,参数类型和返回类型都需要与原方法相匹配,并且需要和原方法在同一个类中。业务异常不会进入 fallback 逻辑。

5. 控制台的使用

1. 下载控制台

点击进入下载页:下载控制台

2. 启动

复制代码
1
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

其中 -Dserver.port=8080 用于指定 Sentinel 控制台端口为 8080

3. 项目引入依赖

复制代码
1
2
3
4
5
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-transport-simple-http</artifactId> <version>x.y.z</version> </dependency>

4. 配置启动参数

启动时加入 JVM 参数 -Dproject.name=xxxx-Dcsp.sentinel.dashboard.server=consoleIp:port 指定控制台地址和端口。若启动多个应用,则需要通过 -Dcsp.sentinel.api.port=xxxx 指定客户端监控 API 的端口(默认是 8719)

6. 主流框架支持

1. Dubbo

1. 添加依赖

复制代码
1
2
3
4
5
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-dubbo-adapter</artifactId> <version>x.y.z</version> </dependency>

引入此依赖后,Dubbo 的服务接口和方法(包括调用端和服务端)就会成为 Sentinel 中的资源,在配置了规则后就可以自动享受到 Sentinel 的防护能力。

2. 基本使用

若不希望开启 Sentinel Dubbo Adapter 中的某个 Filter,可以手动关闭对应的 Filter,比如:

复制代码
1
2
<!-- 关闭 Sentinel 对应的 Service Consumer Filter --> <dubbo:consumer filter="-sentinel.dubbo.consumer.filter"/>

或者:

复制代码
1
2
3
4
5
6
@Bean public ConsumerConfig consumerConfig() { ConsumerConfig consumerConfig = new ConsumerConfig(); consumerConfig.setFilter("-sentinel.dubbo.consumer.filter"); return consumerConfig; }

限流粒度可以是服务接口和服务方法两种粒度:

  • 服务接口:resourceName 为 接口全限定名,如 com.alibaba.csp.sentinel.demo.dubbo.FooService
  • 服务方法:resourceName 为 接口全限定名:方法签名,如 com.alibaba.csp.sentinel.demo.dubbo.FooService:sayHello(java.lang.String)

2. Spring Cloud

Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。Sentinel 与 Spring Cloud 的整合见 Sentinel Spring Cloud Starter。

Spring Cloud Alibaba 默认为 Sentinel 整合了 Servlet 、RestTemplate 和 FeignClient。Sentinel 在 Spring Cloud 生态中,不仅补全了 Hystrix 在 Servlet 和 RestTemplate 这一块的空白,而且还完全兼容了 Hystrix 在 FeignClient 中限流降级的用法,最后还支持运行时灵活地配置和调整限流降级规则。

最后

以上就是温柔长颈鹿最近收集整理的关于Sentinel 使用(1)基本使用1. 简述2. 基本使用3. 注解方式的使用5. 控制台的使用6. 主流框架支持的全部内容,更多相关Sentinel内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部