Sentinel推送配置到nacos*
-
- Sentinel推送配置到nacos
- sentinel是如何加載到目标服務的ip和端口?
Sentinel推送配置到nacos
一、首先下載下傳sentinel的源碼
https://github.com/alibaba/Sentinel
二、修改sentinel的jar包 注釋scope ,不然無法在主程式中使用
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!--<scope>test</scope>-->
</dependency>
三、修改sentinel的sidebar.html頁面
将dashboard.flowV1修改成dashboard.flow
flowV1表示指向FlowControllerV1控制器,這個控制器目前是沒有動态規則的相關代碼的
修改成dashboard.flow後會指向FlowControllerV2控制器
四、在com.alibaba.csp.sentinel.dashboard.rule下增加一個nacos包
五、添加NacosConfig的配置類
@Configuration
public class NacosConfig {
@Bean
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
return s -> JSON.parseArray(s, FlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
//定義IP和端口,如果不指定端口則預設8848
properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1");
return ConfigFactory.createConfigService(properties);
}
}
六、添加FlowRuleNacosPublisher和 FlowRuleNacosProvider兩個類
@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<FlowRuleEntity>> converter;
public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
public static final String GROUP_ID = "DEFAULT_GROUP";
@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
String rules = configService.getConfig(appName + FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
}
@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<List<FlowRuleEntity>, 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<FlowRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
configService.publishConfig(app + FLOW_DATA_ID_POSTFIX, GROUP_ID, converter.convert(rules));
}
}
七、修改FlowControllerV2
聲明nacos的動态實作類
推送配置
publishConfig會從ServerListManager擷取nacos的調用位址和端口通過httpPost發送到nacos的服務位址。
以上代碼都和src/test/java中一樣。
**
sentinel是如何加載到目标服務的ip和端口?
ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFactory)
Spring的ConfigurationClassPostProcessor這個類會把有@Configuration的類都放在beanFactory的這個變量中,比如前面的NacosConfig類,在啟動的時候就會被Spring加載
接下來的啟動過程中Spring會調用nacosConfigService這個方法 ,在方法裡面聲明目标服務的相關配置
這裡可以根據需要在去指定其他不同的配置。
接着ConfigFactory建立ConfigService
調用NacosConfigService的構造方法
agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
建立ServerListManager 這個類是用來存放及解析目标服務的IP相關的處理
ServerListManager 的構造方法,解析nacosConfig傳入的屬性,對IP的和端口的解析。
如果SERVER_ADDR沒有指定端口則調用ParamUtil.getDefaultServerPort擷取預設的端口。
publishConfig裡面最後會調用ServerHttpAgent.httpPost()方法将配置推送到nacos
在getCurrentServerAddr中擷取目标服務的IP和端口等的相關配置
**