重要、重要、重要
Sentinel中文文档:https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
Sentinel和Hystrix
1.Hystrix
1.1 需要我们自己手动搭建监控平台
1.2 没有一套web界面可以给我们进行更加细粒度化的配置。比如:流控、速率、服务熔断、服务降级。。。。。
2.Sentinel
2.1 单独一个组件,可以独立出来
2.2 直接界面化的细粒度同意配置
3. 两个理念基本相同
Sentinel特征(从官方文档抄的)
- 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
- 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
- 完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
Sentinel下载、安装、配置
1.Sentinel服务端
1. 1下载地址:https://github.com/alibaba/Sentinel/tags
1.2 java -jar ./sentinel-dashboard-1.8.0.jar (如下图:端口是8080)
1.3 登录Sentinel后台,地址:http://localhost:8080/ ,默认账号、密码:sentinel
2.Sentinel客户端
2.1 创建Sentinel客户端、配置
pom依赖:
<dependencies>
<!--spring cloud alibaba sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
application.yml配置:
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
#nacos注册中心地址(nacos集群地址)
discovery:
server-addr: localhost:1111
#nacos注册配置中心地址(nacos集群地址)
config:
server-addr: localhost:1111
#指定配置文件类型
file-extension: yaml
sentinel:
transport:
# 配置Sentinel dashboard 地址
dashboard: localhost:8080
# 配置Sentinel端口,默认8719。如果被占用会自动从8719往上+1,直至找到没有被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: '*'
测试controller信息:
package com.tlh.alibabacloud.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FlowLimitController {
@GetMapping("/testA")
public String testA() {
return "this is test A";
}
@GetMapping("/testB")
public String testB() {
return "this is test B";
}
}
我们通过http请求我们的测试接口,然后在Sentinel Dashboard界面就能看到如下信息:
我们的整合Sentinel成功了,后面我们学习用Sentinel怎么来限流、熔断等等
其他
1.参考代码:https://github.com/TianLuhua/springCloud2020.git
2.Sentinel:需要自己下载,然后跑起来
3.Sentinel客户端:cloudalibaba-sentinel-service8401