Search in sources :

Example 1 with FooServiceConsumer

use of com.alibaba.csp.sentinel.demo.apache.dubbo.consumer.FooServiceConsumer 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 2 with FooServiceConsumer

use of com.alibaba.csp.sentinel.demo.apache.dubbo.consumer.FooServiceConsumer 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)

Aggregations

FooServiceConsumer (com.alibaba.csp.sentinel.demo.apache.dubbo.consumer.FooServiceConsumer)2 SentinelRpcException (com.alibaba.csp.sentinel.slots.block.SentinelRpcException)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 ExecutionException (java.util.concurrent.ExecutionException)1