Sentinel整合Gateway,并完成Nacos持久化

713 阅读1分钟

部署启动Sentinel

  • 如果是本地启动项目,则在本地部署启动Sentinel,对应教程: Windows安装Sentinel1.8

  • 如果是Centos服务器启动项目,则在对应的Centos服务器上部署启动Sentinel,对应教程: Centos安装Sentinel1.8

Gateway网关服务整合Sentinel

  1. 在Gateway网关服务中添加Sentinel相关依赖
    <!--sentinel依赖-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    
  2. 在配置文件中,添加Sentinel相关配置。
    spring:
      cloud:
        sentinel:
          enabled: true # sentinel开关
          eager: true
          transport:
            dashboard: 127.0.0.1:8858 # Sentinel控制台地址
            client-ip: 127.0.0.1
    
  3. 设置启动类表示服务为网关类型
    @EnableDiscoveryClient
    @SpringBootApplication
    public class PolarisGatewayApplication {
        public static void main(String[] args) {
            // 添加此项配置,让sentinel识别为网关服务
            System.setProperty("csp.sentinel.app.type", "1");
            SpringApplication.run(PolarisGatewayApplication.class, args);
        }
    }
    

查看整合情况

  1. 启动Gateway和其他项目服务
  2. 启动Sentinel
  3. 通过Gateway访问其他项目服务接口
  4. 访问Sentinel控制台,是否监控到刚才接口流量

image.png