创建 Spring Boot 项目
pom
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.0.2
cn.swback
share-api
0.0.1-SNAPSHOT
pom
share-api
share-api
share-user
17
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-maven-plugin
注意:把项目修改为自动编译,配合 spring-boot-devtools 可以让热部署生效
在启动主类中,编写一个测试接口
测试,成功
提交:初始化项目
新建 share-user 模块
修改 share-user 模块的 pom.xml,添加之前父项目的依赖,并加入 lombok,加入 build 节点
<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.0modelVersion>
<parent>
<groupId>cn.swbackgroupId>
<artifactId>share-apiartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>share-userartifactId>
<properties>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
修改父项目的 pom.xml,移除所有的依赖,移除 build,只保留依赖的版本管理
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>3.0.2version>
<relativePath/>
parent>
<groupId>cn.swbackgroupId>
<artifactId>share-apiartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<name>share-apiname>
<description>share-apidescription>
<modules>
<module>share-usermodule>
modules>
<properties>
<java.version>17java.version>
<spring-cloud.version>2022.0.0spring-cloud.version>
<spring-cloud-alibaba.version>2022.0.0.0spring-cloud-alibaba.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.78version>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.7.16version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.28version>
dependency>
dependencies>
dependencyManagement>
project>
删除父项目的 src
application.yml
src 如图新建包,注意将启动主类放入 config 包
启动,测试
提交:新增 share-user 模块
修改 UserApplication 启动类,增加启动日志
修改 application.yml,增加 /user-service 上下文
重启,观察启动日志
resources 新增 logback-spring.xml 日志配置文件
<configuration>
<property name="PATH" value="./log/user"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>%d{mm:ss.SSS} %highlight(%-5level) %blue(%-30logger{30}:%-4line) %thread %green(%-18X{LOG_ID}) %msg%n
Pattern>
encoder>
appender>
<appender name=