美文网首页
Spring Cloud Alibaba系列之-Sentinel

Spring Cloud Alibaba系列之-Sentinel

作者: AC编程 | 来源:发表于2021-05-14 10:38 被阅读0次

一、SpringCloud Alibaba Sentinel

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

Sentinel 的主要特性:


Sentinel 的主要特性

Sentinel 分为两个部分:

  • 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。

  • 控制台(Dashboard)基于Spring Boot开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

SpringCloud Alibaba Sentinel官网使用文档
SpringCloud Alibaba Sentinel Github

二、安装Sentinel控制台

2.1 下载Sentinel

Sentinel下载地址

Sentinel下载
2.2 启动Sentinel
2.2.1 通过命令启动:
java -jar sentinel-dashboard-1.8.1.jar

如果在Linux环境下,想在后台运行,在命令后面加上 & 即可,如下

java -jar sentinel-dashboard-1.8.1.jar &

默认情况下,sentinel-dashboard以8080端口启动
注意: Sentinel 仅支持 JDK 1.8 或者以上版本。

2.2.2 访问Sentinel管理界面,在浏览器中输入访问地址http://127.0.0.1:8080
Sentinel登录页面

从 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登录功能,默认用户名和密码都是 sentinel。

2.2.3 控制台页面
控制台页面
2.2.4 配置启动端口、用户名、密码

对于启动端口、用户登录的相关配置可以在启动命令中增加下面的参数来进行配置:

 -Dserver.port=8888 指定启动端口为 8888;

-Dsentinel.dashboard.auth.username=sentinel 指定控制台的登录用户名为 sentinel;

-Dsentinel.dashboard.auth.password=admin 指定控制台的登录密码为admin ;如果省略这两个参数,默认用户和密码均为 sentinel;

-Dserver.servlet.session.timeout=7200 指定 Spring Boot 服务端 session 的过期时间,如 7200 表示 7200 秒;60m 表示 60 分钟,默认为 30 分钟;

启动命令

java -Dserver.port=8888 -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=admin -Dserver.servlet.session.timeout=7200 -jar sentinel-dashboard-1.8.1.jar

注意:如果这里是使用powershell执行命令会报错误

PS D:\install> java -Dserver.port=8888 -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=admin -Dserver.servlet.session.timeout=7200 -jar sentinel-dashboard-1.8.1.jar
错误: 找不到或无法加载主类 .port=8888

应该修改为

java '-Dserver.port=8888' '-Dsentinel.dashboard.auth.username=sentinel' '-Dsentinel.dashboard.auth.password=admin' '-Dserver.servlet.session.timeout=7200' -jar sentinel-dashboard-1.8.1.jar

三、module[order-service]接入Sentinel

3.1 父级工程maven配置

在父级工程[ac-mall-cloud] pom.xml中引入Spring Cloud Alibaba Sentinel依赖

<dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
      <version>${alibaba.cloud.version}</version>
</dependency>

[ac-mall-cloud] 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>ac-mall-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>user-service</module>
        <module>product-service</module>
        <module>order-service</module>
        <module>gateway-service</module>
        <module>auth-service</module>
    </modules>

    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        <mysql.version>8.0.17</mysql.version>
        <mybatis.plus.version>3.2.0</mybatis.plus.version>
        <druid.version>1.1.10</druid.version>
        <boot.version>2.2.4.RELEASE</boot.version>
        <alibaba.cloud.version>2.1.0.RELEASE</alibaba.cloud.version>
        <lombok.version>1.18.10</lombok.version>
    </properties>

    <!-- 管理子类所有的jar包的版本,这样的目的是方便去统一升级和维护 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>${alibaba.cloud.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
                <version>${alibaba.cloud.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
                <version>${alibaba.cloud.version}</version>
            </dependency>

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis.plus.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <scope>provided</scope>
            </dependency>

        </dependencies>

    </dependencyManagement>

    <!-- 所有的子工程都会自动加入下面的依赖  -->
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <!-- SpringBoot 工程编译打包的插件,放在父pom中就直接给所有子工程继承 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
3.2 module[order-service]maven配置

在[order-service] pom.xml中引入Spring Cloud Alibaba Sentinel依赖

<dependency>
     <groupId>com.alibaba.cloud</groupId>
     <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

[order-service] 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>ac-mall-cloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.ac</groupId>
    <artifactId>order-service</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

    </dependencies>

</project>
3.3 [order-service] 配置Sentinel地址

在 [order-service] application.yml 配置文件中添加Sentinel地址

spring:
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8888

[order-service] application.yml 完整配置如下

server:
  port: 8020

spring:
  application:
    name: order-service

  cloud:
    nacos:
      discovery:
        server-addr: 47.105.146.74:8848

    sentinel:
      transport:
        dashboard: 127.0.0.1:8888

四、限流测试

4.1 启动微服务访问下单接口

依次启动[user-service]、[order-service]、[auth-service]、[gateway-service]服务,通过[gateway-service]访问[order-service]的下单接口

访问下单接口
4.2 Sentinel 控制台显示下单接口的信息

此时,Sentinel 控制台页面就显示下单接口的信息了,如下图所示

下单接口监控信息
4.3 配置限流规则

1、点击簇点链路菜单,如下图:

簇点链路菜单

2、通过点击流控按钮,来为该接口设置限流规则,如下图所示

image.png

这里做一个最简单的配置:
阈值类型选择:QPS
单机阈值:1
综合起来的配置效果就是,该接口的限流策略是每秒最多允许1个请求进入。

3、流控规则的界面,这里可以看到当前设置的所有限流策略

image.png
4.4 验证限流规则

在完成了上面所有内容之后,我们可以尝试一下快速的调用这个接口,看看是否会触发限流控制,可以看到,快速的调用两次/order接口之后,第二次调用被限流了,如下图所示

image.png

五、动态规则配置持久化

Dashboard中设置的限流规则在应用重启之后就丢失了,因此我们需要对限流规则进行持久化。Sentinel自身就支持了多种不同的数据源来持久化规则配置,目前包括以下几种方式:

  • 文件配置
  • Nacos配置
  • ZooKeeper配置
  • Apollo配置

六、参考文章

Spring Cloud 从入门到精通
Spring Cloud Alibaba学习总结

七、附录

项目源码地址

相关文章

网友评论

      本文标题:Spring Cloud Alibaba系列之-Sentinel

      本文链接:https://www.haomeiwen.com/subject/daxqjltx.html