随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

使用Idea创建一个父子SpringBoot项目

一、概述

  在设计微服务的时候,每个服务都是单独的一个应用,并且可以单独运行。一个大项目中可能会有N个微服务。此时如果不集中管理ide就会打开多个项目,每个项目都是一个单独的窗口,切换起来异常麻烦。其就相当于打开了多个不相关的项目。那么有没有办法可以让这些同一个项目的微服务集中管理呢。答案是肯定的,下面展示如何去做

二、具体步骤

  1.先随便创建一个SpringBoot项目

  2.删除项目中的src文件夹

  3.在项目上用鼠标右键,新建model

  4.多创建几个model,创建之后的效果

  5.此时根目录中的pom.xml就会有你添加的各种model的配置及描述

  下面贴出完整的根目录的pom.xml

复制代码
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tony.micro</groupId>
    <artifactId>tony-micro</artifactId>
    <version>1.0-SNAPSHOT</version><!--配置项目的版本-->
    <packaging>pom</packaging>
    <!--项目中以来的Module-->
    <modules>
        <module>gateway-service</module>
        <module>gateway-service</module>
        <module>user-service</module>
        <module>basic-service</module>
        <module>common</module>
    </modules>

    <properties>
        <!--指定JDK的版本号-->
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <!--根模块继承了SpringBoot,子模块也跟着继承了SpringBoot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <!--版本控制-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.tony.common</groupId>
                <artifactId>common</artifactId>
                <version>${version}</version>
            </dependency>
            <dependency>
                <groupId>com.tony.basic</groupId>
                <artifactId>basic-service</artifactId>
                <version>${version}</version>
            </dependency>
            <dependency>
                <groupId>com.tony.user</groupId>
                <artifactId>user-service</artifactId>
                <version>${version}</version>
            </dependency>
            <dependency>
                <groupId>com.tony.gateway</groupId>
                <artifactId>gateway-service</artifactId>
                <version>${version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!--快速生成getter和setter-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <!--json-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.67_noneautotype2</version>
        </dependency>
        <!--测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 注册中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
        <!-- 配置中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
        <!--        服务间远程调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>

        <!--swagger-->

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
复制代码

  6.在子模块中配置父模块,每个模块重复配置这个

 <parent>
        <groupId>com.tony.micro</groupId>
        <artifactId>tony-micro</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

  7.如果项目中有一个基础模块,而这个基础模块基本每个微服务都需要,则则可以在微服务模块中进行引入

  <!--添加对Common模块的依赖-->
        <dependency>
            <groupId>com.tony.common</groupId>
            <artifactId>common</artifactId>
        </dependency>

  8.大致就这么多了,其实到目前位置也是够用了

 

posted on   飘杨......  阅读(984)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示