Commit 5e268211 by 李根

添加第二种方式实现FunInit接口,在初始化的时候从nacos获取配置

parent c011fa5b
...@@ -23,6 +23,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -23,6 +23,7 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class AopConfiguration { public class AopConfiguration {
// spring-colud-alibaba-sentinel 引入sentinel 不需要手动注入下面Bean 可以直接使用@SentinelResource
// @Bean // @Bean
// public SentinelResourceAspect sentinelResourceAspect() { // public SentinelResourceAspect sentinelResourceAspect() {
......
package com.alibaba.csp.sentinel.demo.springboot.nacos.config; package com.alibaba.csp.sentinel.demo.springboot.nacos.config;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.RuleType;
import com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
/** /**
* @author lnj * @author lnj
* createTime 2018-11-07 22:37 * createTime 2018-11-07 22:37
...@@ -28,39 +12,11 @@ import java.util.Properties; ...@@ -28,39 +12,11 @@ import java.util.Properties;
@Component @Component
public class ApplicationRunnerImpl implements ApplicationRunner { public class ApplicationRunnerImpl implements ApplicationRunner {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired @Autowired
SentinelProperties sentinelProperties; private NacosStartService nacosStartService;
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
logger.info("[NacosSource初始化,从Nacos中获取熔断规则]"); nacosStartService.initNacosConfigFromNacos();
sentinelProperties.getDatasource().entrySet().stream().filter(map -> {
return map.getValue().getNacos() != null;
}).forEach(map -> {
NacosDataSourceProperties nacos = map.getValue().getNacos();
Properties properties = new Properties() {{
setProperty("serverAddr", nacos.getServerAddr());
setProperty("namespace", "31b168cb-51ee-4b8b-adfa-29b42d4afa0b");
}};
if (Objects.equals(nacos.getRuleType(), RuleType.FLOW)) {
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(properties,
nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new com.alibaba.fastjson.TypeReference<List<FlowRule>>() {
}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
if (Objects.equals(nacos.getRuleType(), RuleType.DEGRADE)) {
ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new NacosDataSource<>(properties,
nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new com.alibaba.fastjson.TypeReference<List<DegradeRule>>() {
}));
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
}
});
} }
} }
\ No newline at end of file
package com.alibaba.csp.sentinel.demo.springboot.nacos.config;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.RuleType;
import com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
/**
* @author ligen
* @title: NacosStartService
* @projectName dashboardplus
* @description:
* @date 2020/4/214:08
*/
@Service
public class NacosStartService {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private SentinelProperties sentinelProperties;
public void initNacosConfigFromNacos() {
logger.info("[NacosSource初始化,从Nacos中获取熔断规则]");
sentinelProperties.getDatasource().entrySet().stream().filter(map -> {
return map.getValue().getNacos() != null;
}).forEach(map -> {
NacosDataSourceProperties nacos = map.getValue().getNacos();
Properties properties = new Properties() {{
setProperty("serverAddr", nacos.getServerAddr());
setProperty("namespace", "31b168cb-51ee-4b8b-adfa-29b42d4afa0b");
}};
if (Objects.equals(nacos.getRuleType(), RuleType.FLOW)) {
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(properties,
nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new com.alibaba.fastjson.TypeReference<List<FlowRule>>() {
}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
if (Objects.equals(nacos.getRuleType(), RuleType.DEGRADE)) {
ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new NacosDataSource<>(properties,
nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new com.alibaba.fastjson.TypeReference<List<DegradeRule>>() {
}));
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
}
});
}
}
package com.alibaba.csp.sentinel.demo.springboot.nacos.inti;
import com.alibaba.csp.sentinel.demo.springboot.nacos.config.NacosStartService;
import com.alibaba.csp.sentinel.init.InitFunc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author lnj
* createTime 2018-11-07 22:37
**/
@Component
public class DemoNacosInitFunc implements InitFunc {
@Autowired
private NacosStartService nacosStartService;
@Override
public void init() throws Exception {
nacosStartService.initNacosConfigFromNacos();
}
}
\ No newline at end of file
com.alibaba.csp.sentinel.demo.springboot.nacos.init.DemoNacosInitFunc
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment