04. `sentinel`的部署及简单使用

spring-cloud-alibaba-github

1. docker部署sentinel
  1. jdk安装包、sentinel的jar包上传到服务器

    sentinel-dashboard 官方下载地址

    上传jdk、sentinel

  2. 编辑dockerfile

    1. vim dockerfile

      编辑 dockerfile

    2. 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内容

  3. 构建镜像

    1. docker build -t zero/ubuntu-sentinel ./
    2. 说明
      1. -t:后面跟着镜像的名字,一般为作者的名称/基础镜像-镜像名-版本号
      2. ./:当前文件夹下的dockerfile,如果dockerfile在其他文件夹或者其他的名字,可以指定

    构建镜像

  4. 运行容器

    1. 查看镜像:docker images

      查看容器

    2. 运行容器

      # 运行容器
      docker run --name='zero-sentinel' -p 8217:8080 -d zero/ubuntu-sentinel
      
      # 查看容器
      docker ps -a
      
      1. --name='zero-sentinel'指定容器名为zero-sentinel
      2. -p 8217:8080指定宿主机的端口8217映射容器的端口8080sentinel的默认端口是8080
      3. -d后台运行
      4. zero/ubuntu-sentinel为镜像名

      运行容器

  5. 浏览器登录sentinel

    1. 地址:服务器的ip:运行容器时指定的端口

      登录sentinel

    2. 初始账号/密码sentinel

      账号/密码:sentinel

    3. 首页

      首页

  6. 可用docker-compose安装sentinel

    1. docker-compose.yml

      version: "3.7"
      services:
        zero-sentinel:
          build: ./
          restart: always
          ports:
            - 8718:8080  
      
    2. docker-compose up -d

      当前目录

      运行

      查看容器

      浏览器登录

  7. 远程服务器sentinel的使用很不稳定,有可能没有监控信息,但是可以使用熔断

    不能监控

2. feign集成sentinel

spring-cloud-alibaba-sentinel github 官方文档

  1. 核心依赖

    1. 依赖

      <dependencies>
          <dependency>
              <groupId>com.alibaba.cloud</groupId>
              <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
          </dependency>
      </dependencies>
      
    2. 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>    
      
  2. 配置文件

    1. 提供者sentinel-providerapplication.yml主配置文件及启动类

      1. application.yml

        server:
          # 项目访问端口号
          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
        
      2. 启动类

        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);
            }
        }
        
    2. 消费者sentinel-consumerapplication.yml主配置文件及启动类

      1. application.yml

        server:
          # 项目访问端口
          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
        
      2. 启动类

        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);
            }
        }
        
  3. feign集成sentinel栗子

    1. 提供者sentinel-providerProviderController

      import 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;
          }
      }
      
    2. 消费者sentinel-consumer

      1. 远程调用类ProviderService

        import 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);
        
        }
        
      2. 服务降级类ProviderServiceFallBackImpl

        官方文档

        import 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;
            }
        }
        
      3. ConsumerController

        import 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);
            }
        }
        
  4. 结果

    1. 正常运行

      正常运行

      正常运行

    2. 提供者sentinel-provider项目停止运行后,走了熔断类

      提供者停止运行

      提供者停止运行

  5. sentinel-demo 源码

