先来简单看一下 Sentinel 的简介
Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。
Sentinel 具有以下特性:
丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
为了体验Sentinel的使用,我们先使用docker搭建一个sentinel-dashboard,以方便我们通过web界面直观的看到sentinel的使用和功能,使用如下docker命令即可搭建一个sentinel
docker run --name sentinel -d -p 8858:8858 bladex/sentinel-dashboard:1.7.0
等到容器运行起来之后,我们可以通过http://localhost:8858访问sentinel的web界面
直接使用默认的用户名sentinel密码sentinel即可登录
当然,sentinel的作用是为我们的服务提供熔断限流的,那么它如何对我们的服务进行监控呢?继续之前的项目,我们创建一个模块cloud-sentinel-service-8010来让sentinel对它进行监控
pom.xml配置文件如下
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloudservice</artifactId> <groupId>com.felix</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>cloud-sentinel-service-8010</artifactId>
<dependencies> <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>com.felix</groupId> <artifactId>cloud-common</artifactId> </dependency> <!--nacos 服务注册和发现--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--nacos 配置--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--springcloud openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!--sentinel持久化--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> </dependencies>
</project>
复制代码
这里引入了sentinel和sentinel持久化的jar包
然后,配置bootstrap.yml
spring: application: name: cloud-sentinel-service-8010 cloud: nacos: discovery: server-addr: 192.168.2.162:8848 sentinel: transport: dashboard: 192.168.2.162:8858 #dashboard地址
ribbon: ReadTimeout: 60000 #等待服务器处理的超时时间 ConnectTimeout: 2000 #建立 socket 连接所需要的超时时间
server: port: 8010
management: endpoints: web: exposure: include: '*'
复制代码
在此配置文件中,配置了sentinel的 dashboard地址
写一个简单的接口,以便后面进行测试LimitingController
package com.felix.sentinel.controller;
import com.felix.common.result.CommonResult;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class LimitingController { @GetMapping(value = "/testA") public CommonResult testA(){ return CommonResult.success("TEST A"); }
@GetMapping(value = "/testB") public CommonResult testB(){ return CommonResult.success("TEST B"); }}
复制代码
项目目录如下:
启动项目后,我们必须先访问一下cloud-sentinel-service-8010的接口,因为sentinel采用了懒加载的方式,访问接口后才能在sentinel-dashboard中查看到当前的服务
此时,sentinel的界面中可以看到我们对于接口的访问
至此,我们初步实现了sentinel对于服务的监控,下面,我们将会对接口的限流进行实际的操作.
评论