sentinel配置持久化

sentinel配置持久化到nacos

本次针对网关进行限流
本次版本
<java.version>1.8</java.version>
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.4.RELEASE</spring-cloud-alibaba.version>

gateway pom依赖


<!-- SpringCloud Ailibaba Sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<!-- SpringCloud Ailibaba Sentinel Gateway -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

<!-- Sentinel Datasource Nacos -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

<!-- SpringBoot Actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

gateway配置

注意要在每个微服务的启动参数添加此服务和dashboard交互的ip,端口可以不写,冲突会自动+1,注意ip不要写127.0.0.1,要写实际可以交互的ip地址,端口要开防火墙
spring.cloud.sentinel.transport.clientIp: 172.31.237.102

spring:
  cloud:
    sentinel:
      enabled: false
      # 取消控制台懒加载
      eager: true
      filter:
        enabled: false
      web-context-unify: false
      transport:
        # 控制台地址
        dashboard: 172.31.237.100:8080
      # nacos配置持久化
      datasource:
        flow:
          nacos:
            namespace: sentinel
            server-addr: ${spring.cloud.nacos.config.server-addr}
            dataId: ${spring.application.name}-flow-rules
            username: ${spring.cloud.nacos.username}
            password: ${spring.cloud.nacos.password}
            groupId: SENTINEL_GROUP
            # 规则类型,取值见:
            # com.alibaba.cloud.sentinel.datasource.RuleType
            rule-type: flow
            data-type: json
        gw-flow:
          nacos:
            namespace: sentinel
            server-addr: ${spring.cloud.nacos.config.server-addr}
            dataId: ${spring.application.name}-gw-flow-rules
            username: ${spring.cloud.nacos.username}
            password: ${spring.cloud.nacos.password}
            groupId: SENTINEL_GROUP
            rule-type: gw-flow
            data-type: json
        gw-api-group:
          nacos:
            namespace: sentinel
            server-addr: ${spring.cloud.nacos.config.server-addr}
            dataId: ${spring.application.name}-gateway-api-group-rules
            username: ${spring.cloud.nacos.username}
            password: ${spring.cloud.nacos.password}
            groupId: SENTINEL_GROUP
            rule-type: param-flow
            data-type: json
        degrade:
          nacos:
            namespace: sentinel
            server-addr: ${spring.cloud.nacos.config.server-addr}
            dataId: ${spring.application.name}-degrade-rules
            username: ${spring.cloud.nacos.username}
            password: ${spring.cloud.nacos.password}
            groupId: SENTINEL_GROUP
            rule-type: degrade
            data-type: json
        system:
          nacos:
            namespace: sentinel
            server-addr: ${spring.cloud.nacos.config.server-addr}
            dataId: ${spring.application.name}-system-rules
            username: ${spring.cloud.nacos.username}
            password: ${spring.cloud.nacos.password}
            groupId: SENTINEL_GROUP
            rule-type: system
            data-type: json
        authority:
          nacos:
            namespace: sentinel
            server-addr: ${spring.cloud.nacos.config.server-addr}
            dataId: ${spring.application.name}-authority-rules
            username: ${spring.cloud.nacos.username}
            password: ${spring.cloud.nacos.password}
            groupId: SENTINEL_GROUP
            rule-type: authority
            data-type: json
        param-flow:
          nacos:
            namespace: sentinel
            server-addr: ${spring.cloud.nacos.config.server-addr}
            dataId: ${spring.application.name}-param-flow-rules
            username: ${spring.cloud.nacos.username}
            password: ${spring.cloud.nacos.password}
            groupId: SENTINEL_GROUP
            rule-type: param-flow
            data-type: json

dashboard配置持久化到nacos

为了可以在dashboard中统一配置

  • 官方发布的dashboard是在内存中处理配置,服务重启后配置消失,所以需要改造源码把dashboard的配置写入nacos
  • 这里提供我改在好的dashboard源码

dashboard启动

# kill进程 
PID=`ps -ef | grep "sentinel-dashboard.jar" | grep -v "grep" | awk '{print $2}'`
if [ -n "${PID}" ]
then
    kill -9 ${PID}
fi

nohup java -jar /root/sentinel-dashboard.jar \
-Xmx1g -Xms1g \
--server.port=8080 \
--sentinel.datasource.nacos.server-addr=172.31.237.101:80 \
--sentinel.datasource.nacos.namespace=sentinel \
--sentinel.datasource.nacos.username= \
--sentinel.datasource.nacos.password= \
>/dev/null 2>&1 &

posted @ 2022-12-19 17:42  rm-rf*  阅读(105)  评论(0编辑  收藏  举报