1、新建maven项目
2、删掉src目录
指定springboot版本
最后父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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--指定父依赖springBoot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springcloud_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springcloud_demo</name>
<!--pom:标志为父工程-->
<packaging>pom</packaging>
<!--依赖版本管理-->
<properties>
<!--指定maven编译版本-->
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!--SpringCloud版本-->
<spring.clound.version>2020.0.4</spring.clound.version>
<!--SpringCloud Alibaba版本-->
<alibaba.cloud.version>2.2.6.RELEASE</alibaba.cloud.version>
</properties>
<!--声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,-->
<!--并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。-->
<dependencyManagement>
<dependencies>
<!--SpringCloud依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.clound.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--SpringCloud Alibaba依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${alibaba.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--依赖管理-->
<dependencies>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--指定JDK编译版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
<!--maven打包需要包括如下文件(把xml文件移到src/main/java目录里也需要配置下面这段代码)-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
3、创建子模块:
指明父工程
<?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.example</groupId>
<artifactId>springcloud_demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>nacos_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<!--指定maven编译版本-->
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
到现在就已经搭建好了一个微服务项目架构基础框架
4、yml里面需要配置一下nacos的配置
# Tomcat
server:
port: 9507
# Spring
spring:
application:
# 应用名称
name: nacos_demo
profiles:
# 环境配置
active: dev
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: ${NACOS_HOST:localhost}:${NACOS_PORT:8848}
group: CMS_GROUP
config:
group: CMS_GROUP
# 配置中心地址
server-addr: ${NACOS_HOST:localhost}:${NACOS_PORT:8848}
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs[0]:
data-id: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
group: CMS_GROUP
refresh: false
#debug: true