3

前言

最近开始学习SpringCloud 2020.0.2新的版本,准备使用全新的架构,如图所示
image.png
所以记录下使用idea创建多模块项目,一步一步记录搭建的过程

创建项目(New Project)

image.png
image.png
image.png
image.png
image.png

添加模块(New Module)

image.png
image.png
image.png
image.png

引用SpringCloud前置操作

1、删除ztosin-parent项目下的src目录
image.png
2、修改ztosin-parent项目的的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>

    <artifactId>ztosin-nacossvr</artifactId>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
    </parent>
    
  <properties>
       <java.version>1.8</java.version>
       <spring-boot.version>2.4.4</spring-boot.version>
       <spring-cloud.version>2020.0.2</spring-cloud.version>
       <spring-cloud-alibaba.version>2020.0.RC1</spring-cloud-alibaba.version>
   </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
       <dependencies>
           <!-- spring boot 依赖 -->
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-dependencies</artifactId>
               <version>${spring-boot.version}</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
           <!-- spring cloud 依赖 -->
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>${spring-cloud.version}</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
           <!-- spring cloud alibaba 依赖 -->
           <dependency>
               <groupId>com.alibaba.cloud</groupId>
               <artifactId>spring-cloud-alibaba-dependencies</artifactId>
               <version>${spring-cloud-alibaba.version}</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <configuration>
                    <dockerfile>src/main/docker/Dockerfile</dockerfile>
                    <!--                    <repository>spotify/foobar</repository>-->
                    <!--<tag>${project.version}</tag>-->
                    <buildArgs>
                        <!--提供参数向Dockerfile传递-->
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、添加NacosServerApplication.java

package com.ztosin.nacossvr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class NacosServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosServerApplication.class, args);
    }
}

最终目录结构如下所示 :
image.png

参考

方志朋SpringCloud博客
spring-cloud
使用 IDEA 创建多模块项目

--- 2022-12-22更新 ---

1、如果一个project只有一个module,那你创建project时选择 empty project
2、如果一个project多个module,那你创建project时选择 maven project


Awbeci
3.1k 声望212 粉丝

Awbeci