Alibaba Sentinel 一 :Sentinel初识
一: Sentinel简介
Sentinel是Alibaba Spring Cloud中的重要一员,它是面向分布式服务架构的流量控制组件,主要以流量为切入点,从流量控制、熔断降级、系统自适应保护等多个维度来帮助用户保障微服务的稳定性。
Sentinel有以下几大优势:
-
丰富的应用场景,出身于阿里巴巴,已经历过千锤百炼。
-
易于使用,快速接入。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
-
多样化的流量控制手段。
-
可视化的监控和规则管理,完备的实时监控,Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
额外提一下在我所经历的项目中,遇到的限流方式还有其它两种,大家可以了解一下。一种是使用Guava中的Ratelimiter类进行限流,算法是令牌桶。还有一种是使用lua编程,在nginx中进行限流,这种方式需要很大的开发量。
二: Sentinel下载使用
Sentinel 的使用可以分为两个部分:
- 核心库(Java 客户端):不依赖任何框架/库,能够运行于 Java 7 及以上的版本的运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
-
控制台(Dashboard):Dashboard 主要负责管理推送规则、监控、管理机器信息等。
下载Sentinel Dashboard https://github.com/alibaba/Sentinel/releases
下载夏下来的是一个jar,有java环境的可以直接运行:java -jar sentinel-dashboard.jar
启动完成后,访问http://localhost:8080,需要注意,sentinel的默认端口是8080。用户名和密码默认是sentinel。
到此,sentinel的控制台启动完毕。
三:Sentinel初始化监控
新建一个项目先体验一下sentinel的功能。
在本专栏中的文章,由于是alibaba spring cloud系列,所以涉及到的项目,一般都需要注册到nacos中。同样,sentinel也不例外,虽然本示例不需要注册,但是为了系列文章的连贯性,我还是注册到nacos中了。需要先启动一个单机版的nacos。
1. 创建一个module
2. 修改pom
<dependencies> <!-- SpringCloud ailibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</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.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </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>
3. 在resources目录下新建application.yml文件
server: port: 8400 spring: application: name: sentinal-service cloud: nacos: discovery: #Nacos服务注册中心地址 server-addr: localhost:8848 sentinel: transport: #配置Sentin dashboard地址 dashboard: localhost:8080 # 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口 port: 8719 management: endpoints: web: exposure: include: '*'
4. 创建启动类
package com.xhc.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class SentinelMain { public static void main(String[] args) { SpringApplication.run(SentinelMain.class,args); } }
5. 创建一个测试业务类
package com.xhc.cloud.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @Slf4j public class FlowLimitController { @GetMapping("/testLimit") public String testLimit() { return "----testLimit"; } }
6. 启动项目,在nacos的服务列表中会发现sentinel已经注册进去了。打开sentinel,发现什么都没有,这是正常的。sentinel需要一个触发。
访问:http://localhost:8400/testLimit
然后再一次刷新sentinel,这时就会发现已经有数据显示了。
可以尝试着在浏览器中多刷新几次,sentinel控制台就会更加明显的显示访问的信息。