目录
一、添加Nacos配置信息
- 一、添加Nacos配置信息
- 二、创建项目,引入maven依赖
-
nacos-parent的父工程的pom如下:
com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.0.RELEASE pom import org.springframework.cloud spring-cloud-dependencies Greenwich.RELEASE pom import org.springframework.boot spring-boot-dependencies 2.1.3.RELEASE pom import
-
nacos-server子工程的pom如下:
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator
-
在nacos-server子工程的resources目录下创建bootstrap.yml文件,因为nacos的配置要优先加载,所以放到bootstrap.yml中,而不是application.yml中
server: port: 3377 # 在Nacos Spring Cloud 中,dataId 的完整格式如下:${prefix}-${spring.profiles.active}.${file-extension} spring: application: name: nacos-config-client # 配置名 cloud: nacos: config: server-addr: localhost:8848 # 配置中心地址 file-extension: yaml # 后缀名 namespace: 9b6b638c-34de-4512-8618-4e91d55d4592 # 命名空间ID group: DEFAULT_GROUP # 指定要获取配置的组
-
在nacos-server子工程的resources目录下创建application.yml文件,指定加载的环境
spring: profiles: #配置nacos配置中心的Data Id active: dev #表示开发环境
-
创建测试类
@RestController @RefreshScope //支持nacos动态刷新功能,即nacos配置中心3377客户端动态刷新获取nacos注册中心服务配置的配置文件信息 @Slf4j public class ConfigClientController { //从nacos注册中心服务配置上获取配置文件的获取内容 @Value("${config.info}") private String configInfo; @GetMapping("/config/info") public String getConfigInfo() { log.info("从nacos注册中心服务配置上获取配置文件的获取内容:"+configInfo); return configInfo; } }
-
发送测试请求 http://localhost:3377/config/info