本项目基于spring boot 多模块
注意spring -boot、spring-cloud、spring-cloud-alibaba的版本兼容性
1.1、父级pom依赖
<properties>
<spring-boot.version>2.7.18</spring-boot.version>
<spring.cloud.version>2021.0.1</spring.cloud.version>
<spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot 依赖配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.2、子模块pom依赖
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
2、子模块配置文件
必须在resources下使用bootstrap.yml 文件
确保namespace、group、cluster一致性
server:
port: 8001
servlet:
context-path: /user
spring:
application:
name: user
profiles:
# 环境配置 需要在nacos管理端添加配置文件
active: standard
cloud:
nacos:
discovery:
server-addr: 192.168.20.113:8848
namespace: cedb1f46-d9ae-43b1-99db-39b245152543
group: DEFAULT_GROUP
# 动态读取nacos服务端配置
config:
username: nacos
password: nacos
server-addr: 192.168.20.113:8848
file-extension: yaml
namespace: cedb1f46-d9ae-43b1-99db-39b245152543
group: DEFAULT_GROUP
refresh: true
# 共享配置
shared-configs:
- application-druid.${spring.cloud.nacos.config.file-extension}
3、开启nacos
启动类添加注解@EnableDiscoveryClient
@EnableAsync
@EnableScheduling
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableDiscoveryClient
public class MoodSysApplication{
}