接上一篇博客Dubbo快速入门 发布服务、启动服务
创建一个maven项目名字为 vhukze-order-dubbo-service
引入与上一篇博客中服务生产者一样的依赖
<dependencies>
<dependency>
<groupId>com.vhukze</groupId>
<artifactId>vhukze-member-public-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- zookeeper客户端连接工具 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- dubbo底层框架基于netty实现 -->
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
</dependency>
<!-- spring框架组件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- dubbo包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
在resources文件夹下创建一个配置文件 consumer.xml
内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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-admin 或 dubbo-monitor 会显示这个名字,方便辨识 -->
<dubbo:application name="order-consumer" />
<!--向zookeeper订阅provider的地址,由zookeeper定时发送 -->
<dubbo:registry address="zookeeper://localhost:2181" />
<!--使用 dubbo 协议调用定义好的 api.PermissionService 接口 -->
<dubbo:reference id="memberService"
interface="com.vhukze.api.member.service.MemberService" />
</beans>
创建一个类在com.vhukze.orderTest包下
package com.vhukze.orderTest;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.vhukze.api.member.service.MemberService;
public class OrderToMemberTest {
public static void main(String[] args) {
//加载配置文件
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("consumer.xml");
//获得对象
MemberService memberService = app.getBean(MemberService.class);
//调用方法
System.out.println("订单服务调用会员服务返回结果:"+memberService.getUser(5l));
}
}
启动zookeeper,启动会员服务(生产者)
运行main方法 结果: