Search in sources :

Example 1 with FileRefreshableDataSource

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);
}
Also used : Status(org.springframework.boot.actuate.health.Status) HashMap(java.util.HashMap) Health(org.springframework.boot.actuate.health.Health) AbstractDataSource(com.alibaba.csp.sentinel.datasource.AbstractDataSource) HashMap(java.util.HashMap) Map(java.util.Map) FileRefreshableDataSource(com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource) Test(org.junit.Test)

Example 2 with FileRefreshableDataSource

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"));
}
Also used : Status(org.springframework.boot.actuate.health.Status) HashMap(java.util.HashMap) Health(org.springframework.boot.actuate.health.Health) AbstractDataSource(com.alibaba.csp.sentinel.datasource.AbstractDataSource) HashMap(java.util.HashMap) Map(java.util.Map) FileRefreshableDataSource(com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource) Test(org.junit.Test)

Example 3 with FileRefreshableDataSource

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();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) FileRefreshableDataSourceFactoryBean(com.alibaba.cloud.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean) List(java.util.List) FileRefreshableDataSource(com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource) Test(org.junit.Test)

Example 4 with FileRefreshableDataSource

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());
}
Also used : FileDataSourceProperties(com.alibaba.cloud.sentinel.datasource.config.FileDataSourceProperties) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) IOException(java.io.IOException) FileRefreshableDataSource(com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with FileRefreshableDataSource

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);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(com.alibaba.fastjson.TypeReference) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) FileWritableDataSource(com.alibaba.csp.sentinel.datasource.FileWritableDataSource) File(java.io.File) URL(java.net.URL) FileRefreshableDataSource(com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource)

Aggregations

FileRefreshableDataSource (com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource)5 Test (org.junit.Test)4 List (java.util.List)3 AbstractDataSource (com.alibaba.csp.sentinel.datasource.AbstractDataSource)2 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Health (org.springframework.boot.actuate.health.Health)2 Status (org.springframework.boot.actuate.health.Status)2 FileDataSourceProperties (com.alibaba.cloud.sentinel.datasource.config.FileDataSourceProperties)1 FileRefreshableDataSourceFactoryBean (com.alibaba.cloud.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean)1 FileWritableDataSource (com.alibaba.csp.sentinel.datasource.FileWritableDataSource)1 TypeReference (com.alibaba.fastjson.TypeReference)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1