1、介绍:
Sentinel的官方标题是:分布式系统的流量防卫兵。用来作为服务稳定性保障的。
对于服务稳定性保障组件,Spring Cloud中有一个组件Hystrix,但是目前Netflix已经宣布对Hystrix停止更新。

Sentinel与Hystrix的区别见:https://yq.aliyun.com/articles/633786/

2、windows下启动sentinel
sentinel的jar包下载地址:https://github.com/alibaba/Sentinel/releases
这里选择的是1.8.0版本:https://github.com/alibaba/Sentinel/releases/download/v1.8.0/sentinel-dashboard-1.8.0.jar
在这里插入图片描述

下载好jar包之后,windows环境下可以通过cmd命令启动sentinel,到jar包所在路径下输入命令:

java -jar -Dserver.port=8686 sentinel-dashboard-1.8.0.jar

通过-Dserver.port=xxxx,可以指定运行端口,如果不指定直接使用命令Java -jar sentinel-dashboard-1.8.0.jar,默认的端口是8080,如果端口已经被占用了就不能启动成功
在这里插入图片描述
启动sentinel成功之后,可以访问sentine界面
账号:sentinel
密码:sentinel
路径:127.0.0.1:8686
登录成功之后:
在这里插入图片描述
springboot项目里整合sentinel:
本地项目环境:jdk1.8,springboot: 2.3.1.RELEASE
(该项目已经整合过nacos)
先在pom文件里添加相关依赖:

        <!-- 使用nacos作为注册中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <!-- sentinel -->

        <!--sentinel指定nacos为数据源 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>1.5.2</version>
        </dependency>

        <!-- 引用sentinel配置 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.7.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>2.0.1</version>
        </dependency>

配置yml文件:
port: 8719 如果端口被占用 就从8719+1开始 一直找到那个端口没被占用为止

server:
  port: 8989
  servlet:
    context-path: /provider-demo


# Tomcat specifics
tomcat:
  uri-encoding: UTF-8

# application.yml文件中添加如下配置:
spring:
  application:
    name: ProviderDemo   # 服务名称 应用名称
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848   # 配置注册中心的地址
    sentinel:
      transport:
        dashboard: localhost:8686 # 配置sentinel dashboard地址
        port: 8719
 # 暴露的健康检查服务断点
management:
  endpoint:
    web:
      exposure:
        include: '*'

测试类写个方法就可以
Sentinel是 懒加载机制所以呢 需要访问一下接口即可
再去访问Sentinel 就有数据了
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

sentinel流控监测

可能出现的错误信息:
1、新增流控规则时,显示sentinel 添加流控 失败:invalid type
在这里插入图片描述

错误原因:可能是版本问题,这里是把spring-cloud-starter-alibaba-sentinel版本调整了一下,由:2.2.0.RELEASE改为2.2.1.RELEASE

      <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
          <version>2.2.1.RELEASE</version>
      </dependency>
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