我是靠谱客的博主 踏实芹菜,这篇文章主要介绍SpringCloud学习笔记 - Sentinel概述,现在分享给大家,希望可以做个参考。

1. sentinel简介

sentinel官方文档:https://sentinelguard.io/zh-cn/docs/introduction.html

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。
简而言之,sentinel就是为了做微服务的服务熔断、服务降级、服务限流、服务调用雪崩的,和Hystrix具有异曲同工之妙

sentinel具有以下特性:

  • 丰富的应用场景:
    Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
  • 完备的实时监控:
    Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
  • 广泛的开源生态:
    Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
  • 完善的SPI拓展点:
    Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

在这里插入图片描述

2. sentinel下载和安装

Sentinel 的使用可以分为两个部分:

  • 核心库(Java 客户端):
    核心库是以jar包的形式运行,不依赖任何框架/库,能够运行于 Java 8 及以上的版本的运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持(见 主流框架适配)。
  • 控制台(Dashboard):
    Dashboard 主要负责管理推送规则、监控、管理机器信息等。

sentinel 安装步骤:

  1. 下载核心jar包
    git下载地址:https://github.com/alibaba/Sentinel/releases
    在这里插入图片描述

  2. 运行jar包
    java -jar sentinel-dashboard-1.7.0.jar

sentinel的默认端口是8080,所以我们需要保证8080端口没有被占用

  1. 访问dashboard控制台
    访问localhost:8080,账号密码默认均为:sentinel,可以成功访问就表示安装完成。
    在这里插入图片描述

3. 创建测试module,测试服务监控效果

  1. 创建module:cloudalibaba-sentinel-service8401
    需要将该微服务注册到服务注册中心,所以需要同步启动Nacos

  2. 引入pom依赖:主要依赖项 spring-cloud-starter-alibaba-sentinel

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<dependencies> <!-- sentinel核心依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.2.2.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>0.2.2.RELEASE</version> </dependency> <!-- 服务调用 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- SpringBoot整合Web组件+actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--日常通用jar包配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
  1. 修改yaml配置文件,添加sentinel的配置信息
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server: port: 8401 spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: server-addr: localhost:8848 #Nacos服务注册中心地址 sentinel: transport: dashboard: localhost:8080 #配置Sentinel dashboard地址 port: 8791 #默认端口 management: endpoints: web: exposure: include: "*" #暴露服务监控端点
  1. 创建主启动类
复制代码
1
2
3
4
5
6
7
8
@SpringBootApplication @EnableDiscoveryClient public class MainApp8401 { public static void main(String[] args) { SpringApplication.run(MainApp8401.class,args); } }
  1. 创建测试业务代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController @Slf4j public class FlowLimitController { @GetMapping("/testA") public String testA() { return "------testA"; } @GetMapping("/testB") public String testB() { log.info(Thread.currentThread().getName()+"t"+"...testB"); return "------testB"; } }
  1. 测试服务监控
  • 启动Nacos服务注册中心
  • 启动Sentinel8080:java -jar sentinel-dashboard-1.8.5.jar
  • 启动微服务8401
    注意:初始启动sentinel控制台中是什么都没有的,因为Sentinel采用的懒加载模式,需要被监控的服务执行一次访问即可被监控。
    http://localhost:8401/testA
    http://localhost:8401/testB
  • 监控面板:
    在这里插入图片描述

最后

以上就是踏实芹菜最近收集整理的关于SpringCloud学习笔记 - Sentinel概述的全部内容,更多相关SpringCloud学习笔记内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部