3. 控制面板:官方文档
  1. windows中启动sentinel

    1. java -Dserver.port=8217 -Dcsp.sentinel.dashboard.server=8217 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.7.2.jar
    2. sentinel默认端口为8080,改端口为8217

    启动sentinel

  2. 浏览器访问

    1. 地址:127.0.0.1:8217localhost:8217

      访问

    2. 账号/密码sentinel

    控制面板

  3. 启动项目,实时监控

    1. 启动项目后立刻刷新sentinel控制面板,并不会有监控,原因在于没有访问项目的资源

      启动项目

      监控面板

    2. 访问资源,查看监控

      访问资源

      实时监控

  4. 控制面板的功能

    1. 实时监控

      1. 用于查看接口调用的时间QPS(Query Per Second)平均响应时间

      实时监控

    2. 簇点链路

      1. 查看当前追踪的所有的访问接口
      2. 可以给接口添加流控规则降级规则热点规则授权规则

      簇点链路

    3. 流控规则

      新增流控规则

      1. 资源名
        1. 需要流控的资源的名字
        2. 例如:/consumer/list/{param}
      2. 针对来源
        1. 默认为default,表示所有
        2. 可以设置为特定的服务
      3. 阈值类型
        1. 指对该资源如何进行限制
        2. 两种选择:QPS线程
      4. 单机阈值
        1. 对应在阈值类型的选择
        2. 指控制QPS线程的数量
      5. 流控模式
        1. 直接:表示对指定资源进行限制
        2. 链路:当被关联的资源达到阈值的时候,指定资源将会被限制访问
        3. 链路:是更加细粒度的控制,控制指定资源对链路的限制
      6. 流控效果
        1. 快速失败:当无法访问的时候立刻给用户一个错误响应
        2. Warm Up:即预热,指经过指定的时间后才达到指定的阈值(初始的QPS阈值 / 3开始,经过预热的时长逐渐提升到指定的QPS阈值)
        3. 排队等待:指匀速的通过每秒指定的QPS,其他的请求进行排队,但是并不会一直排下去,超时就会失效。阈值类型必须为QPS
    4. 降级规则

      降级规则

      1. 资源名
        1. 需要降级的资源名
        2. /consumer/list/{param}
      2. 降级策略
        1. RT

          1. 平均响应时间
          2. 如果1秒钟之内进入的请求的平均响应时间大于设置的RT值(单位为毫秒),那么在指定的时间窗口内(降级时间间隔,单位为)的所有的请求都会熔断降级。
        2. 异常比例

          1. 如果1秒钟之内进入的请求数异常比例大于指定的异常比例(数值区间为 0.0 ~ 1.0),那么在指定的时间窗口内(降级时间间隔,单位为)的所有的请求都会熔断降级。

          异常比例

        3. 异常数

          1. 如果1秒钟之内,进入的请求数异常数大于指定的异常数,那么在指定的时间窗口内(降级时间间隔,单位为)的所有的请求都会熔断降级。
          2. 注意:时间窗口的值一般要 大于 60,否则可能会一直处于熔断状态。

          异常数

    5. 热点规则

      1. 针对具体的请求参数进行设置

        // @SentinelResource必须要设置
        @SentinelResource("modify")
        @RequestMapping("/modify")
        public String modify(@RequestParam(required = false) String id,
        				     @RequestParam(required = false) String status) {
        	return "modify_" + id + "_" + password;
        }
        

      热点规则

      1. 资源名:要限制方法@SentinelResource中设置的值
      2. 参数索引:具体对哪一个参数进行QPS限制,通过索引来指定(从0开始)。
      3. 单机阈值:指定在统计时长内的阈值
      4. 统计窗口时长:统计QPS的时长
    6. 系统规则

      系统规则

      1. LOAD:仅对Linux/Unix-like 机器生效,参考值一般是 CPU cores * 2.5
      2. RT:当单台机器上所有入口流量的平均 RT达到阈值即触发系统保护,单位是毫秒
      3. 线程数:当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护
      4. 入口 QPS:当单台机器上所有入口流量的QPS达到阈值即触发系统保护
    7. 授权规则

      1. 指可以将特定的访问应用加入黑名单或者白名单,但是必须在访问的时候携带应用的名称

      授权规则

    8. 集群流控

      集群流控

      1. 是否集群:是否采用集群
      2. 均摊阈值每个集群节点每秒QPS
      3. 集群阈值模式单机均摊是集群中每个节点每秒QPS, 总体阈值整个集群每秒的QPS
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值