Dubbo 常见问题与解答

版权声明:本文为博主原创文章,未经博主允许不得转载。http://mp.blog.csdn.net/configure#i https://blog.csdn.net/wangming520liwei/article/details/87966224

1.dubbo中"读接口"和"写接口"有什么区别?

2.谈谈dubbo中的负载均衡算法及特点?

轮询,随机,最新活跃数,一致性hash算法

最小活跃数算法中是如何统计这个活跃数的?

    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        int length = invokers.size(); // Number of invokers
        int leastActive = -1; // The least active value of all invokers
        int leastCount = 0; // The number of invokers having the same least active value (leastActive)
        int[] leastIndexs = new int[length]; // The index of invokers having the same least active value (leastActive)
        int totalWeight = 0; // The sum of weights
        int firstWeight = 0; // Initial value, used for comparision
        boolean sameWeight = true; // Every invoker has the same weight value?
        for (int i = 0; i < length; i++) {
            Invoker<T> invoker = invokers.get(i);
            int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive(); // Active number
            int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT); // Weight
            if (leastActive == -1 || active < leastActive) { // Restart, when find a invoker having smaller least active value.
                leastActive = active; // Record the current least active value
                leastCount = 1; // Reset leastCount, count again based on current leastCount
                leastIndexs[0] = i; // Reset
                totalWeight = weight; // Reset
                firstWeight = weight; // Record the weight the first invoker
                sameWeight = true; // Reset, every invoker has the same weight value?
            } else if (active == leastActive) { // If current invoker's active value equals with leaseActive, then accumulating.
                leastIndexs[leastCount++] = i; // Record index number of this invoker
                totalWeight += weight; // Add this invoker's weight to totalWeight.
                // If every invoker has the same weight?
                if (sameWeight && i > 0
                        && weight != firstWeight) {
                    sameWeight = false;
                }
            }
        }
        // assert(leastCount > 0)
        if (leastCount == 1) {
            // If we got exactly one invoker having the least active value, return this invoker directly.
            return invokers.get(leastIndexs[0]);
        }
        if (!sameWeight && totalWeight > 0) {
            // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
            int offsetWeight = random.nextInt(totalWeight);
            // Return a invoker based on the random value.
            for (int i = 0; i < leastCount; i++) {
                int leastIndex = leastIndexs[i];
                offsetWeight -= getWeight(invokers.get(leastIndex), invocation);
                if (offsetWeight <= 0)
                    return invokers.get(leastIndex);
            }
        }
        // If all invokers have the same weight value or totalWeight=0, return evenly.
        return invokers.get(leastIndexs[random.nextInt(leastCount)]);
    }

代码中一共两部分,一部分是活跃数和权重的统计, 一部分是 invoker的选择,也就是把最小活跃数的invoker统计到leastIndex数组中,如果权重一致,或者权重为0 ,则均等随机调用

如何理解最小活跃调用?

每个服务有一个活跃计数器,那么我们假如有A,B两个提供者.计数均为0.当A提供者开始处理请求,该计数+1,此时A还没处理完,当处理完后则计数-1.而B请求接收到请求处理得很快.B处理完后A还没处理完,所以此时A,B的计数为1,0.那么当有新的请求来的时候,就会选择B提供者(B的活跃计数比A小).这就是文档说的,使慢的提供者收到更少请求

总的来说就是,选最小活跃数的节点加入到最小活跃数的数组中, 如果最小活跃数中只有一个,直接调用,如果有多个,权重是否一致,如果一致,随机调用invoker,如果不一致,那么进行 那么根据权重比例进行调用

活跃数的变化是在com.alibaba.dubbo.rpc.filter.ActiveLimitFilter中,如果没有配置dubbo:referenceactives属性,默认是调用前活跃数+1,调用结束-1,鉴于很多人可能没用过这个属性,所以我把文档截图贴出来

另外如果使用该种负载均衡算法,则dubbo:service中还需要配置filter="activelimit"

3.简单谈谈你对一致性哈希算法的认识?

构建成一个环形的hash ,将key值映射到 2^32-1 首尾相连的环上

一致性Hash,相同参数的请求总是发到同一个提供者

4.服务发布过程中做了哪些事?

5.dubbo都有哪些协议,他们之间有什么特点,缺省值是什么?

Injvm ,dubbo协议,默认的

6.什么是本地暴露和远程暴露,他们的区别?

本地暴露不需要调用zk来进行通讯,是暴露在同一个JVM中的,同一个JVM中,自己调用自己的接口,不需要通过网络调用

7.服务提供者能实现失效踢出是根据什么原理?

8.讲讲dubbo服务暴露中本地暴露,并画图辅助说明?

9.一般选择什么注册中心,还有别的选择吗?

10.dubbo中zookeeper做注册中心,如果注册中心集群都挂掉,那发布者和订阅者还能通信吗?

不会

notify 有变更会更新 properties文件

初始化

能 ,cache文件

11.项目中有使用过多线程吗?有的话讲讲你在哪里用到了多线程?(面试高频题)

12.zookeeper的java客户端你使用过哪些?

zkClient

13.服务提供者能实现失效踢出是什么原理?(高频题)

Zookeeper 发布订阅

14.zookeeper的有哪些节点,他们有什么区别?讲一下应用场景。

临时节点,永久节点

15.画一画服务注册与发现的流程图。

16.在dubbo中,什么时候更新本地的zookeeper信息缓存文件?订阅zookeeper信息的整体过程是怎么样的?

注册的时候加载,订阅的时候更新,异常的时候读取cache内容链接

17.谈一下你们项目架构设计(很多人在回答这个的时候都容易回答SSH或者SSM,注意,所谓是SSH这些是技术选型,不是架构的设计)

18.既然你们项目用到了dubbo,那你讲讲你们是怎么通过dubbo实现服务降级的,降级的方式有哪些,又有什么区别?

19.dubbo监控平台能够动态改变接口的一些设置,其原理是怎样的?

20.既然你说你看过dubbo源码,那讲一下有没有遇到过什么坑?(区分度高,也是检验是否看过源码的试金石)

21.dubbo的原理是怎么样的?请简单谈谈

22.有没有考虑过自己实现一个类似dubbo的RPC框架,如果有,请问你会如果着手实现?(面试高频题,区分度高)

23.

你说你用过mybatis,那你知道Mapper接口的原理吗?(如果回答得不错,并且提到动态代理这个关键词会继续往下问,那这个动态代理又是如何通过依赖注入到Mapper接口的呢?)

24.描述一下dubbo服务引用的过程,原理

25既然你提到了dubbo的服务引用中封装通信细节是用到了动态代理,那请问创建动态代理常用的方式有哪些,他们又有什么区别?dubbo中用的是哪一种?(高频题)

26.除了JDK动态代理和CGLIB动态代理外,还知不知道其他实现代理的方式?(区分度高)

27.你是否了解spi,讲一讲什么是spi,为什么要使用spi?

28.对类加载机制了解吗,说一下什么是双亲委托模式,他有什么弊端,这个弊端有没有什么我们熟悉的案例,解决这个弊端的原理又是怎么样的?

29.既然你对spi有一定了解,那么dubbo的spi和jdk的spi有区别吗?有的话,究竟有什么区别?

30.你提到了dubbo中spi也增加了IoC,那你先讲讲Spring的IoC,然后再讲讲dubbo里面又是怎么做的?

31.你提到了dubbo中spi也增加了AOP,那你讲讲这用到了什么设计模式,dubbo又是如何做的?

https://www.jianshu.com/p/53feb7f5f5d9

猜你喜欢

转载自blog.csdn.net/wangming520liwei/article/details/87966224