use of com.alibaba.csp.sentinel.slots.block.SentinelRpcException in project Sentinel by alibaba.
the class DefaultDubboFallback method handle.
@Override
public Result handle(Invoker<?> invoker, Invocation invocation, BlockException ex) {
// Just wrap the exception. edit by wzg923 2020/9/23
RpcResult result = new RpcResult();
result.setException(new SentinelRpcException(ex.toRuntimeException()));
return result;
}
use of com.alibaba.csp.sentinel.slots.block.SentinelRpcException in project Sentinel by alibaba.
the class FooConsumerExceptionDegradeBootstrap method main.
public static void main(String[] args) throws InterruptedException, ExecutionException {
AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext();
consumerContext.register(ConsumerConfiguration.class);
consumerContext.refresh();
FooServiceConsumer service = consumerContext.getBean(FooServiceConsumer.class);
initExceptionFallback(3);
registryCustomFallback();
for (int i = 0; i < 10; i++) {
try {
String message = service.exceptionTest(true, false);
System.out.println("Result: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
// sleep 3s to skip the time window
initExceptionFallback(3);
Thread.sleep(3000);
for (int i = 0; i < 10; i++) {
try {
String message = service.exceptionTest(false, true);
System.out.println("Result: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
initExceptionFallback(3);
Thread.sleep(3000);
try {
// timeout to trigger the fallback
CompletableFuture<String> completableFuture = RpcContext.getContext().asyncCall(() -> service.exceptionTest(false, true));
System.out.println("Result: " + completableFuture.get());
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
try {
CompletableFuture<String> result = RpcContext.getContext().asyncCall(() -> service.exceptionTest(false, true));
System.out.println("Result: " + result.get());
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
use of com.alibaba.csp.sentinel.slots.block.SentinelRpcException in project Sentinel by alibaba.
the class FooConsumerBootstrap method main.
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext();
consumerContext.register(ConsumerConfiguration.class);
consumerContext.refresh();
initFlowRule(10, false);
FooServiceConsumer service = consumerContext.getBean(FooServiceConsumer.class);
for (int i = 0; i < 15; i++) {
try {
String message = service.sayHello("Eric");
System.out.println("Success: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
// method flowcontrol
Thread.sleep(1000);
initFlowRule(20, true);
for (int i = 0; i < 10; i++) {
try {
String message = service.sayHello("Eric");
System.out.println("Success: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
System.out.println("fallback:" + service.doAnother());
} catch (Exception ex) {
ex.printStackTrace();
}
}
// fallback to result
Thread.sleep(1000);
registryCustomFallback();
for (int i = 0; i < 10; i++) {
try {
String message = service.sayHello("Eric");
System.out.println("Result: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
// fallback to exception
Thread.sleep(1000);
registryCustomFallbackForCustomException();
for (int i = 0; i < 10; i++) {
try {
String message = service.sayHello("Eric");
System.out.println("Result: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
Thread.sleep(1000);
registryCustomFallbackWhenFallbackError();
for (int i = 0; i < 10; i++) {
try {
String message = service.sayHello("Eric");
System.out.println("Result: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
use of com.alibaba.csp.sentinel.slots.block.SentinelRpcException in project spring-cloud-alibaba by alibaba.
the class SentinelDubboConsumerApp method main.
public static void main(String[] args) {
FlowRule flowRule = new FlowRule();
flowRule.setResource("com.alibaba.cloud.examples.FooService:hello(java.lang.String)");
flowRule.setCount(10);
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setLimitApp("default");
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
SpringApplicationBuilder consumerBuilder = new SpringApplicationBuilder();
ApplicationContext applicationContext = consumerBuilder.web(WebApplicationType.NONE).sources(SentinelDubboConsumerApp.class).run(args);
FooServiceConsumer service = applicationContext.getBean(FooServiceConsumer.class);
for (int i = 0; i < 15; i++) {
try {
String message = service.hello("Jim");
System.out.println((i + 1) + " -> Success: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
use of com.alibaba.csp.sentinel.slots.block.SentinelRpcException in project Sentinel by alibaba.
the class FooConsumerBootstrap method main.
public static void main(String[] args) {
AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext();
consumerContext.register(ConsumerConfiguration.class);
consumerContext.refresh();
FooServiceConsumer service = consumerContext.getBean(FooServiceConsumer.class);
for (int i = 0; i < 15; i++) {
try {
String message = service.sayHello("Eric");
System.out.println("Success: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Aggregations