Dubbo之旅--Consumer示例

      我们已经对提供者示例进行了详细的阐述,接下来的消费者相对而言比较简单.

 

     1 配置相关pom.xml信息,跟提供者类似,需要相关环境的java包应用.

 

 

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>

       <artifactId>dubbo-demo</artifactId>

       <groupId>org.pactera.dubbo</groupId>

       <version>1.0-SNAPSHOT</version>

    </parent>

   <modelVersion>4.0.0</modelVersion>

 

   <groupId>org.pactera.dubbo</groupId>

   <artifactId>dubbo-demo-consumer</artifactId>

    <packaging>jar</packaging>

 

   <name>dubbo-demo-consumer</name>

   <url>http://maven.apache.org</url>

 

    <properties>

       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

 

    <dependencies>

        <dependency>

           <groupId>org.pactera.dubbo</groupId>

           <artifactId>dubbo-demo-api</artifactId>

           <version>1.0-SNAPSHOT</version>

        </dependency>

        <dependency>

           <groupId>org.pactera.dubbo</groupId>

           <artifactId>dubbo-expand-loadbalance</artifactId>

           <version>1.0-SNAPSHOT</version>

        </dependency>

        <dependency>

           <groupId>com.alibaba</groupId>

           <artifactId>dubbo</artifactId>

           <version>2.4.10</version>

        </dependency>

        <dependency>

           <groupId>com.101tec</groupId>

           <artifactId>zkclient</artifactId>

            <version>0.3</version>

        </dependency>

        <dependency>

           <groupId>junit</groupId>

           <artifactId>junit</artifactId>

           <version>3.8.1</version>

            <scope>test</scope>

        </dependency>

    </dependencies>

 

    </project>

 

 

2 消费者相关的配置文件如下:

 

/dubbo-demo/dubbo-demo-consumer/src/main/resources/dubbo-demo-consumer.xml

 

 

<?xmlversion="1.0" encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://code.alibabatech.com/schema/dubbo

        http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        ">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样-->

    <dubbo:applicationname="consumer-of-helloworld-app" />

 

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->

<!--     <dubbo:registryaddress="multicast://224.5.6.7:1234" /> -->

   

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

    <dubbo:protocol name="dubbo"port="20884" />

<dubbo:monitorprotocol="registry"></dubbo:monitor>

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService-->

    <dubbo:referenceid="demoService" interface="org.pactera.dubbo.DemoService"retries="0" timeout="30000" />

   </beans>

 

 

        这里跟提供者不一样的地方,在生成的远程服务代理,可以像调用本地的bean一样使用提供者服务demoService.很好的跟Spring融合在一起.

 

 

 

3  接下来我们需要模拟一个消费者的行为,此行为用来调用demoService服务,从而实现相关的功能,如下文件:

 

 

/dubbo-demo/dubbo-demo-consumer/src/main/java/org/pactera/dubbo/DemoServiceConsumer.java

 

 

packageorg.pactera.dubbo;

 

importcom.alibaba.dubbo.rpc.RpcContext;

importlombok.extern.slf4j.Slf4j;

importorg.pactera.dubbo.model.Count;

importorg.pactera.dubbo.thread.DemoServiceConsumerThread;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

 

importjava.util.concurrent.CountDownLatch;

 

@Slf4j

publicclass DemoServiceConsumer {

 

    public static void main(String[] args)throws Exception {

        ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext(new String[] {"dubbo-demo-consumer.xml" });

        context.start();

        DemoService demoService = (DemoService)context.getBean("demoService");

        /** 单个测试 */

        String resInfo =demoService.sayHello("world");

        String serverIP =RpcContext.getContext().getRemoteHost();

        int serverPort =RpcContext.getContext().getRemotePort();

        System.out.println("rspInfo:" + resInfo + ", response from provider: " + serverIP +"," + serverPort);

 

           }

 

}

 

 

启动DemoServiceConsumer模拟一个单次的消费过程.这里输入world.我们可以看一下消费者日志:

 

计算机生成了可选文字:(Java Application) (2015*3E30E ice'); of factory hierarchy o. s. b.f.s.Defau1tListab1eBeanFactory s"rcd instance of s in sleton sin sin sin o, s. b.f. s,Defau1tListab1cBe o, s. b.f. s,Defau1tListab1cB sin g Icton 214) 214) 214) 214) Retu"ins Rctu"ins instanc of instanc of instanc of instanc of an ctory ctory ctory • anfis. can ctory try. 214) C matins ctory an ctory crony rearing instance of o, s. b.f. s , DefaultListab1cBe Easv•ly caching to DEBUG Invoki S o.s.b.f.s.Defau1tListab1eBeanFa n with o. s. b.f. s, DefaultListab1cBeanFactory (AbstractBeanFactory.java: sin sin sin o. s. b.f. s,Defau1tListab1cBe o, s. b.f. s,Defau1tListab1cB 214) Rctu"ins cach-cd instance of 214) Retu"ins instance of 214) Retu"ins instance of SnFactory.jwa:41L) 214) Rctu"ins cach-cd instance of singleton 214) g 214) Rctu"i g an ctory • anfis. can ctory • anfis. ctory cmatins instance O O h d instance of singleton h d i stance of singleton s b.f. s, DefaultListab1cBeanFactory (AbstractBeanFactory.java: s b.f. s. DefaultListab1eBeanFactory r,se f. 192.168. L.Iø9,2eS81 of

 

 5    此时提供者端收到消费者的请求,提供者日志如下:

 

 

计算机生成了可选文字:Bootstrap (1) 2ø15-ø3-3ø START me) an ctory creating instance of Returnins of singleton aultsLng D etc r -y to for n ctor•y o. s. b.f. (AbstractAut n 'org. 2ø15-ø3-3ø DEBUG Eagerly ular Ce.octstrap 2ø15-ø3-3ø creatins 2ø1s.øY.yø Finished 2ø15-ø3-3ø INFO o. s. b.f. (AbstractAut Insta ce of bean • dubbo' re-instantiating in • . s . support. O efauLtListableyeanFactcry@a4ffcS: defining cnflg, 2ø1s-øy-yø nFacWW. 2ø15-ø3-3ø 2ø15-ø3-3ø 2ø1s-øy-3ø 2B15-g3-3g dub be. BUG e rning BUG cached Instance of singleton instance of singleton • duSW instance of singleton strap STARTING) 'Abstract30 a: 2141 R tu ; Returning a•2L41 Retu n ble table r ing ached •org. . dubbo.DwSwice • ae.nF.ctory 3eanFactory C Abstract3u java:2141 Retu æqlnfo: mrLd, for. h d instance of singleton t rted -

 

         到此为止,我们基本上完成了对DubboProviderCustomer的示例,而在实际的项目应用中也是在此基础之上不断的增加服务,基本上的原理和配置本篇和上篇的示例基本上可以满足需求,但是还有一些特殊的需求和在实际开发项目中遇到的问题,这些就不够了,还需要对Dubbo的一些深层的配置和属性进行了解.

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值