use of com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource in project spring-cloud-alibaba by alibaba.
the class SentinelHealthIndicatorTests method testSentinelDataSourceSuccess.
@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);
}
use of com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource in project spring-cloud-alibaba by alibaba.
the class SentinelHealthIndicatorTests method testSentinelDataSourceFailed.
@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.UNKNOWN);
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.UNKNOWN.getCode(), "fileDataSource2 error"));
}
use of com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource in project spring-cloud-alibaba by alibaba.
the class FileRefreshableDataSourceFactoryBeanTests method testFile.
@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();
}
use of com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource in project spring-cloud-alibaba by alibaba.
the class DataSourcePropertiesTests method testPostRegister.
@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());
}
use of com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource in project learn-simple by muggle0.
the class MyflieInitFunc method init.
@Override
public void init() throws Exception {
URL resource = MyflieInitFunc.class.getClassLoader().getResource("");
File file = new File(resource.getPath() + "/config/flow.json");
File fileParent = file.getParentFile();
if (!fileParent.exists()) {
fileParent.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
ReadableDataSource<String, List<FlowRule>> flowReadDataSource = new FileRefreshableDataSource<>(resource.getPath() + "/config/flow.json", source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
}));
FlowRuleManager.register2Property(flowReadDataSource.getProperty());
WritableDataSource<List<FlowRule>> flowWriteDataSource = new FileWritableDataSource<>(resource.getPath() + "/config/flow.json", t -> JSON.toJSONString(t));
WritableDataSourceRegistry.registerFlowDataSource(flowWriteDataSource);
}
Aggregations