dubbo源码解析(四) api配置之服务消费者

目录

前言:

1、dubbo服务提供者api

2、获取服务对象

3、获取代理对象


前言:

           在前面有关dubbo源码解析的博文中我们分析了api编码的形式配置服务消费者进行了服务的发布,下面我们继续使用api的形式进行暴露服务的消费。

1、dubbo服务提供者api

    /**
     * dubbo 服务消费者api形式进行服务消费
     */
    @Test
    public void consumerDubboService(){
        //声明应用 dubbo生态质检服务调用是基于应用的
        ApplicationConfig application = new ApplicationConfig("dubbo-refrence");
        //涉及注册中心
        RegistryConfig registryCenter = new RegistryConfig();
        registryCenter.setAddress("zookeeper://182.92.189.235:2181");

        //消费者消费
        //设置消费者全局配置
        ConsumerConfig consumerConfig = new ConsumerConfig();
        //设置默认的超时时间
        consumerConfig.setTimeout(1000*5);
        ReferenceConfig<UserService> userConfigReference = new ReferenceConfig<>();
        userConfigReference.setApplication(application);
//        List<RegistryConfig> registryConfigs = new ArrayList<>();
//        registryConfigs.add(registryCenter);
        userConfigReference.setRegistry(registryCenter);
        userConfigReference.setInterface(UserService.class);
        //设置methodConfig 方法级别的dubbo参数包配置 比如方法名必填、重试次数、超时时间、负载均衡策略
        MethodConfig methodConfig = new MethodConfig();
        //方法名必填
        methodConfig.setName("queryUserInfo");
        //超时时间
        methodConfig.setTimeout(1000 * 5);
        //重试次数
        methodConfig.setRetries(3);
        //获取服务(并非真实的对象而是代理对象)
        UserService userService = userConfigReference.get();
        //调用对象方法
        String info = userService.queryUserInfo();
        System.out.println(info);
    }

上述代码核心是构造了一个ReferenceConfig对象 该对应设置了应用信息配置(applicationConfig)、注册中心(RegistryConfig)、全局消费者配置(ConsumerConfig)、方法级别配置(MethodConfig)最终调用ReferenceConfig的get方法获取duubo服务对象进行服务消费。我们核心需要关注其get方法

2、获取服务对象

   ReferenceConfig的get方法最终会调用其对应的init()方法进行dubbo服务代理对象的创建,最终业务代码调用代理对象的方法进行服务消费下面来关注一下init()方法

private void init() {
    //如果该实例已经初始化 则返回
    if (initialized) {
        return;
    }
    //标记该实例初始化(先占坑) 防止重复实例化
    initialized = true;
    //消费接口非空校验
    if (interfaceName == null || interfaceName.length() == 0) {
        throw new IllegalStateException("<dubbo:reference interface=\"\" /> interface not allow null!");
    }
    //获取全局服务消费者配置信息(ConsumerConfig) 逻辑:获取该对象配置的ConsumerConfig对象 并将系统属性中
    //涉及到服务消费者dubbo.consumer.xxx 相关的配置属性通过setXxx方法填充到ConsumerConfig对象中
    checkDefault();
    //服务消费者dubbo.reference.xxx 相关的配置属性通过setXxx方法填充到ReferenceConfig对象中
    appendProperties(this);
    //设置是否泛化接口标识
    if (getGeneric() == null && getConsumer() != null) {
        setGeneric(getConsumer().getGeneric());
    }
    //如果是泛化接口 class为GenericService
    if (ProtocolUtils.isGeneric(getGeneric())) {
        interfaceClass = GenericService.class;
    } else {
        try {
            //普通接口 反射获取class
            interfaceClass = Class.forName(interfaceName, true, Thread.currentThread()
                    .getContextClassLo
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值