Spring Cloud Alibaba-04-Sentinel规则持久化Nacos方式-推荐

Lison , v1.0.0, 2023.10.03

Spring Cloud Alibaba-04-Sentinel规则持久化全面Nacos方式

文章目录

  • Spring Cloud Alibaba-04-Sentinel规则持久化全面Nacos方式
    • Sentinel-Dashboard 添加规则Nacos双向绑定
    • 实现
      • 注释掉test
      • 增加 NacosConfig配置
      • 加入Repository配置
      • 注入Repository
      • 最后替换掉对应Controller中对应的repository,
      • 添加参数配置

Sentinel-Dashboard 添加规则Nacos双向绑定

官网文档地址:https://sentinelguard.io/zh-cn/docs/quick-start.html

思路
从动态规则扩展章节得知,可以通过NacosDataSource作为配置数据源
从Sentinel-Dashboard代码中可以得知,dashboard通过http请求推送配置变更

publishRules(entity.getApp(), entity.getIp(), entity.getPort()).get(5000, TimeUnit.MILLISECONDS);

   List<FlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
        return sentinelApiClient.setFlowRuleOfMachineAsync(app, ip, port, rules);

所以,只需要在dashboard推送变更后,将配置保存到nacos中且搭配NacosDataSource扩展即可实现配置持久化

从dashboard各个规则的Controller中可以发现,每个Controller都有类似repository的注入

    @Autowired
    private RuleRepository<DegradeRuleEntity, Long> repository;

负责规则的增删改查,默认提供的都是内存控制,例如InMemDegradeRuleStore ,每个内存控制类都继承同一个抽象类

public class InMemDegradeRuleStore extends InMemoryRuleRepositoryAdapter<DegradeRuleEntity>

只要在respository写操作后添加配置推送的nacos的操作

实现

注释掉test

        <dependency>
            <groupId>com.alibaba.cspgroupId>
            <artifactId>sentinel-datasource-nacosartifactId>

        dependency>

增加 NacosConfig配置

package com.alibaba.csp.sentinel.dashboard.config;


import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.Properties;

/**
 * @className: com.alibaba.csp.sentinel.dashboard.config-> NacosConfig
 * @description:
 * @author: Lison
 * @createDate: 2023-10-03
 */
@Configuration
@EnableConfigurationProperties(NacosConfig.NacosProperties.class)
public class NacosConfig {
   

    @ConfigurationProperties(prefix = "nacos.config")
    public static class NacosProperties{
   
        private String group = "DEFAULT_GROUP";
        private String namespace;
        private String authDataId = "sentinel-dashboard-auth";

        private String username;


        private String password;
        private Long timeout = 3000L;

        private String serverAddr;

        public String getGroup() {
   
            return group;
        }

        public void setGroup(String group) {
   
            this.group = group;
        }

        public String getNamespace() {
   
            return namespace;
        }

        public void setNamespace(String namespace) {
   
            this.namespace = namespace;
        }

        public Long getTimeout() {
   
            

你可能感兴趣的:(#,Spring,Cloud,Alibaba,java,架构,sentinel)