dubbo服务引用一之客户端配置

  • 修改相应的dubbo.properties,启动com.alibaba.dubbo.demo.provider.DemoProvider做为服务提供方。
  • 修改相应的dubbo.properties,调试com.alibaba.dubbo.demo.consumer.DemoConsumer做为服务引用方。

一、 consumer端服务引用配置

1.一、服务提接口pom坐标

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-demo-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

1.二、dubbo:reference标签

基于dubbo.xsd中描述的扩展spring schema类配置xmljava

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方同样 -->
    <dubbo:application name="demo-consumer"/>

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->

    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 生成远程服务代理,能够和本地bean同样使用demoService -->
    <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>

    <bean class="com.alibaba.dubbo.demo.consumer.DemoAction" init-method="start">
        <property name="demoService" ref="demoService"/>
    </bean>

</beans>

1.三、注入代码

经过set的方式来注入DemoService属性【spring Ioc的一种注入方式】spring

public class DemoAction {

    private DemoService demoService;

    public void setDemoService(DemoService demoService) {
        this.demoService = demoService;
    }
    
    ...
}

二、问题

这一些列操做完了,在conusmer端,咱们并无看到DemoService的任何实现类。那么问题来了,segmentfault

2.一、如何得到DemoService实例的

https://segmentfault.com/a/11...,咱们能够类比得知,dubbo:reference会映射成一个ReferenceBeanapi

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

    private static final long serialVersionUID = 213195494150089726L;

    private transient ApplicationContext applicationContext;

    public ReferenceBean() {
        super();
    }
    ...
    public Object getObject() throws Exception {
        return get();
    }
    
    public Class<?> getObjectType() {
        return getInterfaceClass();
    }

    @Parameter(excluded = true)
    public boolean isSingleton() {
        return true;
    }
    ...
}

发现ReferenceBean实现了FactoryBean接口,FactoryBean是一个Java Bean,可是它是一个能生产对象的工厂Bean。经过FactoryBean#getObject咱们就能够拿到该对象的实例。缓存

public interface FactoryBean<T> {  
        //返回由FactoryBean建立的bean实例,若是isSingleton()返回true,则该实例会放到Spring容器中单实例缓存池中。  
   T getObject() throws Exception;    
   //返回FactoryBean建立的bean类型。  
   Class<?> getObjectType();  
   //返回由FactoryBean建立的bean实例的做用域是singleton仍是prototype。  
   boolean isSingleton();      
}

2.二、得到的DemoService实例是啥

那么FactoryBean#getObject到底都发生了啥,下面咱们来粗略的看一下。app

相关文章
相关标签/搜索