(六)接口抽取及依赖版本统一
前面的项目,我们会发现有个接口是一样的,我们需要单独抽取出来,统一维护;
以及两个项目的maven依赖包的版本也是一样的,我们也需要单独抽取出来,统一维护;
抽取接口,我们通过新建一个项目,然后其他两个项目依赖这个这个项目即可;
统一依赖包版本,我们可以通过建立parent项目,来统一管理依赖以及版本,子项目继承即可;
我们新建dubbo-demo-api项目,然后把DemoProviderService接口类放到该项目里即可;
我们再新建一个dubbo-demo-parent项目;
pom.xml里:
1 <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com.wishwzp</groupId> 4 <artifactId>dubbo-demo-parent</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>pom</packaging> 7 8 <!-- 依赖版本管理 --> 9 <properties> 10 <dubbo-demo-api.version>0.0.1-SNAPSHOT</dubbo-demo-api.version> 11 <dubbo.version>2.6.0</dubbo.version> 12 <zkclient.version>0.10</zkclient.version> 13 <curator-framework.version>4.0.1</curator-framework.version> 14 <fastjson.version>1.2.46</fastjson.version> 15 <log4j.version>1.2.17</log4j.version> 16 <slf4j-api.version>1.7.25</slf4j-api.version> 17 <commons-lang3.version>3.4</commons-lang3.version> 18 <netty-all.version>4.0.35.Final</netty-all.version> 19 </properties> 20 21 <!-- 依赖管理--> 22 <dependencyManagement> 23 <dependencies> 24 <dependency> 25 <groupId>com.wishwzp</groupId> 26 <artifactId>dubbo-demo-api</artifactId> 27 <version>${dubbo-demo-api.version}</version> 28 </dependency> 29 <dependency> 30 <groupId>com.alibaba</groupId> 31 <artifactId>dubbo</artifactId> 32 <version>${dubbo.version}</version> 33 </dependency> 34 <dependency> 35 <groupId>com.101tec</groupId> 36 <artifactId>zkclient</artifactId> 37 <version>${zkclient.version}</version> 38 </dependency> 39 <dependency> 40 <groupId>org.apache.curator</groupId> 41 <artifactId>curator-framework</artifactId> 42 <version>${curator-framework.version}</version> 43 </dependency> 44 <dependency> 45 <groupId>com.alibaba</groupId> 46 <artifactId>fastjson</artifactId> 47 <version>${fastjson.version}</version> 48 </dependency> 49 <dependency> 50 <groupId>log4j</groupId> 51 <artifactId>log4j</artifactId> 52 <version>${log4j.version}</version> 53 </dependency> 54 <dependency> 55 <groupId>org.slf4j</groupId> 56 <artifactId>slf4j-api</artifactId> 57 <version>${slf4j-api.version}</version> 58 </dependency> 59 <dependency> 60 <groupId>org.apache.commons</groupId> 61 <artifactId>commons-lang3</artifactId> 62 <version>${commons-lang3.version}</version> 63 </dependency> 64 <dependency> 65 <groupId>io.netty</groupId> 66 <artifactId>netty-all</artifactId> 67 <version>${netty-all.version}</version> 68 </dependency> 69 </dependencies> 70 </dependencyManagement> 71 </project>
dubbo-demo-provider项目pom.xml修改:
1 <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <parent> 4 <groupId>com.wishwzp</groupId> 5 <artifactId>dubbo-demo-parent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <artifactId>dubbo-demo-provider</artifactId> 9 10 <dependencies> 11 <dependency> 12 <groupId>com.wishwzp</groupId> 13 <artifactId>dubbo-demo-api</artifactId> 14 </dependency> 15 <dependency> 16 <groupId>com.alibaba</groupId> 17 <artifactId>dubbo</artifactId> 18 </dependency> 19 <dependency> 20 <groupId>com.101tec</groupId> 21 <artifactId>zkclient</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>org.apache.curator</groupId> 25 <artifactId>curator-framework</artifactId> 26 </dependency> 27 <dependency> 28 <groupId>com.alibaba</groupId> 29 <artifactId>fastjson</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>log4j</groupId> 33 <artifactId>log4j</artifactId> 34 </dependency> 35 <dependency> 36 <groupId>org.slf4j</groupId> 37 <artifactId>slf4j-api</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>org.apache.commons</groupId> 41 <artifactId>commons-lang3</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>io.netty</groupId> 45 <artifactId>netty-all</artifactId> 46 </dependency> 47 </dependencies> 48 </project>
类似,dubbo-demo-consumer项目pom.xml修改:
1 <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <parent> 4 <groupId>com.wishwzp</groupId> 5 <artifactId>dubbo-demo-parent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <artifactId>dubbo-demo-consumer</artifactId> 9 10 <dependencies> 11 <dependency> 12 <groupId>com.wishwzp</groupId> 13 <artifactId>dubbo-demo-api</artifactId> 14 </dependency> 15 <dependency> 16 <groupId>com.alibaba</groupId> 17 <artifactId>dubbo</artifactId> 18 </dependency> 19 <dependency> 20 <groupId>com.101tec</groupId> 21 <artifactId>zkclient</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>org.apache.curator</groupId> 25 <artifactId>curator-framework</artifactId> 26 </dependency> 27 <dependency> 28 <groupId>com.alibaba</groupId> 29 <artifactId>fastjson</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>log4j</groupId> 33 <artifactId>log4j</artifactId> 34 </dependency> 35 <dependency> 36 <groupId>org.slf4j</groupId> 37 <artifactId>slf4j-api</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>org.apache.commons</groupId> 41 <artifactId>commons-lang3</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>io.netty</groupId> 45 <artifactId>netty-all</artifactId> 46 </dependency> 47 </dependencies> 48 </project>
测试结果OK;