当前位置 博文首页 > 不送花的程序猿:(万字好文)Dubbo服务熔断与降级的深入讲解&am

    不送花的程序猿:(万字好文)Dubbo服务熔断与降级的深入讲解&am

    作者:不送花的程序猿 时间:2021-01-31 00:22

    代码实战,源码分析,带你彻底深入Dubbo架构如何做服务熔断与降级

    原文链接:(万字好文)Dubbo服务熔断与降级的深入讲解&代码实战

    一、Dubbo服务降级实战

    1 mock 机制

    谈到服务降级,Dubbo 本身就提供了服务降级的机制;而 Dubbo 的服务降级机制主要是利用服务消费者的 mock 属性。

    服务消费者的 mock 属性有以下三种使用方式,下面将带着例子简单介绍一下。

    1.1 服务消费者注册url的mock属性

    例子:
    mock=return+null,即当服务提供者出现异常(宕机或者业务异常),则返回null给服务消费者。

    2021-01-26 09:39:54.631 [main] [INFO ] [o.a.d.r.z.ZookeeperRegistry] [] [] -  [DUBBO] Notify urls for subscribe url consumer://127.0.0.1/com.winfun.service.DubboServiceOne?application=dubbo-service&category=providers,configurators,routers&check=false&dubbo=2.0.2&init=false&interface=com.winfun.service.DubboServiceOne&lazy=true&methods=sayHello&mock=return+null&pid=36270&qos.enable=false&reference.filter=default,dubboLogFilter&release=2.7.7&retries=1&side=consumer&sticky=false&timestamp=1611625194544
    

    1.2 @DubboReference注解或者dubbo:reference标签的mock属性

    例子:
    mock="return null",即当服务提供者出现异常(宕机或者业务异常),则返回null给服务消费者。

    public class HelloController{
        
        @DubboReference(check = false,lazy = true,retries = 1,mock = "return null")
        private DubboServiceOne dubboServiceOne;
        
        //.....
    }
    

    1.3 服务消费者mock属性设置为true+Mock实现类

    例子:
    Mock实现类 为 Dubbo接口 的实现类,并且 Mock实现类 与 Dubbo接口 放同一个项目中。

    /**
     *
     * DubboServiceTwo
     * @author winfun
     * @date 2020/10/29 5:00 下午
     **/
    public interface DubboServiceTwo {
    
        /***
         *  say hi
         * @author winfun
         * @param name name
         * @return {@link ApiResult<String> }
         **/
        ApiResult<String> sayHi(String name);
    }
    
    /**
     * DubboServiceTwo 降级实现类
     * @author winfun
     * @date 2021/1/26 9:21 上午
     **/
    public class DubboServiceTwoMock implements DubboServiceTwo{
    
    
        /***
         *  say hi
         * @author winfun
         * @param name name
         * @return {@link ApiResult <String> }
         **/
        @Override
        public ApiResult<String> sayHi(String name) {
            return ApiResult.fail("Mock实现类-服务降级了");
        }
    }
    

    2 实战例子:

    下面将使用第二种方式来展示 Duboo 自身提供的服务降级机制。

    2.1 consumer 代码:

    /**
     * Say Hello
     * @author winfun
     * @date 2020/10/29 5:12 下午
     **/
    @RestController
    public class HelloController {
    
        @DubboReference(check = false,lazy = true,retries = 1,mock="return {\"code\":1,\"message\":\"熔断限流了\"}")
        private DubboServiceOne dubboServiceOne;
    
        @GetMapping("/hello/{name}")
        public ApiResult sayHello(@PathVariable("name") String name){
            return this.dubboServiceOne.sayHello(name);
        }
    }
    

    2.2 provider 代码:

    /**
     * DubboServiceOneImpl
     * @author winfun
     * @date 2020/10/29 5:04 下午
     **/
    @DubboService(interfaceClass = DubboServiceOne.class)
    public class DubboServiceOneImpl implements DubboServiceOne {
        /***
         *  say hello
         * @author winfun
         * @param name name
         * @return {@link ApiResult<String> }
         **/
        @SneakyThrows
        @Override
        public ApiResult<String> sayHello(String name) {
            // dubbo 接口默认超时时间为1s,我们这里直接休眠5s
            Thread.sleep(5000);
            return ApiResult.success("hello "+name);
        }
    

    2.3 结果分析:

    情况一:服务提供者没起来

    这个时候服务消费者不会进行重试,只会直接进行服务降级,返回我们设定的 Mock 值。

    2021-01-27 18:02:12.288 [http-nio-8081-exec-1] [WARN ] [o.a.d.r.c.s.w.MockClusterInvoker] [/hello/name] [16117417328056102141] -  [DUBBO] fail-mock: sayHello fail-mock enabled , url : zookeeper://127.0.0.1:2181/org.apache.dubbo.registry.RegistryService?application=dubbo-service&check=false&dubbo=2.0.2&init=false&interface=com.winfun.service.DubboServiceOne&lazy=true&methods=sayHello&mock=return+%7B%22code%22%3A1%2C%22message%22%3A%22%E7%86%94%E6%96%AD%E9%99%90%E6%B5%81%E4%BA%86%22%7D&pid=51874&qos.enable=false&reference.filter=default,dubboLogFilter,-sentinel.dubbo.consumer.filter&register.ip=172.26.144.16&release=2.7.7&retries=0&side=consumer&sticky=false&timestamp=1611741716665, dubbo version: 2.7.7, current host: 172.26.144.16
    org.apache.dubbo.rpc.RpcException: No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 172.26.144.16 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    	//.....省略
    2021-01-27 18:02:12.289 [http-nio-8081-exec-1] [INFO ] [o.a.d.r.c.s.w.MockClusterInvoker] [/hello/name] [16117417328056102141] -  [DUBBO] Exception when try to invoke mock. Get mock invokers error for service:com.winfun.service.DubboServiceOne, method:sayHello, will construct a new mock with 'new MockInvoker()'., dubbo version: 2.7.7, current host: 172.26.144.16
    org.apache.dubbo.rpc.RpcException: No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 172.26.144.16 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
        //....省略
    

    大家可能会存在一个质疑点:我上面说的是不会进行重试,但是控制台我们看到了两次异常;其实第二次异常,是获取 MockInvoker 导致的,所以大家可以先忽略,下面我会详细分析 MockClusterInvoker 的源码,就知道为啥会有两次异常了。

    情况二:服务提供者超时

    注意:Dubbo 提供的服务降级机制,不支持对服务提供者业务异常的情况。

    这个时候消费者会进行 1+retries 次调用,然后再进行服务降级。

    org.apache.dubbo.rpc.RpcException: Failed to invoke the method sayHello in the service com.winfun.service.DubboServiceOne. Tried 2 times of the providers [127.0.0.1:20881] (1/1) from the registry 127.0.0.1:2181 on the consumer 172.26.144.16 using the dubbo version 2.7.7. Last error is: Invoke remote method timeout. method: sayHello, provider: dubbo://127.0.0.1:20881/com.winfun.service.DubboServiceOne?anyhost=true&application=dubbo-service&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&init=false&interface=com.winfun.service.DubboServiceOne&lazy=true&methods=sayHello&mock=return+%7B%22code%22%3A1%2C%22message%22%3A%22%E7%86%94%E6%96%AD%E9%99%90%E6%B5%81%E4%BA%86%22%7D&pid=52020&qos.enable=false&reference.filter=default,dubboLogFilter,-sentinel.dubbo.consumer.filter&register.ip=172.26.144.16&release=2.7.7&remote.application=dubbo-provider-one&retries=1&service.filter=default,dubboLogFilter&side=consumer&sticky=false&timestamp=1611742232968, cause: org.apache.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2021-01-27 18:10:42.189, end time: 2021-01-27 18:10:43.204, client elapsed: 1 ms, server elapsed: 1014 ms, timeout: 1000 ms, request: Request [id=1, version=2.0.2, twoway=true, event=false, broken=false, data=null], channel: /172.26.144.16:56663 -> /172.26.144.16:20881
    	at org.apache.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:113)
    //....省略
    

    3 源码分析

    关于服务降级的逻辑,主要在 Dubbo 提供的 MockClusterInvoker 类中。

    下面我们直接看看 MockClusterInvoker 的源码:

    public class MockClusterInvoker<T> implements Invoker<T> {
    
        // ...省略
    
        @Override
        public Result invoke(Invocation invocation) throws RpcException {
            Result result = null;
            // 从 consumer 的注册url中获取方法的 mock 属性值
            String value = getUrl().getMethodParameter(invocation.getMethodName(), MOCK_KEY, Boolean.FALSE.toString()).trim();
            // mock 为空或者false,不进行服务降级处理
            if (value.length() == 0 || "false".equalsIgnoreCase(value)) {
                //no mock
                result = this.invoker.invoke(invocation);
            // 是否强行执行服务降级处理
            } else if (value.startsWith("force")) {
                if (logger.isWarnEnabled()) {
                    logger.warn("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + getUrl());
                }
                //force:direct mock
                result = doMockInvoke(invocation, null);
            } else {
                //fail-mock
                try {
                    result = this.invoker.invoke(invocation);
    
                    //fix:#4585
                    if(result.getException() != null && result.getException() instanceof RpcException){
                        RpcException rpcException= (RpcException)result.getException();
                        // 如果是业务异常,直接抛出
                        if(rpcException.isBiz()){
                            throw  rpcException;
                        }else {
                            // 服务降级处理
                            result = doMockInvoke(invocation, rpcException);
                        }
                    }
    
                } catch (RpcException e) {
                    // 如果是业务异常,直接抛出
                    if (e.isBiz()) {
                        throw e;
                    }
    
                    if (logger.isWarnEnabled()) {
                        logger.warn("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + getUrl(), e);
                    }
                    // 服务降级处理
                    result = doMockInvoke(invocation, e);
                }
            }
            return result;
        }
    
        @SuppressWarnings({"unchecked", "rawtypes"})
        private Result doMockInvoke(Invocation invocation, RpcException e) {
            Result result = null;
            Invoker<T> minvoker;
    
            // 获取服务降级 Invoker 列表
            List<Invoker<T>> mockInvokers = selectMockInvoker(invocation);
            if (CollectionUtils.isEmpty(mockInvokers)) {
                // 服务降级 Invoker 列表为空,默认使用 Dubbo 提供的 MockInvoker 类
                minvoker = (Invoker<T>) new MockInvoker(getUrl(), directory.getInterface());
            } else {
                minvoker = mockInvokers.get(0);
            }
            try {
                // 服务降级处理
                result = minvoker.invoke(invocation);
            } catch (RpcException me) {
                if (me.isBiz()) {
                    result = AsyncRpcResult.newDefaultAsyncResult(me.getCause(), invocation);
                } else {
                    throw new RpcException(me.getCode(), getMockExceptionMessage(e, me), me.getCause());
                }
            } catch (Throwable me) {
                throw new RpcException(getMockExceptionMessage(e, me), me.getCause());
            }
            return result;
        }
    
        //...
    }
    
    

    从上面的源码分析可知:

    • 在 MockClusterInvoker#invoke 方法里面,如果是业务异常是不会做 mock 做处理的。而其他关于 RPC 的异常,是会做 mock 处理的,例如我们这里拿来测试的服务提供者超时异常和服务提供者不存在;

    • 在 doMockInvoke 方法中,首先会调用 selectMockInvoker 方法会尝试获取 MockInvoker,而默认情况下,都是没有的,所以最后会使用 Dubbo 提供的 MockInvoker。

    这里也回答了情况一例子中,会抛出两次异常的原因,就是因为获取 MockInvoker 的时候,会调用 selectMockInvoker,而如果服务没启动,那么必须还会再抛一次 No provider 的异常啦~

    最后,大家如果有兴趣,可以自行继续研究 MockInvoker 的源码,特别是里面的 invoke 和 parseMockValue 方法,分别是服务降级的逻辑和 Mock 属性内容的解析。

    如果不是看了 MockInvoker#parseMockValue 方法,一开始我都不知道如何返回 Json 格式的结果,只会直接返回 null,哈哈哈。所以源码还是得多看!

    到此,关于 Dubbo 提供的服务降级的 Mock 机制已经简单地过了一遍,但是我们可以发现有一个问题:

    4 Dubbo 服务降级机制的缺陷

    4.1 缺陷分析

    其实经过上面的例子分析和源码分析,我们会发现一个问题:由于 dubbo 不带熔断机制,所以尽管每次因为 RPC 异常而导致调用失败,也不会进行熔断处理;即不管调用失败多少次,消费者还是会继续进行调用。

    其实这样会导致服务的资源浪费:

    • 只要服务提供者出现异常达到一定的次数,其实可以理解为服务提供者短时间内已经不能正常提供服务了,后续再调用也是浪费资源。
    • 如果是上述的超时问题,消费者还会进行 1+retires 次的 RPC 调用,这样就更加浪费资源了。

    所以,为 dubbo 配一个熔断机制是非常有必要的了。

    二 熔断机制

    我个人对熔断的理解:

    如果当调用失败达到指定的次数,则将熔断器打开一段时间,即将请求链路断开;在指定时间内,都不再让消费者向提供者发送请求;当熔断时间到了,就将熔断器设置为半打开的状态,此时消费者可以往提供者发送请求,并统计成功次数,如果达到指定的成功次数,熔断器则变为关闭状态,即将请求链路打开,否则熔断器又变回打开状态。

    所以说,只要给dubbo加上熔断机制,不管是业务错误还是请求超时,只要时间内达到了一定的次数就做上述的熔断处理,这样就可以防止没有必要的调用。

    熔断框架比较:https://www.icode9.com/content-4-620551.html

    1 引入 Sentinel

    经过上面的对比,我个人是建议使用 Sentinel 的,因为它提供了更加灵活的使用方式,并且支持更多的规则,还提供了一个易用强大的控制台。

    Sentinel 提供了三大接入方式:利用 sentinel-core 组件进行硬代码、利用 sentinel-annotation-aspectj 组件提供的注解功能、各种主流框架的接入方式。

    下面我将分别使用三种方式为 Dubbo接口 接入限流和熔断的机制。

    在接入之前,我们需要注意的一个点是:要为 Dubbo接口 接入熔断&限流等机制,必须要为 Dubbo接口 指定资源,并且为资源初始化规则;否则只是引入了组件,也无法使用 Sentinel 提供的限流&熔断功能。

    1.1 方式一:sentinel-core组件

    引入依赖:
    引入 sentinel-core 依赖,直接植入硬代码进行熔断限流。

    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-core</artifactId>
        <version>1.8.0</version>
    </dependency>
    

    Controller 代码:

    /**
     * Say Hello
     * @author winfun
     * @date 2020/10/29 5:12 下午
     **/
    @Slf4j
    @RequestMapping("/hello")
    @RestController
    public class HelloController {
    
        public static final String RESOURCE_NAME = "dubboServiceOne";
        @DubboReference(check = false,lazy = true,retries = 0)
        private DubboServiceOne dubboServiceOne;
    
        /**
         * 初始化流控规则和熔断规则
         * ps:因为我们没有接入 Sentinel Dashboard,所以得自己在代码里面设置好
         */
        static{
            // 初始化流控规则
            final List<FlowRule> flowRules = new ArrayList<>();
            final List<DegradeRule> degradeRules = new ArrayList<>();
            // 限流规则
            final FlowRule flowRule = new FlowRule();
            flowRule.setResource(RESOURCE_NAME);
            flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
            // 1 QPS
            flowRule.setCount(1);
            flowRules.add(flowRule);
            // 熔断规则
            final DegradeRule degradeRule = new DegradeRule();
            degradeRule.setResource(RESOURCE_NAME);
            degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
            // 2个异常数
            degradeRule.setCount(2);
            // 时间窗口长度,单位为秒
            degradeRule.setTimeWindow(5);
            // 最小请求数
            degradeRule.setMinRequestAmount(5);
            // 熔断时长:当5秒内,10个请求里面出现2个异常,则进行熔断,熔断时长为10s
            degradeRule.setStatIntervalMs(10000);
            degradeRules.add(degradeRule);
            FlowRuleManager.loadRules(flowRules);
            DegradeRuleManager.loadRules(degradeRules);
        }
    
        @GetMapping("/{name}")
        public ApiResult sayHello(@PathVariable("name") final String name){
            final String hello = this.sayHelloByDubbo2Code(name);
            return ApiResult.success(hello);
        }
    
    
        /***
         * 接入Sentinel方式:植入硬代码进行熔断限流
         * @author winfun
         * @param name name
         * @return {@link ApiResult<String> }
         **/
        private ApiResult<String> sayHelloByDubbo2Code(final String name) {
    
            ApiResult<String> result;
            Entry entry = null;
            try {
                entry = SphU.entry(RESOURCE_NAME);
                result = this.dubboServiceOne.sayHello(name);
            }  catch (BlockException e) {
                if (e instanceof DegradeException){
                    log.error("资源:{} 被熔断了,message is {}",RESOURCE_NAME,e.getMessage());
                    result = ApiResult.fail("hello fallback");
                }else {
                    log.error("资源:{} 被流控了",RESOURCE_NAME);
                    result = ApiResult.fail("hello block");
                }
            } catch (Exception e){
                log.error("业务处理发生异常,exception is {}",e.getMessage());
                // 若需要配置降级规则,需要通过这种方式记录业务异常
                Tracer.traceEntry(e, entry);
                result = ApiResult.fail("exception");
                //throw new RuntimeException("业务处理发生异常");
            } finally {
                // 务必保证 exit,务必保证每个 entry 与 exit 配对
                if (entry != null) {
                    entry.exit();
                }
            }
            return result;
        }
    }
    

    单元测试:
    利用 MockMvc 来进行接口测试,循环调用。

    @AutoConfigureMockMvc
    @SpringBootTest(classes = DubboServiceApplication.class)
    @RunWith(SpringRunner.class)
    public class ServiceOneApplicationTests {
    
        @Autowired
        private MockMvc mockMvc;
    
        @SneakyThrows
        @Test
        public void sayHello(){
            for (int i = 0; i < 100; i++) {
                // 休眠500毫秒,即1秒两次调用,可以触发流控规则
                Thread.sleep(500);
                MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/hello/winfun"))
                        .andReturn();
                System.out.println(result.getResponse().getContentAsString());
            }
        }
    }
    

    在看单元测试的结果前,我们先分析一下我们的流控&熔断配置:

    • 限流,我们是限制每秒只能有1个请求
    • 熔断:在5秒内,如果5个请求中有两个请求是发生异常的,则打开熔断器,熔断时长为10s。
    • 熔断中,当超过熔断时长了,接下来一个请求如果还是发生异常,则继续打开熔断器,否则就是关闭熔断器,恢复正常的请求链路。

    单元测试中,我们是不会开启服务提供者的,先看看控制台打印的日志信息:

    2021-01-27 11:42:18.568 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"exception","data":null,"traceId":null}
    2021-01-27 11:42:19.147 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:42:19.653 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"exception","data":null,"traceId":null}
    2021-01-27 11:42:20.160 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:42:20.666 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"exception","data":null,"traceId":null}
    2021-01-27 11:42:21.172 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:42:21.681 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"exception","data":null,"traceId":null}
    2021-01-27 11:42:22.190 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:42:22.695 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"exception","data":null,"traceId":null}
    2021-01-27 11:42:23.202 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:42:23.709 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"exception","data":null,"traceId":null}
    2021-01-27 11:42:24.215 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:42:24.724 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"exception","data":null,"traceId":null}
    2021-01-27 11:42:25.232 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:42:25.738 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:26.247 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:26.757 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:27.265 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:27.769 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:28.274 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:28.779 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:29.284 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:29.790 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"exception","data":null,"traceId":null}
    2021-01-27 11:42:30.299 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:42:30.803 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:31.309 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:42:31.814 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    // .....
    

    总结:
    我们可以看到,控制台的日志打印是符合我们的预期的:

    • 限流:1秒内会调用两次接口,所以会每两次调用都会触发限流规则。
    • 熔断:服务提供者未启动,即每次 RPC 调用都会出现异常,所以时间窗口内出现一定数量的异常调用,触发了熔断规则。

    并且,流控规则的优先级优先于熔断规则。

    1.2 方式二:使用注解 @SentinelResource

    引入依赖:
    引入 sentinel-annotation-aspectj 依赖。

    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-transport-simple-http</artifactId>
        <version>1.8.0</version>
    </dependency>
    

    注入切面:
    手动注入 SentinelResourceAspect,因为 Sentinel 提供的这个切面,是没有 @Component 等注解的,所以还是得自己注入到 Spring 容器中。

    /**
     * 需要引入一下,因为 Sentile 提供的这个切面是没有加 @Component 注解的
     * @return
     */
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
    

    我们使用这个 @SentinelResource 注解,可直接在 Controller 的方法加注解,也可以自己再弄一个 Service 来加注解,这两个都可以.
    因为我们的 Controller 层就是只调用了 Dubbo 接口,所以就直接在 Controller 的方法上面加注解了,而如果业务是非常多的,那就可以考虑单独弄个 Service 来给 Dubbo 接口再包一层。

    Spring AOP 同时支持 JDK 和 CGLIB 动态代理,SpringBoot 2.x 默认使用 CGLIB 动态代理。

    Controller 代码:

    /**
     * Say Hello
     * @author winfun
     * @date 2020/10/29 5:12 下午
     **/
    @Slf4j
    @RequestMapping("/hello")
    @RestController
    public class HelloController {
    
        public static final String RESOURCE_NAME = "dubboServiceOne";
        @DubboReference(check = false,lazy = true,retries = 0)
        private DubboServiceOne dubboServiceOne;
    
        /**
         * 初始化流控规则和熔断规则
         * ps:因为我们没有接入 Sentinel Dashboard,所以得自己在代码里面设置好
         */
        static{
            // 初始化流控规则
            final List<FlowRule> flowRules = new ArrayList<>();
            final List<DegradeRule> degradeRules = new ArrayList<>();
            // 限流规则
            final FlowRule flowRule = new FlowRule();
            flowRule.setResource(RESOURCE_NAME);
            flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
            // 1 QPS
            flowRule.setCount(1);
            flowRules.add(flowRule);
            // 熔断规则
            final DegradeRule degradeRule = new DegradeRule();
            degradeRule.setResource(RESOURCE_NAME);
            degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
            // 2个异常数
            degradeRule.setCount(1);
            // 时间窗口长度,单位为秒
            degradeRule.setTimeWindow(5);
            // 最小请求数
            degradeRule.setMinRequestAmount(5);
            // 熔断时长:当5秒内,10个请求里面出现2个异常,则进行熔断,熔断时长为10s
            degradeRule.setStatIntervalMs(10000);
            degradeRules.add(degradeRule);
    
            FlowRuleManager.loadRules(flowRules);
            DegradeRuleManager.loadRules(degradeRules);
        }
    
        @GetMapping("/{name}")
        @SentinelResource(value=RESOURCE_NAME,fallback = "sayHelloFallback",blockHandler = "sayHelloBlock")
        public ApiResult sayHello(@PathVariable("name") final String name){
            String hello = this.dubboServiceOne.sayHello(name);
            return ApiResult.success(hello);
        }
    
        /**
         * Fallback 函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
         * @param name
         * @param throwable
         * @return {@link ApiResult<String> }
         */
        public ApiResult<String> sayHelloFallback(final String name, final Throwable throwable){
            log.error("资源:{} 发生异常,message is {}",RESOURCE_NAME,throwable.getMessage());
            return ApiResult.fail("hello exception");
        }
    
        /**
         * BlockHandler 函数
         * blockHandler 函数访问范围需要是 public,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException
         * @param name
         * @param e
         * @return {@link ApiResult<String> }
         */
        public ApiResult<String> sayHelloBlock(final String name, final BlockException e){
            ApiResult<String> result;
            if (e instanceof DegradeException){
                log.error("资源:{} 被熔断了,message is {}",RESOURCE_NAME,e.getMessage());
                result = ApiResult.fail("hello fallback");
            }else {
                log.error("资源:{} 被流控了",RESOURCE_NAME);
                result = ApiResult.fail("hello block");
            }
            return result;
        }
    }
    
    

    bolckHandler 和 fallbackHandler 的返回值都要原方法一致。

    单元测试:

    @AutoConfigureMockMvc
    @SpringBootTest(classes = DubboServiceApplication.class)
    @RunWith(SpringRunner.class)
    public class ServiceOneApplicationTests {
    
        @Autowired
        private MockMvc mockMvc;
    
        @SneakyThrows
        @Test
        public void sayHello(){
            for (int i = 0; i < 100; i++) {
                // 休眠500毫秒,即1秒两次调用,可以出发流控规则
                Thread.sleep(500);
                MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/hello/winfun"))
                        .andReturn();
                System.out.println(result.getResponse().getContentAsString());
            }
        }
    
    }
    

    控制台打印:可以看到和上面使用 Sentinel-core 植入硬代码的打印是一样的

    2021-01-27 11:43:36.995 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:37.560 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:38.098 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:43:38.605 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:39.114 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:43:39.620 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:40.129 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:43:40.638 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:41.144 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:43:41.651 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:42.160 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:43:42.668 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:43.173 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:43:43.679 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:44.185 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:43:44.691 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:45.198 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:43:45.704 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:43:46.208 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:43:46.714 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:43:47.222 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:43:47.729 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:43:48.233 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:43:48.736 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:43:49.247 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:43:49.758 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 发生异常,message is No provider available from registry 127.0.0.1:2181 for service com.winfun.service.DubboServiceOne on consumer 127.0.0.1 use dubbo version 2.7.7, please check status of providers(disabled, not registered or in blacklist).
    {"code":1,"message":"hello exception","data":null,"traceId":null}
    2021-01-27 11:43:50.266 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被流控了
    {"code":1,"message":"hello block","data":null,"traceId":null}
    2021-01-27 11:43:50.773 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    2021-01-27 11:43:51.277 [main] [ERROR] [c.w.c.HelloController] [] [] - 资源:dubboServiceOne 被熔断了,message is null
    {"code":1,"message":"hello fallback","data":null,"traceId":null}
    // .....
    
    
    下一篇:没有了