Sentinel控制台改造:规则同步到Nacos

Sentinel生产环境改造控制台使规则同步到Nacos

  • 首先下载Sentinel控制台的源码
  • 修改pom.xml文件
  • 修改sidebar.html
  • 创建Nacos配置类
  • 拉取Nacos配置
  • 推送配置给Nacos
  • 修改注入的Bean
  • 结束语

官方给出的文档:在生产环境中使用-Sentinel
我们要实现push 模式,推送规则为Sentinel 控制台 → Nacos配置中心 → Sentinel 数据源 → Sentinel
下面以限流规则为例改造控制台

首先下载Sentinel控制台的源码

Sentinel源码
然后打开源码中的sentinel-dashboard项目

修改pom.xml文件

 
        
            com.alibaba.csp
            sentinel-datasource-nacos
        

修改sidebar.html

找到sentinel-dashboard\src\main\webapp\resources\app\scripts\directives\sidebar中的sidebar.html

  •   流控规则
  • 修改为:

  •   流控规则
  • 创建Nacos配置类

    com.alibaba.csp.sentinel.dashboard.rule包下新建一个nacos包
    在nacos包下创建下面的类:

    import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
    import com.alibaba.csp.sentinel.datasource.Converter;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.nacos.api.PropertyKeyConst;
    import com.alibaba.nacos.api.config.ConfigFactory;
    import com.alibaba.nacos.api.config.ConfigService;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.List;
    import java.util.Properties;
    
    /**
     * @author dingshh
     * @since 1.7.1
     */
    @Configuration
    public class NacosConfig {
    
        @Bean
        public Converter, String> flowRuleEntityEncoder() {
            return JSON::toJSONString;
        }
    
        @Bean
        public Converter> flowRuleEntityDecoder() {
            return s -> JSON.parseArray(s, FlowRuleEntity.class);
        }
    
        @Bean
        public ConfigService nacosConfigService() throws Exception {
            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR, "NacosIp:端口号");
    //        properties.put(PropertyKeyConst.NAMESPACE, "xxx");
            return ConfigFactory.createConfigService(properties);
        }
    }
    
    

    其中NacosIp:端口号对应Nacos配置中心的Ip地址和端口号
    如果用到了namespace隔离环境,可以在nacosConfigService方法中再加入配置properties.put(PropertyKeyConst.NAMESPACE, “xxx”);

    拉取Nacos配置

    在nacos包下创建下面的类:

    import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
    import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
    import com.alibaba.csp.sentinel.datasource.Converter;
    import com.alibaba.csp.sentinel.util.StringUtil;
    import com.alibaba.nacos.api.config.ConfigService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author dingshh
     * @since 1.7.1
     */
    @Component("flowRuleNacosProvider")
    public class FlowRuleNacosProvider implements DynamicRuleProvider> {
    
        @Autowired
        private ConfigService configService;
        @Autowired
        private Converter> converter;
    
        public static final String FLOW_DATA_ID_POSTFIX = "sentinel";
        public static final String GROUP_ID = "DEFAULT_GROUP";
    
        @Override
        public List getRules(String appName) throws Exception {
            String rules = configService.getConfig(FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000);
            if (StringUtil.isEmpty(rules)) {
                return new ArrayList<>();
            }
            return converter.convert(rules);
        }
    }
    
    

    推送配置给Nacos

    在nacos包下创建下面的类:

    import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
    import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
    import com.alibaba.csp.sentinel.datasource.Converter;
    import com.alibaba.csp.sentinel.util.AssertUtil;
    import com.alibaba.nacos.api.config.ConfigService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    /**
     * @author dingshh
     * @since 1.7.1
     */
    @Component("flowRuleNacosPublisher")
    public class FlowRuleNacosPublisher implements DynamicRulePublisher> {
    
        @Autowired
        private ConfigService configService;
        @Autowired
        private Converter, String> converter;
    
        public static final String FLOW_DATA_ID_POSTFIX = "sentinel";
        public static final String GROUP_ID = "DEFAULT_GROUP";
    
        @Override
        public void publish(String app, List rules) throws Exception {
            AssertUtil.notEmpty(app, "app name cannot be empty");
            if (rules == null) {
                return;
            }
            configService.publishConfig(FLOW_DATA_ID_POSTFIX, GROUP_ID, converter.convert(rules));
        }
    }
    
    

    修改注入的Bean

    修改`com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2

    	@Autowired
        @Qualifier("flowRuleNacosProvider")
        private DynamicRuleProvider> ruleProvider;
        @Autowired
        @Qualifier("flowRuleNacosPublisher")
        private DynamicRulePublisher> rulePublisher;
    
    

    结束语

    以上仅仅是修改了限流规则与Nacos配置中心的同步。
    以下是我的代码地址,完整代码中实现了正常模式和网关模式的所有规则同步。
    github地址

    你可能感兴趣的:(Java)