1. docker部署sentinel
-
将
jdk安装包、sentinel的jar包上传到服务器上传jdk、sentinel
-
编辑
dockerfile-
vim dockerfile编辑 dockerfile
-
dockerfile的内容# 基础镜像 FROM ubuntu:18.04 # 将当前文件夹下的 jdk-8u221-linux-x64.tar.gz 复制并解压到 /opt/ 下 ADD ./jdk-8u221-linux-x64.tar.gz /opt/ # 配置环境变量 ENV JAVA_HOME /opt/jdk1.8.0_221 ENV CLASSPATH .;${JAVA_HOME}/lib;${JAVA_HOME}/jre/lib; ENV PATH ${JAVA_HOME}/bin:$PATH # 将当前文件夹下的 sentinel-dashboard-1.7.2.jar 复制到 /opt/ 下,不解压 COPY ./sentinel-dashboard-1.7.2.jar /opt/ # 切换目录 WORKDIR /opt/ # 运行 sentinel CMD ["java","-jar","sentinel-dashboard-1.7.2.jar"]CMD ["java","-jar","sentinel-dashboard-1.7.2.jar"]这一行代码中,["java","-jar","sentinel-dashboard-1.7.2.jar"]中不能有空格,否则有可能会导致容器自动退出。dockerfile内容
-
-
构建镜像
docker build -t zero/ubuntu-sentinel ./- 说明
-
-t:后面跟着镜像的名字,一般为作者的名称/基础镜像-镜像名-版本号 -
./:当前文件夹下的dockerfile,如果dockerfile在其他文件夹或者其他的名字,可以指定
-
构建镜像
-
运行容器
-
查看镜像:
docker images查看容器
-
运行容器
# 运行容器 docker run --name='zero-sentinel' -p 8217:8080 -d zero/ubuntu-sentinel # 查看容器 docker ps -a-
--name='zero-sentinel'指定容器名为zero-sentinel -
-p 8217:8080指定宿主机的端口8217映射容器的端口8080,sentinel的默认端口是8080 -
-d后台运行 -
zero/ubuntu-sentinel为镜像名
运行容器
-
-
-
浏览器登录
sentinel-
登录sentinel
-
初始
账号/密码:sentinel账号/密码:sentinel
-
首页
首页
-
-
可用
docker-compose安装sentinel-
docker-compose.ymlversion: "3.7" services: zero-sentinel: build: ./ restart: always ports: - 8718:8080 -
docker-compose up -d当前目录
运行
查看容器
浏览器登录
-
-
远程服务器
sentinel的使用很不稳定,有可能没有监控信息,但是可以使用熔断不能监控
2. feign集成sentinel
spring-cloud-alibaba-sentinel github 官方文档
-
核心依赖
-
依赖
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> </dependencies> -
spring-cloud-alibaba-sentinel的父依赖<properties> <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version> </properties> <dependencyManagement> <!-- spring-cloud-alibaba 父依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencyManagement>
-
-
配置文件
-
提供者
sentinel-provider的application.yml主配置文件及启动类-
application.ymlserver: # 项目访问端口号 port: 8081 spring: application: # 注册到注册中心的服务名 name: sentinel-provider cloud: nacos: discovery: # 注册中心的地址 server-addr: 120.25.207.44:8848 sentinel: transport: # 推送数据,sentinel熔断器的地址 dashboard: 120.25.207.44:8217 # 接收数据,当在 sentinel 的面板中配置一些参数,会通过该端口传送过来 port: 8719 -
启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; // 开启 nacos 注册中心扫描,发现该服务 @EnableDiscoveryClient @SpringBootApplication public class SentinelProviderApplication { public static void main(String[] args) { SpringApplication.run(SentinelProviderApplication.class, args); } }
-
-
消费者
sentinel-consumer的application.yml主配置文件及启动类-
application.ymlserver: # 项目访问端口 port: 8082 servlet: context-path: /api/v1 spring: application: # 注册到注册中心的服务名 name: sentinel-consumer cloud: nacos: discovery: # 注册中心的地址 server-addr: 120.25.207.44:8848 # 不将自己注册到注册中心 register-enabled: false sentinel: transport: # 推送数据,sentinel熔断器的地址 dashboard: 120.25.207.44:8217 # 接收数据,当在 sentinel 的面板中配置一些参数,会通过该端口传送过来 port: 8720 feign: okhttp: # 开启 okhttp,性能最好 enabled: true sentinel: # 开启 feign 对 sentinel 的支持 enabled: true -
启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; // 开启 feign 的客户端 @EnableFeignClients // 开启 nacos 注册中心扫描,发现该服务 @EnableDiscoveryClient @SpringBootApplication public class SentinelConsumerApplication { public static void main(String[] args) { SpringApplication.run(SentinelConsumerApplication.class, args); } }
-
-
-
feign集成sentinel栗子-
提供者
sentinel-provider的ProviderControllerimport org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/provider") public class ProviderController { @GetMapping("/list/{param}") public String list(@PathVariable String param) { return "provider_list: " + param; } @GetMapping("/test") public String test(String param) { return "provider_test: " + param; } } -
消费者
sentinel-consumer-
远程调用类
ProviderServiceimport com.sheng.cloud.consumer.service.fallback.ProviderServiceFallBackImpl; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; /** * 说明:FeignClient(value = "sentinel-provider", path = "/provider", , fallback = ProviderServiceFallBackImpl.class) * value:提供者注册到注册中心的服务名,path:访问路径,fallback:熔断之后的服务降级类 * * @author sheng */ @Service @FeignClient(value = "sentinel-provider", path = "/provider", fallback = ProviderServiceFallBackImpl.class) public interface ProviderService { /** * 对应提供者的 public String list(@PathVariable String param){} * * @param param 测试参数 * @return 字符串 provider_list: param */ @GetMapping("/list/{param}") String list(@PathVariable String param); /** * 对应 提供者的 public String test(String param){} * * @param param 测试参数,在这里必须用@RequestParam修饰,默认在请求体中传递 * @return 字符串 provider_test: param */ @GetMapping("/test") String test(@RequestParam String param); } -
服务降级类
ProviderServiceFallBackImplimport com.sheng.cloud.consumer.service.ProviderService; import org.springframework.stereotype.Component; /** * 说明:必须注册到容器中,不然报错 * * @author sheng */ @Component public class ProviderServiceFallBackImpl implements ProviderService { @Override public String list(String param) { return "熔断了: fallBack_list: " + param; } @Override public String test(String param) { return "熔断了: fallBack_test: " + param; } } -
ConsumerControllerimport com.sheng.cloud.consumer.service.ProviderService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * 说明: * * @author sheng */ @RestController @RequestMapping("/consumer") public class ConsumerController { @Resource private ProviderService providerService; @GetMapping("/list/{param}") public String list(@PathVariable String param) { return providerService.list(param); } @GetMapping("/test") public String test(String param) { return providerService.test(param); } }
-
-
-
结果
-
正常运行
正常运行
正常运行
-
提供者
sentinel-provider项目停止运行后,走了熔断类提供者停止运行
提供者停止运行
-
3. 控制面板:官方文档
-
windows中启动sentineljava -Dserver.port=8217 -Dcsp.sentinel.dashboard.server=8217 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.7.2.jar-
sentinel默认端口为8080,改端口为8217
启动sentinel
-
浏览器访问
-
地址:
127.0.0.1:8217或localhost:8217访问
-
账号/密码:sentinel
控制面板
-
-
启动项目,实时监控
-
启动项目后
立刻刷新sentinel控制面板,并不会有监控,原因在于没有访问项目的资源启动项目
监控面板
-
访问资源,查看监控
访问资源
实时监控
-
-
控制面板的功能
-
实时监控- 用于查看接口调用的
时间、QPS(Query Per Second)、平均响应时间
实时监控
- 用于查看接口调用的
-
簇点链路- 查看当前追踪的所有的访问接口
- 可以给接口添加
流控规则、降级规则、热点规则、授权规则
簇点链路
-
流控规则新增流控规则
-
资源名- 需要流控的资源的名字
- 例如:
/consumer/list/{param}
-
针对来源- 默认为
default,表示所有 - 可以设置为特定的服务
- 默认为
-
阈值类型- 指对该资源如何进行限制
- 两种选择:
QPS或线程
-
单机阈值- 对应在
阈值类型的选择 - 指控制
QPS或线程的数量
- 对应在
-
流控模式-
直接:表示对指定资源进行限制 -
链路:当被关联的资源达到阈值的时候,指定资源将会被限制访问 -
链路:是更加细粒度的控制,控制指定资源对链路的限制
-
-
流控效果-
快速失败:当无法访问的时候立刻给用户一个错误响应 -
Warm Up:即预热,指经过指定的时间后才达到指定的阈值(初始的QPS从阈值 / 3开始,经过预热的时长逐渐提升到指定的QPS阈值) -
排队等待:指匀速的通过每秒指定的QPS,其他的请求进行排队,但是并不会一直排下去,超时就会失效。阈值类型必须为QPS
-
-
-
降级规则降级规则
-
资源名- 需要
降级的资源名 - 如
/consumer/list/{param}
- 需要
-
降级策略-
RT- 平均响应时间
- 如果
1秒钟之内进入的请求的平均响应时间大于设置的RT值(单位为毫秒),那么在指定的时间窗口内(降级时间间隔,单位为秒)的所有的请求都会熔断降级。
-
异常比例- 如果
1秒钟之内进入的请求数的异常比例大于指定的异常比例(数值区间为0.0 ~ 1.0),那么在指定的时间窗口内(降级时间间隔,单位为秒)的所有的请求都会熔断降级。
异常比例
- 如果
-
异常数- 如果
1秒钟之内,进入的请求数的异常数大于指定的异常数,那么在指定的时间窗口内(降级时间间隔,单位为秒)的所有的请求都会熔断降级。 - 注意:
时间窗口的值一般要大于 60,否则可能会一直处于熔断状态。
异常数
- 如果
-
-
-
热点规则-
针对
具体的请求参数进行设置// @SentinelResource必须要设置 @SentinelResource("modify") @RequestMapping("/modify") public String modify(@RequestParam(required = false) String id, @RequestParam(required = false) String status) { return "modify_" + id + "_" + password; }
热点规则
-
资源名:要限制方法@SentinelResource中设置的值 -
参数索引:具体对哪一个参数进行QPS限制,通过索引来指定(从0开始)。 -
单机阈值:指定在统计时长内的阈值 -
统计窗口时长:统计QPS的时长
-
-
系统规则系统规则
-
LOAD:仅对Linux/Unix-like机器生效,参考值一般是CPU cores * 2.5 -
RT:当单台机器上所有入口流量的平均 RT达到阈值即触发系统保护,单位是毫秒 -
线程数:当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护 -
入口 QPS:当单台机器上所有入口流量的QPS达到阈值即触发系统保护
-
-
授权规则- 指可以将
特定的访问应用加入黑名单或者白名单,但是必须在访问的时候携带应用的名称
授权规则
- 指可以将
-
集群流控集群流控
-
是否集群:是否采用集群 -
均摊阈值:每个集群节点每秒的QPS -
集群阈值模式:单机均摊是集群中每个节点每秒的QPS,总体阈值是整个集群每秒的QPS
-
-











网友评论