标签:
我们已经对提供者示例进行了详细的阐述,接下来的消费者相对而言比较简单.
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);
}
}
4 启动DemoServiceConsumer模拟一个单次的消费过程.这里输入world.我们可以看一下消费者日志:
5 此时提供者端收到消费者的请求,提供者日志如下:
到此为止,我们基本上完成了对Dubbo的Provider和Customer的示例,而在实际的项目应用中也是在此基础之上不断的增加服务,基本上的原理和配置本篇和上篇的示例基本上可以满足需求,但是还有一些特殊的需求和在实际开发项目中遇到的问题,这些就不够了,还需要对Dubbo的一些深层的配置和属性进行了解.
标签:
原文地址:http://blog.csdn.net/jnqqls/article/details/44762637