dubbo的服务调用原理

一、前言

在我们使用dubbo进行远程服务消费时,可以通过@Reference注解或dubbo:reference来配置引用的接口,最终会生成远程服务的代理类,转化成ReferenceBean,这样我们就可以像调用本地接口方法一样使用远程服务提供的功能;

二、实现服务调用的类;

ReferenceBean

public class ReferenceBean<T> extends ReferenceConfig<T> implements FactoryBean, ApplicationContextAware, InitializingBean, DisposableBean {
   

    @Override
    public Object getObject() throws Exception {
   
        return get();
    }
    
        public synchronized T get() {
   
        if (destroyed) {
   
            throw new IllegalStateException("Already destroyed!");
        }
        if (ref == null) {
   
        //此方法是ReferenceConfig的init();
            init();
        }
        return ref;
    }

获取被Reference标识的类的代理对象,从getObject开始,之后进入init()进行初始化

PS:如果第一次debug时,发现ref不为空,进入不了init()方法,可以参考:ref==null

ReferenceConfig

 private void init() {
   
    //判断是否已经初始化,因为是第一次执行,所以是false
        if (initialized) {
   
            return;
        }
        initialized = true;
        if (interfaceName == null || interfaceName.length() == 0) {
   
            throw new IllegalStateException("<dubbo:reference interface=\"\" /> interface not allow null!");
        }
           // get consumer's global configuration
        checkDefault();
        appendProperties(this);
        if (getGeneric() == null && getConsumer() != null) {
   
            setGeneric(getConsumer().getGeneric());
        }
        if (ProtocolUtils.isGeneric(getGeneric())) {
   
            interfaceClass = GenericService.class;
        } else {
   
            try {
   
            //加载接口的class文件
                interfaceClass = Class.forName(interfaceName, true, Thread.currentThread()
                        .getContextClassLoader());
            } catch (ClassNotFoundException e) {
   
                throw new IllegalStateException(e.getMessage(), e);
            }
            checkInterfaceAndMethods(interfaceClass, methods);
        }
        //......省略了很多检测配置文件的方法(不是此方法的重点,所以不分析)
        Map<String, String> map = new HashMap<String, String>();
        //map存放配置属性,可见下图
        Map<Object, Object> attributes = new HashMap<Object, Object>();
        map.put(Constants.SIDE_KEY, Constants.CONSUMER_SIDE);
        map.put(Constants.DUBBO_VERSION_KEY, Version.getVersion());
        map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
        if (ConfigUtils.getPid() > 0) {
   
            map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));
        }
        if (!isGeneric()) {
   
            String revision = Version.getVersion(interfaceClass, version);
            if (revision != null && revision.length() > 0) {
   
                map.put("revision", revision);
            }
              //获取interfaceClass的方法名;
            String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames();
            if (methods.length == 0) {
   
                logger.warn("NO method found in service interface " + interfaceClass.getName());
                map.put("methods", Constants.ANY_VALUE);
            } else {
   
                map.put("methods", StringUtils.join(new HashSet<String>(Arrays.asList(methods)), ","));
            }
        }
        map.put(Constants.INTERFACE_KEY, interfaceName);
        //把相关配置属性加入 AbstractConfig中;
        appendParameters(map, application);
        appendParameters(map, module);
        appendParameters(map, consumer, Constants.DEFAULT_KEY);
        appendParameters(map, this);
        String prefix = StringUtils.getServiceKey(map);
        if (methods != null && !methods.isEmpty()) {
   
            for (MethodConfig method : methods) {
   
                appendParameters(map, method, method.getName());
                String retryKey = method.getName() + ".retry";
                if (map.containsKey(retryKey)) {
   
                    String retryValue = map.remove(retryKey);
                    if ("false".equals(retryValue)) {
   
                        map.put(method.getName() + ".retries", "0");
                    }
                }
                appendAttributes(attributes, method, prefix + "." + method.getName());
           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值