第四步:创建Nacos的配置类,具体代码如下:
@Configuration
public class NacosConfig {
@Bean
public Converter<List, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List> flowRuleEntityDecoder() {
return s -> JSON.parseArray(s, FlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, “localhost”);
return ConfigFactory.createConfigService(properties);
}
}
如果用到了namespace隔离环境,可以在nacosConfigService
方法中再加入配置,比如:properties.put(PropertyKeyConst.NAMESPACE, "130e71fa-97fe-467d-ad77-967456f2c16d");
第五步:实现Nacos的配置拉取。
@Component(“flowRuleNacosProvider”)
public class FlowRuleNacosProvider implements DynamicRuleProvider<List> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List> converter;
public static final S