Spring Cloud版本
2021.0.5
Spring Cloud Alibaba版本
2021.0.5.0
Spring Boot版本
2.7.10
pom文件
需要放在依赖管理的pom文件
<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>
<!-- aliyun组件 配置组件,没有它阿里的中间件用不了 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-spring-boot-dependencies</artifactId>
<version>${aliyun-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依赖 -->
<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>
</dependencyManagement>
注意: 这些pom引入的,要放在依赖管理里
引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<!-- 这个版本需要引入spring-cloud-starter,不然找不到bootstrap.properties文件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
bootstrap.properties文件
spring.application.name=aaaa
spring.profiles.active=dev
# 注册中心
spring.cloud.nacos.discovery.server-addr=xxxx:8848
spring.cloud.nacos.discovery.group=cloud-dev
spring.cloud.nacos.discovery.namespace=ccccc
# 配置中心
spring.cloud.nacos.config.server-addr=xxxx:8848
spring.cloud.nacos.config.file-extension=properties
spring.cloud.nacos.config.group=cloud-dev
spring.cloud.nacos.config.namespace=ccccc
其他的就是nacos服务端的配置了
nacos服务端配置
创建命名空间
创建配置文件
要注意nacos客户端识别配置文件是按配置的${spring.application.name}-${spring.profiles.active}.${file-extension}方式获取的,但是按实际操作来看,这个版本会按namespace、data-id、file-extension、spring.profiles.active的自由组合方式来获取配置文件