一、建立maven模块和provider、consumer、service子模块,其中service是开发接口的模块
建立一个maven模块,不选择样板,直接next知道完成,建立三个子模块,建立完后发现各个模块的java目录不是源目录
右键——>make Directory as
provider模块和consumer模块都依赖service模块,需要导入依赖
二、在父模块导入所需的最少jar包
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version><exclusions><exclusion><artifactId>spring</artifactId><groupId>org.springframework</groupId></exclusion></exclusions></dependency></dependencies>
三、写provider的spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="demo2"></dubbo:application><dubbo:registry address="multicast://224.5.6.7:1234"></dubbo:registry><dubbo:protocol name="dubbo" port="20880"></dubbo:protocol><dubbo:service interface="com.dr.RegistryService" ref="registryService"></dubbo:service><bean id="registryService" class="com.dr.StuRegistryService"></bean> </beans>
有几点需要注意的地方:
1.http://code.alibabatech.com/schema/dubbo这个地方和官网的不一样,因为使用的版本不同,需根据dubbo的jar包中的dubbo.xsd确定
2.<dubbo:registry address="multicast://224.5.6.7:1234"/>,水平有限,发现不用这个地址配置,就运行错误,官网这样解释:
The demo uses multicast as the registry since it is simple and does not require to extra installation
使用多播地址作为注册处,这样比较简单,不用额外安装
剩下的在dubbo官网有,自己的gitHub做了一点修改之后也可以直接运行,博客作为个人的学习笔记
gitHub地址:https://github.com/dengrongrong/my-dubbo-demo1