com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource Java Examples

The following examples show how to use com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileDataSourceDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #2
Source File: FileDataSourceInit.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: FileDataSourceDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #4
Source File: FileDataSourceInit.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: FileRefreshableDataSourceFactoryBeanTests.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #6
Source File: EnvoyRlsRuleDataSourceService.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #7
Source File: DataSourcePropertiesTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #8
Source File: SentinelHealthIndicatorTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #9
Source File: SentinelHealthIndicatorTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #10
Source File: FileRefreshableDataSourceFactoryBean.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public FileRefreshableDataSource getObject() throws Exception {
	return new FileRefreshableDataSource(new File(file), converter,
			recommendRefreshMs, bufSize, Charset.forName(charset));
}
 
Example #11
Source File: FileRefreshableDataSourceFactoryBean.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public Class<?> getObjectType() {
	return FileRefreshableDataSource.class;
}