Java Code Examples for com.alibaba.csp.sentinel.config.SentinelConfig#setConfig()

The following examples show how to use com.alibaba.csp.sentinel.config.SentinelConfig#setConfig() . 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: DubboUtilsTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetInterfaceName() {

    URL url = URL.valueOf("dubbo://127.0.0.1:2181")
            .addParameter(CommonConstants.VERSION_KEY, "1.0.0")
            .addParameter(CommonConstants.GROUP_KEY, "grp1")
            .addParameter(CommonConstants.INTERFACE_KEY, DemoService.class.getName());
    Invoker invoker = mock(Invoker.class);
    when(invoker.getUrl()).thenReturn(url);
    when(invoker.getInterface()).thenReturn(DemoService.class);

    SentinelConfig.setConfig(DUBBO_INTERFACE_GROUP_VERSION_ENABLED, "false");
    assertEquals("com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService", DubboUtils.getInterfaceName(invoker));

    SentinelConfig.setConfig(DUBBO_INTERFACE_GROUP_VERSION_ENABLED, "true");
    assertEquals("com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:1.0.0:grp1", DubboUtils.getInterfaceName(invoker));

}
 
Example 2
Source File: DubboUtilsTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetResourceNameWithPrefix() throws NoSuchMethodException {
    Invoker invoker = mock(Invoker.class);
    when(invoker.getInterface()).thenReturn(DemoService.class);

    Invocation invocation = mock(Invocation.class);
    Method method = DemoService.class.getDeclaredMethod("sayHello", String.class, int.class);
    when(invocation.getMethodName()).thenReturn(method.getName());
    when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes());

    //test with default prefix
    String resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboProviderPrefix());
    assertEquals("dubbo:provider:com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);
    resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboConsumerPrefix());
    assertEquals("dubbo:consumer:com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);


    //test with custom prefix
    SentinelConfig.setConfig(DubboConfig.DUBBO_PROVIDER_PREFIX, "my:dubbo:provider:");
    SentinelConfig.setConfig(DubboConfig.DUBBO_CONSUMER_PREFIX, "my:dubbo:consumer:");
    resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboProviderPrefix());
    assertEquals("my:dubbo:provider:com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);
    resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboConsumerPrefix());
    assertEquals("my:dubbo:consumer:com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);

}
 
Example 3
Source File: HeartbeatSenderInitFuncTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetrieveInterval() {
    HeartbeatSender sender = mock(HeartbeatSender.class);

    long senderInterval = 5666;
    long configInterval = 6777;

    when(sender.intervalMs()).thenReturn(senderInterval);

    HeartbeatSenderInitFunc func = new HeartbeatSenderInitFunc();
    assertEquals(senderInterval, func.retrieveInterval(sender));

    // Invalid interval.
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_INTERVAL_MS, "-1");
    assertEquals(senderInterval, func.retrieveInterval(sender));

    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_INTERVAL_MS, String.valueOf(configInterval));
    assertEquals(configInterval, func.retrieveInterval(sender));
}
 
Example 4
Source File: TransportConfigTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetHeartbeatApiPath() {
    // use default heartbeat api path
    assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatApiPath()));
    assertEquals(TransportConfig.HEARTBEAT_DEFAULT_PATH, TransportConfig.getHeartbeatApiPath());

    // config heartbeat api path
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_API_PATH, "/demo");
    assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatApiPath()));
    assertEquals("/demo", TransportConfig.getHeartbeatApiPath());

    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_API_PATH, "demo/registry");
    assertEquals("/demo/registry", TransportConfig.getHeartbeatApiPath());

    SentinelConfig.removeConfig(TransportConfig.HEARTBEAT_API_PATH);
    assertEquals(TransportConfig.HEARTBEAT_DEFAULT_PATH, TransportConfig.getHeartbeatApiPath());
}
 
Example 5
Source File: SentinelHealthIndicatorTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinelDashboardConfiguredSuccess() throws Exception {
	when(sentinelProperties.isEnabled()).thenReturn(true);
	SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
	when(heartbeatSender.sendHeartbeat()).thenReturn(true);

	Health health = sentinelHealthIndicator.health();

	assertThat(health.getStatus()).isEqualTo(Status.UP);
}
 
Example 6
Source File: SentinelHealthIndicatorTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	beanFactory = mock(DefaultListableBeanFactory.class);
	sentinelProperties = mock(SentinelProperties.class);
	sentinelHealthIndicator = new SentinelHealthIndicator(beanFactory,
			sentinelProperties);

	SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "");

	heartbeatSender = mock(HeartbeatSender.class);
	Field heartbeatSenderField = ReflectionUtils
			.findField(HeartbeatSenderProvider.class, "heartbeatSender");
	heartbeatSenderField.setAccessible(true);
	ReflectionUtils.setField(heartbeatSenderField, null, heartbeatSender);
}
 
