use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelWebFluxIntegrationTest method testCustomizedUrlCleaner.
@Test
public void testCustomizedUrlCleaner() throws Exception {
final String fooPrefix = "/foo/";
String url1 = fooPrefix + 1;
String url2 = fooPrefix + 2;
WebFluxCallbackManager.setUrlCleaner(((exchange, originUrl) -> {
if (originUrl.startsWith(fooPrefix)) {
return "/foo/*";
}
return originUrl;
}));
this.webClient.get().uri(url1).exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello 1");
this.webClient.get().uri(url2).exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello 2");
ClusterNode cn = ClusterBuilderSlot.getClusterNode(fooPrefix + "*");
assertEquals(2, cn.passQps(), 0.01);
assertNull(ClusterBuilderSlot.getClusterNode(url1));
assertNull(ClusterBuilderSlot.getClusterNode(url2));
WebFluxCallbackManager.resetUrlCleaner();
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class MetricTimerListener method run.
@Override
public void run() {
Map<Long, List<MetricNode>> maps = new TreeMap<>();
for (Entry<ResourceWrapper, ClusterNode> e : ClusterBuilderSlot.getClusterNodeMap().entrySet()) {
ClusterNode node = e.getValue();
Map<Long, MetricNode> metrics = node.metrics();
aggregate(maps, metrics, node);
}
aggregate(maps, Constants.ENTRY_NODE.metrics(), Constants.ENTRY_NODE);
if (!maps.isEmpty()) {
for (Entry<Long, List<MetricNode>> entry : maps.entrySet()) {
try {
metricWriter.write(entry.getKey(), entry.getValue());
} catch (Exception e) {
RecordLog.warn("[MetricTimerListener] Write metric error", e);
}
}
}
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelAnnotationIntegrationTest method testForeignBlockHandlerClass.
@Test
public void testForeignBlockHandlerClass() throws Exception {
assertThat(fooService.random()).isNotEqualTo(FooUtil.BLOCK_FLAG);
String resourceName = MethodUtil.resolveMethodName(FooService.class.getDeclaredMethod("random"));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();
FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
assertThat(fooService.random()).isEqualTo(FooUtil.BLOCK_FLAG);
assertThat(cn.blockQps()).isPositive();
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelAnnotationIntegrationTest method testFallbackWithNoParams.
@Test
public void testFallbackWithNoParams() throws Exception {
assertThat(fooService.fooWithFallback(1)).isEqualTo("Hello for 1");
String resourceName = "apiFooWithFallback";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();
// Fallback should be ignored for this.
try {
fooService.fooWithFallback(5758);
fail("should not reach here");
} catch (IllegalAccessException e) {
assertThat(cn.exceptionQps()).isZero();
}
// Fallback should take effect.
assertThat(fooService.fooWithFallback(5763)).isEqualTo("eee...");
assertThat(cn.exceptionQps()).isPositive();
assertThat(cn.blockQps()).isZero();
FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
// Fallback should not take effect for BlockException, as blockHandler is configured.
assertThat(fooService.fooWithFallback(2221)).isEqualTo("Oops, 2221");
assertThat(cn.blockQps()).isPositive();
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelAnnotationIntegrationTest method testFallBackPrivateMethod.
@Test
public void testFallBackPrivateMethod() throws Exception {
String resourceName = "apiFooWithFallback";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
try {
fooService.fooWithPrivateFallback(5758);
fail("should not reach here");
} catch (Exception ex) {
// Should not be traced.
assertThat(cn.exceptionQps()).isZero();
}
assertThat(fooService.fooWithPrivateFallback(5763)).isEqualTo("EEE...");
// Test for blockHandler
FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
assertThat(fooService.fooWithPrivateFallback(2221)).isEqualTo("Oops, 2221");
}
Aggregations