1.准备工作
当下稍微上点规模的项目,都在使用maven多模块开发模式,细分到具体功能模块,有些模块还需要单独部署
按照dubbo架构的设计,基础的项目模块划分如下:
- 服务接口模块(xe-base)
- 服务提供者(xe-provider)
- 服务消费者api(xe-web)
如果使用一个应用来管理的话,任何一点代码的修改,整个项目都需要重新编译,多模块设计则不需要,只需要配置好依赖关系,就可以实现模块间的解耦合。这样的设计才是遵循“高内聚,低耦合”设计原则的。
2.创建maven多模块项目
使用IDEA来创建Maven多模块项目
2.1.创建一个普通Maven项目
1.选择maven项目,选择Project SDK 版本,这里是自定义目录的jdk1.8版本,
2.输入GroupId,ArtifactId和Project Name(dubbo-maven-01),最后点击finish
3.删除src目录即可,需要检查的是pom
2.2.给Maven项目添加模块
2.2.1.基础子模块xe-base
新建一个Module模块xe-base,右键单击项目名,选择module
选择maven,选择默认的父工程dubbo-maven-01,输入GroupId,ArtifactId和Module Name(xe-base),最后点击finish
由于是jar包模块,删除不需要的resource目录
2.2.2.子模块xe-provider和xe-web
与构建基础子模块xe-base一致,唯一不同地方是,选择的是Spring Initializer,而不是maven,因为provider是服务提供者,打算基于springboot构建。剩下的都一样,Module Name(xe-provider),maven构建,java版本是1.8.springboot的版本是2.4.8
删除src下的test目录,单元测试暂时用不到
2.3.各个pom.xml整理
dubbo-maven-01的pom.xml内容如下:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.8</version>
</parent>
<groupId>com.xe</groupId>
<artifactId>dubbo-maven-01</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>xe-base</module>
<module>xe-provider</module>
<module>xe-web</module>
</modules>
<properties>
<xe.version>1.0.0</xe.version>
<springboot.version>2.4.8</springboot.version>
<java.version>1.8</java.version>
<dubbo.starter.version>0.2.0</dubbo.starter.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.xe.base</groupId>
<artifactId>xe-base</artifactId>
<version>${xe.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
xe-provider的pom.xml文件
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xe</groupId>
<artifactId>dubbo-maven-01</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.xe.provider</groupId>
<artifactId>xe-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.xe.base</groupId>
<artifactId>xe-base</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 自动配置管理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 在编写yaml配置文件内容关联自定义Bean类属性的时候,存在提示的功能,
但是在打包插件中去掉自动配置提示的依赖,因为没啥用,会导致jdk加载变慢-->
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
<mainClass>com.xe.provider.XeProviderApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
xe-web的pom.xml文件
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xe</groupId>
<artifactId>dubbo-maven-01</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.xe.web</groupId>
<artifactId>xe-web</artifactId>
<dependencies>
<dependency>
<groupId>com.xe.base</groupId>
<artifactId>xe-base</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 自动配置管理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 在编写yaml配置文件内容关联自定义Bean类属性的时候,存在提示的功能,
但是在打包插件中去掉自动配置提示的依赖,因为没啥用,会导致jdk加载变慢-->
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
<mainClass>com.xe.web.XeWebApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
打包双击package即可