【Sentinel系列】Sentinel Dashboard 整合Nacos 实现 流控规则 双向同步 (二)

这是我参与11月更文挑战的第25天,活动详情查看:2021最后一次更文挑战

  • 修改resources/app/scripts/directives/sidebar/sidebar.html文件部分代码,把dashboard.flowV1改成dashboard.flow。
<li ui-sref-active="active" ng-if="entry.isGateway">
            <a ui-sref="dashboard.gatewayFlow({app: entry.app})">
                <!--<a ui-sref="dashboard.gatewayFlowV1({app: entry.app})">-->
              <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则</a>
          </li>
复制代码

修改后会调佣FlowControllerV2的接口。

  • 在com.alibaba.csp.sentinel.dashboard.rule包中创建一个Nacos包,并创建一个类用加载外部化配置。
@Data
@ConfiguartionProperties(prefix="sentinel.nacos")
public class NacosPropertiesConfiguration{
	private String serverAddr;
	private String groupId = "DEFAULT_GROUP";
	private String namespace;
	private String dataId;
}
复制代码
  • 创建一个Nacos配置类NacosConfiguration
@EnableConfigurationProperties(NacosPropertiesConfiguration.class)
@Configuration
public class NacosConfiguration{
	@Bean
    public Converter<List<FlowRuleEntity>,String> flowRuleEntityEncoder(){
        retur JSON::toJSONString;
    } 
    @Bean
    public Converter<String,List<FlowRuleEntity>> flowRuleDecoder(){
        return s->JSON.parseArray(s,FlowRuleEntity.class);
    }
    @Bean
    public ConfigService nacosCOnfigService(NacosPropertiesConfiguartion nacosPropertiesConfiguration) throws NacosException{
       Properties properties = new Properties();
       properties.put(PropertyKeyConst.NAMESPACE,nacosPropertiesConfiguration.getNamespace());
        properties.put(PropertyKeyConst.SERVER_ADDR,nacosPropertiesConfiguration.getServerAddr());
        return ConfigFactory.createConfigService(properties);
    }
}
复制代码

注入Controller转换器,把FlowRuleEntity转化为FlowRule,并且反向转化 注入Nacos配置服务ConfigService

  • 创建一个常量类NacosConstants,分别表示默认的data_id和group_id的后缀。
public class NacosConstants{
 	public static final String DATA_ID_POSTFIX = "-sentinel-flow";
    public static final String GROUP_ID  =  "DEFAULT_GROUP";
}
复制代码
  • 实现动态从Nacos配置中心获取流控规则
@Service
@Slf4j
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>>
{
	
    @Autowired
    private NacosPropertiesConfiguration nacosPropertiesConfiguration;
    @Autowired
    private Converter<String,List<FlowRuleEntity>> converter;
    
    @Override
    public List<FlowRuleEntity> getRules(String appName) throw Exception{
        String dataId=new StringBuilder(appName).append(NacosConstants.DATA_ID_POSTFIX).toString();
    String rules=ConfigService.getConfig(dataId,nacosPropertiesConfiguration.getGroupId(),3000);
        log.info("从Nacos配置中心推送流控规则:{}",rules);
        if(StringUtils.isEmpty(rules)){
            return new ArrayList<>();
        }
        return converter.convert(rules);
    }
}
复制代码

通过ConfigService.getConfig方法从Nacos Config服务端中读取指定配置信息,并且通过converter转化为FlowRule规则。

  • 创建一个流控规则发布类,在Sentinel Dashboard 上修改配置后,需要调用发布方法将数据持久化到Nacos配置中心上。
@Service
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>>{
	 @Autowired
    private NacosPropertiesConfiguration nacosPropertiesConfiguration;
    @Autowired
    private Converter<String,List<FlowRuleEntity>> converter;
    
    @Override
    public void publish(String appName,List<FlowRuleEntity> rules) throws Exception{
        AssertUtil.notEmpty(appName,"appName 不能为空");
        if(rules == null){
            return;
        }
        String dataId = new StringBuilder(appName).append(nacosPropertiesConfiguration.DATA_ID_POSTFIX).toString();
    configService.publishConfig(dataId,nacosPropertiesConfiguration.getGroupId(),converter.convert(rules));
    }
}
复制代码

猜你喜欢

转载自juejin.im/post/7034522149268226055