Springboot之nocos整合与使用的方法
前言Nacos致力于帮助您发现、配置和管理微服务。Nacos提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。Nacos帮助您更敏捷和容易地构建、交付和管理微服务平台。Nacos是构建以“服务”为中心的现代应用架构(例如微服务范式、云原生范式)的服务基础设施1,创建工程先创建maven工程,父工程pom如下:4.0.0org.exampleconfigDemo1.0-SNAPSHOTorg.springframework.bootspring-boot-starter
前言
Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施
1, 创建工程
先创建maven工程,父工程pom如下:
4.0.0 org.example configDemo 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.3.2.RELEASE com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-alibaba-dependencies 2.2.5.RELEASE pom import
2,启动nacos-server服务
访问的url是:http://localhost:8848/nacos/ 默认端口是8848,账号密码是:nacos/nocos
3,编写controller进行动态配置生效
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yhq
* @version 1.0
* @date 2022/7/15 19:07
*/
@RestController
@RefreshScope //@RefreshScope:需要配置这个才能动态更新配置。
public class TestController {
@Value("${name}")
private String name;
@GetMapping("/getName")
public String test(){
return name;
}
}4,添加配置文件boostrap.yaml
springboot默认加载配置文件顺序:
bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml 其中bootstrap.properties 配置为最高优先级先加载的会被后加载的覆盖掉,所以.properties和.yml同时存在时,.properties会失效,.yml会起作用。”
#端口
server:
port: 8888
#配置项目名称
spring:
application:
#configdemo默认是nacos的DateId名称
name: configdemo
#指定test的配置文件
profiles:
active: test
cloud:
nacos:
config:
server-addr: localhost:8848
#加载yaml的nacos文件
file-extension: yaml
可以看到启动时进行加载了文件如下:

5,nacos配置
配置了configdemo和configdemo-test.yaml
注意的是:它的加载规则是:# 1.DataId
- 用来读取远程配置中心的中具体配置文件其完整格式如下:
- ${prefix}-${spring.profile.active}.${file-extension}
a. prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
b. spring.profile.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档。 注意:当 spring.profile.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
c. file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。
如果configdemo和configdemo-test.yaml 都存在name的配置,优先configdemo-test.yaml
访问结果如下:

Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















