Search in sources :

Example 1 with SentinelRpcException

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;
}
Also used : SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException) RpcResult(com.alibaba.dubbo.rpc.RpcResult)

Example 2 with SentinelRpcException

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();
        }
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) FooServiceConsumer(com.alibaba.csp.sentinel.demo.apache.dubbo.consumer.FooServiceConsumer) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with SentinelRpcException

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();
        }
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) FooServiceConsumer(com.alibaba.csp.sentinel.demo.apache.dubbo.consumer.FooServiceConsumer) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException)

Example 4 with SentinelRpcException

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();
        }
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException)

Example 5 with SentinelRpcException

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();
        }
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) FooServiceConsumer(com.alibaba.csp.sentinel.demo.dubbo.consumer.FooServiceConsumer) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException)

Aggregations

SentinelRpcException (com.alibaba.csp.sentinel.slots.block.SentinelRpcException)7 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)4 FooServiceConsumer (com.alibaba.csp.sentinel.demo.apache.dubbo.consumer.FooServiceConsumer)2 FooServiceConsumer (com.alibaba.csp.sentinel.demo.dubbo.consumer.FooServiceConsumer)2 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)1 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)1 RpcResult (com.alibaba.dubbo.rpc.RpcResult)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)1 ApplicationContext (org.springframework.context.ApplicationContext)1