使用IDEA工具 创建springboot,含有父Module以及多子模块的微服务架构

在 IntelliJ IDEA 中,没有类似于 Eclipse 工作空间(Workspace)的概念,而是提出了ProjectModule这两个概念。在 IntelliJ IDEA 中Project是最顶级的结构单元,然后就是Module,一个Project可以有多个Module。目前,主流的大型项目结构基本都是多Module的结构,这类项目一般是按功能划分的,比如:user-center-module、order-center-module和eureka-module等等,模块之间彼此可以相互依赖。通过这些Module的命名可以看出,它们都是处于同一个项目中的模块,彼此之间是有着不可分割的业务关系。因此,我们可以大致总结出:一个Project是由一个或多个Module组成。

     当为单Module项目的时候,这个单独的Module实际上就是一个Project;
     当为多Module项目的时候,多个模块处于同一个Project之中,此时彼此之间具有互相依赖的关联关系。

Module结构一般如下:

1、创建父模块

1、首先是创建父Module: fang-root,这个步骤跟普通的单项目一模一样,可以参考上一篇文章,

①打开IDEA,点击File,点击New选择 Project. 开始创建一个新项目。

②2.在左侧菜单找到并点击 Spring Initializr,点击next。注意,这里idea默认使用https://start.spring.io提供的在线模板,所以需要保证网络畅通。

③在左上侧,可以选择Spring Boot的版本,这里使用2.5.0。

因为创建的是父工程,第四部的一些依赖不需要选择,直接点Finish 就可以了。

2.项目结构如下

3.然后就是删除无用的.mvn目录、src目录、mvnw及mvnw.cmd文件,最终只留.gitignore和pom.xml 。

2、创建子模块

1、在fang-root右键选择,选择New -> Module,点击next

2、可以选择自己所需要的依赖,也可以不选择直接Finish 。结构如下:

3、按照上面的方法,依次创建自己所需要的模块,这里创建了fang-eureka(注册中心)、fang-gateway(网关)、fang-modules(业务模块)、fang-user(用户)、fang-order(订单)。结构如下:

4、父pom文件中声明所有子模块及继承SpringBoot提供的父工程:

①fang-root的pom

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.fang</groupId>
    <artifactId>fang-root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>fang-root</name>
    <packaging>pom</packaging>
    <description>Demo project for Spring Boot</description>

    <modules>
        <!--注册中心-->
        <module>fang-eureka</module>
        <!--网关中心-->
        <module>fang-gateway</module>
        <!--业务中心-->
        <module>fang-modules</module>
    </modules>



    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
②fang-modules的pom
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.fang</groupId>
        <artifactId>fang-root</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <artifactId>fang-modules</artifactId>
    <packaging>pom</packaging>
    <description>业务中心</description>

    <modules>
        <!--订单中心-->
        <module>fang-order</module>
        <!--用户中心-->
        <module>fang-user</module>
    </modules>
</project>

③fang-order子模块的pom,xom

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.fang</groupId>
        <artifactId>fang-modules</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <artifactId>fang-order</artifactId>
    <description>订单中心</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

5、这样一个多模块的Springboot项目就完成了,先不急着去配置或者写代码,先运行起来。在运行的时候遇到了几个问题。

①clean的时候直接报错:Process terminated   没有任何提示

      需要将所有pom文件里的 全部删除。

     relativePath/>设定一个空值默认值为../pom.xml 表示将始终从父级仓库中获取,不从本地路径获取

    MAVEN构建jar包时候查找顺序:relativePath元素中的地址–本地仓库–远程仓库 

②父类的pom  必须 

<packaging>pom</packaging>   因为没有代码,不需要jar包

③springboot启动不了也不报错的解决方案

6、通过以上配置,这个项目基本上是建立起来了

7、针对上面第5点的第三个问题,我这里翻了个错误,因为我在创建项目的时候什么依赖都没有选择。所以一直都无法启动成功。需要在pom里面加入启动包,

spring-web 不行,改为spring-boot-starter-web,正确的pom如下:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

引入上面的包,项目就可以正常启动了。到此,一个完整的多模块项目就搭建结束了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值