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

The following examples show how to use com.alibaba.csp.sentinel.datasource.AbstractDataSource. 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: AbstractDataSourceProperties.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public void postRegister(AbstractDataSource dataSource) {
	switch (this.getRuleType()) {
	case FLOW:
		FlowRuleManager.register2Property(dataSource.getProperty());
		break;
	case DEGRADE:
		DegradeRuleManager.register2Property(dataSource.getProperty());
		break;
	case PARAM_FLOW:
		ParamFlowRuleManager.register2Property(dataSource.getProperty());
		break;
	case SYSTEM:
		SystemRuleManager.register2Property(dataSource.getProperty());
		break;
	case AUTHORITY:
		AuthorityRuleManager.register2Property(dataSource.getProperty());
		break;
	case GW_FLOW:
		GatewayRuleManager.register2Property(dataSource.getProperty());
		break;
	case GW_API_GROUP:
		GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
		break;
	default:
		break;
	}
}
 
Example #2
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 #3
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 #4
Source File: SentinelHealthIndicator.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
	Map<String, Object> detailMap = new HashMap<>();

	// If sentinel isn't enabled, set the status up and set the enabled to false in
	// detail
	if (!sentinelProperties.isEnabled()) {
		detailMap.put("enabled", false);
		builder.up().withDetails(detailMap);
		return;
	}

	detailMap.put("enabled", true);

	// Check health of Dashboard
	boolean dashboardUp = true;
	List<Tuple2<String, Integer>> consoleServerList = TransportConfig
			.getConsoleServerList();
	if (CollectionUtils.isEmpty(consoleServerList)) {
		// If Dashboard isn't configured, it's OK and mark the status of Dashboard
		// with UNKNOWN.
		detailMap.put("dashboard",
				new Status(Status.UNKNOWN.getCode(), "dashboard isn't configured"));
	}
	else {
		// If Dashboard is configured, send a heartbeat message to it and check the
		// result
		HeartbeatSender heartbeatSender = HeartbeatSenderProvider
				.getHeartbeatSender();
		boolean result = heartbeatSender.sendHeartbeat();
		if (result) {
			detailMap.put("dashboard", Status.UP);
		}
		else {
			// If failed to send heartbeat message, means that the Dashboard is DOWN
			dashboardUp = false;
			detailMap.put("dashboard",
					new Status(Status.DOWN.getCode(), String.format(
							"the dashboard servers [%s] one of them can't be connected",
							consoleServerList)));
		}
	}

	// Check health of DataSource
	boolean dataSourceUp = true;
	Map<String, Object> dataSourceDetailMap = new HashMap<>();
	detailMap.put("dataSource", dataSourceDetailMap);

	// Get all DataSources and each call loadConfig to check if it's OK
	// If no Exception thrown, it's OK
	// Note:
	// Even if the dynamic config center is down, the loadConfig() might return
	// successfully
	// e.g. for Nacos client, it might retrieve from the local cache)
	// But in most circumstances it's okay
	Map<String, AbstractDataSource> dataSourceMap = beanFactory
			.getBeansOfType(AbstractDataSource.class);
	for (Map.Entry<String, AbstractDataSource> dataSourceMapEntry : dataSourceMap
			.entrySet()) {
		String dataSourceBeanName = dataSourceMapEntry.getKey();
		AbstractDataSource dataSource = dataSourceMapEntry.getValue();
		try {
			dataSource.loadConfig();
			dataSourceDetailMap.put(dataSourceBeanName, Status.UP);
		}
		catch (Exception e) {
			// If one DataSource failed to loadConfig, means that the DataSource is
			// DOWN
			dataSourceUp = false;
			dataSourceDetailMap.put(dataSourceBeanName,
					new Status(Status.DOWN.getCode(), e.getMessage()));
		}
	}

	// If Dashboard and DataSource are both OK, the health status is UP
	if (dashboardUp && dataSourceUp) {
		builder.up().withDetails(detailMap);
	}
	else {
		builder.down().withDetails(detailMap);
	}
}