关于在win10上安装的dubbo的上一篇文章已经说过了,点击跳转java
前沿:本片文章只是使用得普通项目,后期测试得时候使用官方得方法加载相关得配置文件便可!redis
(0):测试项目结构spring
(1):首先你们能够看官网给出的使用示意图:apache
上一篇文章已经说过了,咱们的代码只须要提升服务提供者和服务消费者,另外的两个注册中心和管理控制台,都是环境配置的app
,注册中心是必须的!管理控制台随意(最好安装,方便查看与调试)maven
(2):既然是两个项目以前的通讯,那么咱们局须要创建2个项目,由于在后面的通讯时,服务提供者与服务消费者都是使用暴露的接口来找到实现类,全部官方推荐咱们将bean和相关接口类放在一个专门给暴露的项目中,其它项目可使用dependencyide
来引入改项目,已达到通用的效果!测试
user-service-provider | 服务提供者(这个项目是实现类) |
user-service-consumer | 服务消费者(这个项目是实现类) |
taobao-interface | 用于暴露服务,其中有user-service-provider的接口,与user-service-consumer的接口! |
我这里就创建的3个maven的项目spa
(3):那么接下来,咱们须要在user-service-provider项目和user-service-consumer中使增长dubbo的jar包和zookeeper客户端.net
并将taobao-interface项目引入到这两个项目中去,即pom.xml得文件须要增长以下依赖(还有javassist.jar)
<dependencies> <dependency> <groupId>cn.gxm.taobao</groupId> <artifactId>taobao-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 须要映引入改jar包,不然会初始化失败,报错说没有改jar包 --> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.23.1-GA</version> </dependency> <!-- 第一步引入dubbo依赖 --> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- 第二步引入zookeeper客户端 --> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency>
这里我须要说明得一点就是我使用得是dubbo得2.6.2得jar包,因此zookeeper得客户端是zookeeperorg.apache.curator,可是若是是2.5.0如下得须要使用得zookeeper得客户端是zkclient,即须要更改
(3):接下来咱们须要将服务暴露出去!暴露的是接口,可是接口关联是实现类便可即咱们在user-service-provider得src/main/resorces下创建一个provide.xml得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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系能够随意,可是最好和项目名一致,而且不能重复 --> <dubbo:application name="user-service-provider" /> <!-- 使用zooKeeper广播注册中心暴露服务地址,本机得2181端口, 可是若是是在虚拟机上,根据增长得Ip进行修改便可! 若是使用得不说zookeeper做为注册中心,须要根据你本身得设置 有以下几种: (1)Multicast (2)zookeeper (官方推荐使用) (3)Redis (4)Simple --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务,即在20880端口通讯 name能够根据官方得说明有如下几种 (1)multicast (2)zookeeper (3) redis (4)simple 若是是这个,name能够省略! prot 你们随意!即索命服务提供者和服务消费者在那个端口通讯! --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明须要暴露的服务接口 (这里暴露的是接口) 可是接口关联了实现类,因此等下消费者链接提供者时,使用得就是实体类 --> <dubbo:service interface="cn.gxm.taobao.serviceInterface.UserService" ref="userServiceImpl" /> <!-- 和本地bean同样实现服务 --> <bean id="userServiceImpl" class="cn.gxm.taobao.userService.userServiceImpl" /> </beans>
(4):好了服务提供者已经完成,关于相关实现类我这里就不说了
在user-service-provice中写一个加载配置文件得类!
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.in.read();
(5):首先运行zookeeper(zkservice.cmd)和minitor得jar包(Java -jar dubbo-admin-0.0.1-SNAPSHOT.jar )运行后,登录minitor(http://localhost:7001):会出现一个服务提供者!以下:服务提供者为1,消费者为0!
以上就是服务提供者以完成:接下来就是没法消费者:
(6):完成服务消费者得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:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方同样,一样不能重复 --> <dubbo:application name="order-service-consumer" /> <!-- 使用zookeeper广播注册中心暴露发现服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,使用提供者暴露的接口与以前在provider.xml中暴露得接口要同样! 那么就会使用接口对应得实现类 此时得id。其实很重要,可是们这里是一个小得demo用不到,后面再作更加复杂得项目时 会用到 --> <dubbo:reference id="userService" interface="cn.gxm.taobao.serviceInterface.UserService" /> </beans>
(7):加载consumer.xml运行便可:
package cn.gxm.taobao.orderService; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.gxm.taobao.bean.User; import cn.gxm.taobao.serviceInterface.OrderService; import cn.gxm.taobao.serviceInterface.UserService; public class TestConsumer { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); context.start(); UserService userService = (UserService)context.getBean(UserService.class); User user = userService.getAddressById(1); System.out.println(user.toString()); System.out.println("调用完成"); System.in.read(); } }
(8):结果:
(9):完成!