Java源码示例:com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource
示例1
private void listenRules() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
String flowRulePath = URLDecoder.decode(classLoader.getResource("FlowRule.json").getFile(), "UTF-8");
String degradeRulePath = URLDecoder.decode(classLoader.getResource("DegradeRule.json").getFile(), "UTF-8");
String systemRulePath = URLDecoder.decode(classLoader.getResource("SystemRule.json").getFile(), "UTF-8");
// Data source for FlowRule
FileRefreshableDataSource<List<FlowRule>> flowRuleDataSource = new FileRefreshableDataSource<>(
flowRulePath, flowRuleListParser);
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
// Data source for DegradeRule
FileRefreshableDataSource<List<DegradeRule>> degradeRuleDataSource
= new FileRefreshableDataSource<>(
degradeRulePath, degradeRuleListParser);
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
// Data source for SystemRule
FileRefreshableDataSource<List<SystemRule>> systemRuleDataSource
= new FileRefreshableDataSource<>(
systemRulePath, systemRuleListParser);
SystemRuleManager.register2Property(systemRuleDataSource.getProperty());
}
示例2
@Override
public void init() throws Exception {
// A fake path.
String flowRuleDir = System.getProperty("user.home") + "/sentinel/rules";
String flowRuleFile = "flowRule.json";
String flowRulePath = flowRuleDir + "/" + flowRuleFile;
ReadableDataSource<String, List<FlowRule>> ds = new FileRefreshableDataSource<>(
flowRulePath, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})
);
// Register to flow rule manager.
FlowRuleManager.register2Property(ds.getProperty());
WritableDataSource<List<FlowRule>> wds = new FileWritableDataSource<>(flowRulePath, this::encodeJson);
// Register to writable data source registry so that rules can be updated to file
// when there are rules pushed from the Sentinel Dashboard.
WritableDataSourceRegistry.registerFlowDataSource(wds);
}
示例3
private void listenRules() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
String flowRulePath = URLDecoder.decode(classLoader.getResource("FlowRule.json").getFile(), "UTF-8");
String degradeRulePath = URLDecoder.decode(classLoader.getResource("DegradeRule.json").getFile(), "UTF-8");
String systemRulePath = URLDecoder.decode(classLoader.getResource("SystemRule.json").getFile(), "UTF-8");
// Data source for FlowRule
FileRefreshableDataSource<List<FlowRule>> flowRuleDataSource = new FileRefreshableDataSource<>(
flowRulePath, flowRuleListParser);
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
// Data source for DegradeRule
FileRefreshableDataSource<List<DegradeRule>> degradeRuleDataSource
= new FileRefreshableDataSource<>(
degradeRulePath, degradeRuleListParser);
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
// Data source for SystemRule
FileRefreshableDataSource<List<SystemRule>> systemRuleDataSource
= new FileRefreshableDataSource<>(
systemRulePath, systemRuleListParser);
SystemRuleManager.register2Property(systemRuleDataSource.getProperty());
}
示例4
@Override
public void init() throws Exception {
// A fake path.
String flowRuleDir = System.getProperty("user.home") + File.separator + "sentinel" + File.separator + "rules";
String flowRuleFile = "flowRule.json";
String flowRulePath = flowRuleDir + File.separator + flowRuleFile;
ReadableDataSource<String, List<FlowRule>> ds = new FileRefreshableDataSource<>(
flowRulePath, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})
);
// Register to flow rule manager.
FlowRuleManager.register2Property(ds.getProperty());
WritableDataSource<List<FlowRule>> wds = new FileWritableDataSource<>(flowRulePath, this::encodeJson);
// Register to writable data source registry so that rules can be updated to file
// when there are rules pushed from the Sentinel Dashboard.
WritableDataSourceRegistry.registerFlowDataSource(wds);
}
示例5
@Test
public void testFile() throws Exception {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(
TestConfig.class);
assertThat(annotationConfigApplicationContext.getBean("fileBean")).isNotNull();
FileRefreshableDataSource fileRefreshableDataSource = annotationConfigApplicationContext
.getBean("fileBean", FileRefreshableDataSource.class);
assertThat(((List<FlowRule>) fileRefreshableDataSource.loadConfig()).size())
.isEqualTo(1);
FileRefreshableDataSourceFactoryBean factoryBean = annotationConfigApplicationContext
.getBean("&fileBean", FileRefreshableDataSourceFactoryBean.class);
assertThat(factoryBean.getBufSize()).isEqualTo(1024);
assertThat(factoryBean.getCharset()).isEqualTo("utf-8");
assertThat(factoryBean.getRecommendRefreshMs()).isEqualTo(2000);
assertThat(factoryBean.getFile()).isNotNull();
assertThat(factoryBean.getConverter()).isNotNull();
}
示例6
public synchronized void init() throws Exception {
if (ds != null) {
return;
}
String configPath = getRuleConfigPath();
if (StringUtil.isBlank(configPath)) {
throw new IllegalStateException("Empty rule config path, please set the file path in the env: "
+ SentinelEnvoyRlsConstants.RULE_FILE_PATH_ENV_KEY);
}
this.ds = new FileRefreshableDataSource<>(configPath, s -> Arrays.asList(yaml.loadAs(s, EnvoyRlsRule.class)));
EnvoyRlsRuleManager.register2Property(ds.getProperty());
}
示例7
@Test
public void testPostRegister() throws Exception {
FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
fileDataSourceProperties.setFile("classpath: flowrule.json");
fileDataSourceProperties.setRuleType(RuleType.FLOW);
FileRefreshableDataSource fileRefreshableDataSource = new FileRefreshableDataSource(
ResourceUtils
.getFile(StringUtils
.trimAllWhitespace(fileDataSourceProperties.getFile()))
.getAbsolutePath(),
new Converter<String, List<FlowRule>>() {
ObjectMapper objectMapper = new ObjectMapper();
@Override
public List<FlowRule> convert(String source) {
try {
return objectMapper.readValue(source,
new TypeReference<List<FlowRule>>() {
});
}
catch (IOException e) {
// ignore
}
return null;
}
});
fileDataSourceProperties.postRegister(fileRefreshableDataSource);
assertThat(FlowRuleManager.getRules())
.isEqualTo(fileRefreshableDataSource.loadConfig());
}
示例8
@Test
public void testSentinelDataSourceSuccess() throws Exception {
when(sentinelProperties.isEnabled()).thenReturn(true);
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
when(heartbeatSender.sendHeartbeat()).thenReturn(true);
Map<String, AbstractDataSource> dataSourceMap = new HashMap<>();
FileRefreshableDataSource fileDataSource1 = mock(FileRefreshableDataSource.class);
dataSourceMap.put("ds1-sentinel-file-datasource", fileDataSource1);
FileRefreshableDataSource fileDataSource2 = mock(FileRefreshableDataSource.class);
dataSourceMap.put("ds2-sentinel-file-datasource", fileDataSource2);
when(beanFactory.getBeansOfType(AbstractDataSource.class))
.thenReturn(dataSourceMap);
Health health = sentinelHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
Map<String, Status> dataSourceDetailMap = (Map<String, Status>) health
.getDetails().get("dataSource");
assertThat(dataSourceDetailMap.get("ds1-sentinel-file-datasource"))
.isEqualTo(Status.UP);
assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource"))
.isEqualTo(Status.UP);
}
示例9
@Test
public void testSentinelDataSourceFailed() throws Exception {
when(sentinelProperties.isEnabled()).thenReturn(true);
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
when(heartbeatSender.sendHeartbeat()).thenReturn(true);
Map<String, AbstractDataSource> dataSourceMap = new HashMap<>();
FileRefreshableDataSource fileDataSource1 = mock(FileRefreshableDataSource.class);
dataSourceMap.put("ds1-sentinel-file-datasource", fileDataSource1);
FileRefreshableDataSource fileDataSource2 = mock(FileRefreshableDataSource.class);
when(fileDataSource2.loadConfig())
.thenThrow(new RuntimeException("fileDataSource2 error"));
dataSourceMap.put("ds2-sentinel-file-datasource", fileDataSource2);
when(beanFactory.getBeansOfType(AbstractDataSource.class))
.thenReturn(dataSourceMap);
Health health = sentinelHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
Map<String, Status> dataSourceDetailMap = (Map<String, Status>) health
.getDetails().get("dataSource");
assertThat(dataSourceDetailMap.get("ds1-sentinel-file-datasource"))
.isEqualTo(Status.UP);
assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource"))
.isEqualTo(new Status(Status.DOWN.getCode(), "fileDataSource2 error"));
}
示例10
@Override
public FileRefreshableDataSource getObject() throws Exception {
return new FileRefreshableDataSource(new File(file), converter,
recommendRefreshMs, bufSize, Charset.forName(charset));
}
示例11
@Override
public Class<?> getObjectType() {
return FileRefreshableDataSource.class;
}