现在网络上已经有了服务,接下来创建一个消费该服务的工程helloxmldirectconsumer,一共要创建5个文件,创建顺序和功能如下表:
创建顺序 文件名 作用1 pom.xml 工程的pom文件
2 src/main/java/com/bolingcavalry/helloxmldirectconsumer/ConsumerApplication.java 启动、调用远端服务、再结束自身进程
3 src/main/resources/log4j.properties 日志配置文件
4 src/main/resources/dubbo.properties dubbo配置文件
5 src/main/resources/spring/dubbo-consumer.xml spring的bean配置
完整的文件位置如下图:
接下来逐个创建上述文件;
创建名为helloxmldirectconsumer的子工程,pom.xml内容如下,可见刚才新建的二方库practiceinterface也被依赖了:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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 "> <parent> <artifactId>dubbopractice</artifactId> <groupId>com.bolingcavalry</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.bolingcavalry</groupId> <artifactId>helloxmldirectconsumer</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.bolingcavalry</groupId> <artifactId>practiceinterface</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> </dependencies> </project>编写唯一的java文件ConsumerApplication.java,里面用了最简单的方法初始化spring环境,然后取得服务实例,执行过方法后结束进程:
package com.bolingcavalry.helloxmldirectconsumer; import com.bolingcavalry.dubbopractice.service.DemoService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ConsumerApplication { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml"); context.start(); DemoService demoService = context.getBean("demoService", DemoService.class); String hello = demoService.sayHello("world1"); System.out.println("result: " + hello); } }日志配置文件log4j.properties,内容如下:
###set log levels### log4j.rootLogger=info, stdout ###output to console### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n在同样位置创建dubbo配置文件dubbo.properties,内容很简单只有qos的端口设置,用于支持telnet命令,本例中是用不上的,因为远程调用后进程就会结束:
dubbo.application.qos.port=33333在resources目录下新建文件夹spring,在此文件夹下创建文件dubbo-consumer.xml,要重点关注的是dubbo:registry的配置,其address属性值为multicast://224.5.6.7:1234?unicast=false,代表当前服务通过广播让消费者获得自身信息,unicast=false表示多个消费者都能收到广播:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <dubbo:application/> <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false"/> <dubbo:reference check="false" interface="com.bolingcavalry.dubbopractice.service.DemoService" timeout="2000"/> </beans>