Example 7
Source File: BaseTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void clearDubboContext() {
    SentinelConfig.setConfig("csp.sentinel.dubbo.resource.use.prefix", "false");
    SentinelConfig.setConfig(DubboConfig.DUBBO_PROVIDER_PREFIX, "");
    SentinelConfig.setConfig(DubboConfig.DUBBO_CONSUMER_PREFIX, "");
    SentinelConfig.setConfig(DubboConfig.DUBBO_INTERFACE_GROUP_VERSION_ENABLED, "false");
    DubboFallbackRegistry.setConsumerFallback(new DefaultDubboFallback());
    RpcContext.removeContext();

}
 
Example 8
Source File: DubboUtilsTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() {
    SentinelConfig.setConfig("csp.sentinel.dubbo.resource.use.prefix", "false");
    SentinelConfig.setConfig(DubboConfig.DUBBO_PROVIDER_PREFIX, "");
    SentinelConfig.setConfig(DubboConfig.DUBBO_CONSUMER_PREFIX, "");
    SentinelConfig.setConfig(DUBBO_INTERFACE_GROUP_VERSION_ENABLED, "false");
}
 
Example 9
Source File: DubboUtilsTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    SentinelConfig.setConfig("csp.sentinel.dubbo.resource.use.prefix", "true");
    SentinelConfig.setConfig(DubboConfig.DUBBO_PROVIDER_PREFIX, "");
    SentinelConfig.setConfig(DubboConfig.DUBBO_CONSUMER_PREFIX, "");
    SentinelConfig.setConfig(DUBBO_INTERFACE_GROUP_VERSION_ENABLED, "false");
}
 
Example 10
Source File: SentinelHealthIndicatorTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinelDashboardConfiguredFailed() throws Exception {
	when(sentinelProperties.isEnabled()).thenReturn(true);
	SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
	when(heartbeatSender.sendHeartbeat()).thenReturn(false);

	Health health = sentinelHealthIndicator.health();

	assertThat(health.getStatus()).isEqualTo(Status.DOWN);
	assertThat(health.getDetails().get("dashboard")).isEqualTo(
			new Status(Status.DOWN.getCode(), "localhost:8080 can't be connected"));
}
 
Example 11
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 12
Source File: TransportConfigTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHeartbeatClientIp() {
    String clientIp = "10.10.10.10";
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_CLIENT_IP, clientIp);
    // Set heartbeat client ip to system property.
    String ip = TransportConfig.getHeartbeatClientIp();

    assertNotNull(ip);
    assertEquals(clientIp, ip);

    // Set no heartbeat client ip.
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_CLIENT_IP, "");
    assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatClientIp()));
}
 
Example 13
Source File: TransportConfigTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHeartbeatInterval() {
    long interval = 20000;
    assertNull(TransportConfig.getHeartbeatIntervalMs());
    // Set valid interval.
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_INTERVAL_MS, String.valueOf(interval));
    assertEquals(new Long(interval), TransportConfig.getHeartbeatIntervalMs());
    // Set invalid interval.
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_INTERVAL_MS, "Sentinel");
    assertNull(TransportConfig.getHeartbeatIntervalMs());
}
 
Example 14
Source File: TransportConfigTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHeartbeatClientIp() {
    String clientIp = "10.10.10.10";
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_CLIENT_IP, clientIp);
    // Set heartbeat client ip to system property.
    String ip = TransportConfig.getHeartbeatClientIp();

    assertNotNull(ip);
    assertEquals(clientIp, ip);

    // Set no heartbeat client ip.
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_CLIENT_IP, "");
    assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatClientIp()));
}
 
Example 15
Source File: TransportConfigTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHeartbeatInterval() {
    long interval = 20000;
    assertNull(TransportConfig.getHeartbeatIntervalMs());
    // Set valid interval.
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_INTERVAL_MS, String.valueOf(interval));
    assertEquals(new Long(interval), TransportConfig.getHeartbeatIntervalMs());
    // Set invalid interval.
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_INTERVAL_MS, "Sentinel");
    assertNull(TransportConfig.getHeartbeatIntervalMs());
}
 
Example 16
Source File: HeartbeatSenderInitFunc.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
private void setIntervalIfNotExists(long interval) {
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_INTERVAL_MS, String.valueOf(interval));
}
 
Example 17
Source File: WebServletConfig.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public static void setBlockPage(String blockPage) {
    SentinelConfig.setConfig(BLOCK_PAGE, blockPage);
}
 
Example 18
Source File: HttpHeartbeatSenderTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
private void setAddr(String serverList) {
    SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, serverList);
}
 
Example 19
Source File: AbstractDubboFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    SentinelConfig.setConfig("csp.sentinel.dubbo.resource.use.prefix", "true");
    SentinelConfig.setConfig(DubboConfig.DUBBO_PROVIDER_PREFIX, "");
    SentinelConfig.setConfig(DubboConfig.DUBBO_CONSUMER_PREFIX, "");
}
 
Example 20
Source File: HeartbeatSenderInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
private void setIntervalIfNotExists(long interval) {
    SentinelConfig.setConfig(TransportConfig.HEARTBEAT_INTERVAL_MS, String.valueOf(interval));
}