SpringCloud多模块项目搭建

多模块Maven项目

为了便于统一的版本维护和管理,经常会用到Maven的多模块模式。

本文以以下模块结构为例,对如何创建多模块Maven项目进行描述。 
这里写图片描述

构建主工程

首先创建一个Maven项目作为主工程,类型无所谓,这里建议使用maven-archetype-quickstart骨架,创建过程如下:

  • File-->New-->Project
  • -->Maven-->Create from archetype-->maven-archetype-quickstart-Next
  • -->GroupId={你的GroupId}-->AritifactId={你的ArtifactId}
  • -->Next-->Next-->Finish-->New Whindow

本示例中,我的主项目叫main-projectGroupIdAritifactId的配置如下: 
这里写图片描述

构建子模块

子模块项目创建于主工程之内,创建过程如下:

  • 右键点击项目名称-->New-->Module
  • 选中Spring Initializr-->Next
  • -->Group={主工程的GroupId}-->Aritifact={当前模块的ArtifactId}
  • -->Next-->Next-->Finish

本示例中,子模块的名称为module-amodule-bGroupAritifact的配置如下: 
这里写图片描述

通过同样的方式创建子模块module-b

优化结构

完成上述的主工程以及两个子模块创建之后,项目结构视图如下: 
这里写图片描述 

 

下面对这个多模块项目进行优化。

删除主工程多余目录

并不需要在主工程进行任何代码开发,所以删除其src目录。

编辑主工程pom.xml

作为主工程,其pom.xml可以作为其他子模块工程的基准依赖,方便进行统一的版本管理。

将主工程pom.xml修改如下:


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <groupId>pers.hanchao </groupId>
  6. <artifactId>main-project </artifactId>
  7. <version>1.0-SNAPSHOT </version>
  8. <name>main-project </name>
  9. <!--子模块工厂配置-->
  10. <modules>
  11. <module>module-a </module>
  12. <module>module-b </module>
  13. </modules>
  14. <!--Spring Boot配置-->
  15. <parent>
  16. <groupId>org.springframework.boot </groupId>
  17. <artifactId>spring-boot-starter-parent </artifactId>
  18. <version>1.5.2.RELEASE </version>
  19. <relativePath/> <!-- lookup parent from repository -->
  20. </parent>
  21. <!--配置参数-->
  22. <properties>
  23. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  24. <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding>
  25. <java.version>1.8 </java.version>
  26. <spring-cloud.version>Dalston.SR1 </spring-cloud.version>
  27. </properties>
  28. <dependencies>
  29. <!--Spring Boot 执行器组件-->
  30. <dependency>
  31. <groupId>org.springframework.boot </groupId>
  32. <artifactId>spring-boot-starter-actuator </artifactId>
  33. </dependency>
  34. <!--Spring Cloud 服务注册组件-->
  35. <dependency>
  36. <groupId>org.springframework.cloud </groupId>
  37. <artifactId>spring-cloud-starter-eureka-server </artifactId>
  38. </dependency>
  39. <!--Spring Boot Web组件-->
  40. <dependency>
  41. <groupId>org.springframework.boot </groupId>
  42. <artifactId>spring-boot-starter-web </artifactId>
  43. </dependency>
  44. <!--Spring Boot 测试组件-->
  45. <dependency>
  46. <groupId>org.springframework.boot </groupId>
  47. <artifactId>spring-boot-starter-test </artifactId>
  48. <scope>test </scope>
  49. </dependency>
  50. </dependencies>
  51. <!--Spring Cloud 版本序列配置-->
  52. <dependencyManagement>
  53. <dependencies>
  54. <dependency>
  55. <groupId>org.springframework.cloud </groupId>
  56. <artifactId>spring-cloud-dependencies </artifactId>
  57. <version>${spring-cloud.version} </version>
  58. <type>pom </type>
  59. <scope>import </scope>
  60. </dependency>
  61. </dependencies>
  62. </dependencyManagement>
  63. <!--仓库配置-->
  64. <repositories>
  65. <repository>
  66. <id>spring-snapshots </id>
  67. <name>Spring Snapshots </name>
  68. <url>https://repo.spring.io/snapshot </url>
  69. <snapshots>
  70. <enabled>true </enabled>
  71. </snapshots>
  72. </repository>
  73. <repository>
  74. <id>spring-milestones </id>
  75. <name>Spring Milestones </name>
  76. <url>https://repo.spring.io/milestone </url>
  77. <snapshots>
  78. <enabled>false </enabled>
  79. </snapshots>
  80. </repository>
  81. </repositories>
  82. </project>

在上述的pom.xml文件中,主要做了以下几件事:

  • 配置Spring Boot的版本
  • 配置Spring Cloud的版本
  • 配置微服务的一些基本组件,如actuatoreurekawebtest
  • 配置编码方式
  • 配置java版本
  • 配置子模块

注意:Spring BootSpring Cloud有特定的对照关系,具体可参看:http://projects.spring.io/spring-cloud/

 

编辑子模块pom.xml

子模块pom.xml可以引用主工程pom.xml的依赖关系,所以也对其进行配置,其中module-apom.xml配置如下:


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <groupId>pers.hanchao.mainproject </groupId>
  6. <artifactId>module-a </artifactId>
  7. <version>0.0.1-SNAPSHOT </version>
  8. <packaging>jar </packaging>
  9. <name>module-a </name>
  10. <description>Demo project for Spring Cloud Module A </description>
  11. <!--父工程的依赖-->
  12. <parent>
  13. <groupId>pers.hanchao </groupId>
  14. <artifactId>main-project </artifactId>
  15. <version>1.0-SNAPSHOT </version>
  16. <relativePath/> <!-- lookup parent from repository -->
  17. </parent>
  18. <!--子模块的个性化依赖-->
  19. <dependencies>
  20. </dependencies>
  21. </project>

从上面的配置可以得知,module-a主工程中继承了一系列依赖关系,如actuatoreurekawebtest

并且,module-a还可以对自己模块特有的一些依赖进行配置。

运行测试

改写module-a的启动类ModuleAApplication.java,如下:


 
 
  1. @RestController
  2. @SpringBootApplication
  3. public class ModuleAApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ModuleAApplication.class, args);
  6. }
  7. @GetMapping( "/hi")
  8. public String hi(){
  9. return "Hello World!";
  10. }
  11. }

启动项目,在浏览器访问http://localhost:8080/hi,结果如图所示: 
这里写图片描述

说明配置成功。

原文链接:https://blog.csdn.net/hanchao5272/article/details/80558780

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值