创建父工程 i-cloud

image.png
image.png
项目结构如下:
image.png
该模块是所有项目的父模块,仅用于聚合子模块,将src目录下的内容删除,只保留pom.xml,然后修改pom.xml,
内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.imcode</groupId>
  7. <artifactId>i-cloud</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>pom</packaging>
  10. <properties>
  11. <spring-boot.version>2.3.2.RELEASE</spring-boot.version>
  12. <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
  13. <spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version>
  14. </properties>
  15. <dependencyManagement>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-dependencies</artifactId>
  20. <version>${spring-boot.version}</version>
  21. <type>pom</type>
  22. <scope>import</scope>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-dependencies</artifactId>
  27. <version>${spring-cloud.version}</version>
  28. <type>pom</type>
  29. <scope>import</scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>com.alibaba.cloud</groupId>
  33. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  34. <version>${spring-cloud-alibaba.version}</version>
  35. <type>pom</type>
  36. <scope>import</scope>
  37. </dependency>
  38. </dependencies>
  39. </dependencyManagement>
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. <configuration>
  46. <fork>true</fork>
  47. <addResources>true</addResources>
  48. </configuration>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. </project>

创建公共模块 i-common

2.2 开发环境搭建 - 图4

2.2 开发环境搭建 - 图5

2.2 开发环境搭建 - 图6

完成后项目结构如下:
2.2 开发环境搭建 - 图7

注意,将模块创建在和父工程同级目录,方便团队协作和版本管理。

i-cloud 模块 pom.xml 自动添加了如下模块引用:
2.2 开发环境搭建 - 图8

i-common 模块引入公共依赖,pom.xml 的内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <parent>
  7. <artifactId>i-cloud</artifactId>
  8. <groupId>com.imcode</groupId>
  9. <version>1.0-SNAPSHOT</version>
  10. <relativePath>../i-cloud/pom.xml</relativePath>
  11. </parent>
  12. <modelVersion>4.0.0</modelVersion>
  13. <artifactId>i-common</artifactId>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-security</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-oauth2</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.projectlombok</groupId>
  29. <artifactId>lombok</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-devtools</artifactId>
  34. <scope>runtime</scope>
  35. <optional>true</optional>
  36. </dependency>
  37. </dependencies>
  38. <build>
  39. <plugins>
  40. <plugin>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-maven-plugin</artifactId>
  43. <configuration>
  44. <fork>true</fork>
  45. </configuration>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>

创建微服务聚合工程 i-server

因为存在多个微服务提供者,所以我们先新增一个 i-server 作为这些微服务的父项目,统一进行管理。
2.2 开发环境搭建 - 图9

2.2 开发环境搭建 - 图10

2.2 开发环境搭建 - 图11

2.2 开发环境搭建 - 图12

删除 i-server 下的src 文件夹,pom.xml 中引入i-common 依赖:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <parent>
  7. <artifactId>i-cloud</artifactId>
  8. <groupId>com.imcode</groupId>
  9. <version>1.0-SNAPSHOT</version>
  10. </parent>
  11. <modelVersion>4.0.0</modelVersion>
  12. <artifactId>i-server</artifactId>
  13. <packaging>pom</packaging>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.imcode</groupId>
  17. <artifactId>i-common</artifactId>
  18. <version>1.0-SNAPSHOT</version>
  19. </dependency>
  20. </dependencies>
  21. </project>