1、首先有两个maven工程,一个提供接口,一个消费接口
需要一个注册中心(这里使用zookeeper)
2 介绍一下dubbo的整个流程
3、改造提供者工程
(1)首先引入dubbo需要的jar包
如下图
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.42.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
(2)创建暴露接口信息的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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.itsm.core.service" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="contractManagement" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.233.238:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.itsm.core.service.biz.HelloWorldService" ref="helloWorldService" />
<!-- 和本地bean一样实现服务 -->
<bean id="helloWorldService" class="com.itsm.core.service.biz.impl.HelloWorldServiceImlp" />
</beans>
(3)在web.xml中引入上面的dubbo-provider.xml文件,或者在其他已经引入的xml文件中导入上面的.xml
(4)创建接口和实现类(自己具体的业务自己去实现即可)
提供者以上基本配置完毕
4、改造消费者工程
(1)同样导入需要的jar包(和提供者一样)
(2)编辑消费者的.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置controller扫描 -->
<!-- <context:component-scan base-package="com.itsm.core.service" />-->
<!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
<dubbo:application name="consumer-contractManagement" />
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!--点对点的方式-->
<!--<dubbo:registry address="N/A" />-->
<dubbo:registry address="zookeeper://192.168.233.238:2181" check="false"/>
<!--生成一个远程服务的调用代理-->
<dubbo:reference id="helloWorldService"
interface="com.itsm.core.service.biz.HelloWorldService" check="false"/>
</beans>
(3)、引入上面的dubbo.consumer.xml文件
(4)、编写接口,不需要写实现
(5)、编写controller,从前台调用测试
5、搭建注册中心
6、启动测试