Nacos 之服务配置中心
一、Nacos 作为配置中心-基础配置
1.建立Model
建立一个名为”cloudalibaba-config-nacos-client3377“的Model。
2.改POM
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>springcloud-nacos</artifactId> <groupId>com.ckfuture.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloudalibaba-config-nacos-client3377</artifactId> <dependencies> <!--nacos-config--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--nacos-discovery--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--web+actuator--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
3.建YML
分别建立”application.yml“和”bootstrap.yml“两个配置
application.yml
spring:
profiles:
active: dev #表示开发环境
bootstrap.yml
# Nacos全局配置 server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 #nacos 服务注册中心地址 config: server-addr: localhost:8848 #nacos 作为配置中心地址 file-extension: yaml #指定yaml格式的配置
4.主启动类
package com.ckguture.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class NacosConfigClientMain3377 { public static void main(String[] args) { SpringApplication.run(NacosConfigClientMain3377.class,args); } }
5.业务类
package com.ckguture.springcloud.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; @RestController @RefreshScope //支持Nacos的动态刷新功能 public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/config/info") public String getConfigInfo(){ return configInfo; } }
6.在Nacos中添加配置信息
Nacos配置管理dataId的完整格式:
${prefix}-${spring.profile.active}.${file-extension}
prefix 默认为spring.application.name 的值,也可通过配置项 spring.cloud.nacos.config.prefix来配置。
spring.profile.active 即为当前环境对应的profile
file-extension 为配置内容的数据格式,目前支持 properties和yml类型。
最终公式:
${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
根据公司得到配置dataId为:nacos-config-client-dev.yaml (注意 yaml不是yml)
在Nacos的配置列表中新增配置
配置内容为:
config: info: nacos config center,version = 1
7.测试
启动主启动类
浏览器访问:http://localhost:3377/config/info
8.自带动态刷新
修改Nacos配置内容,接口访问跟着刷新。
再次访问:
二 Nacos作为配置中心-分类配置
NameSpace+Group+DataID
指定spring.profile.active和配置文件的DataID来使不同环境下读取不同的配置
建立"nacos-config-client-test.yaml"
配置内容:
config: info: nacos config center,nacos-config-client-test.yaml version = 2
修改3377配置
启动并测试
现在两个配置(dev和test都默认在public中)。
相同的DataID在不同的GROUP中,程序中怎样区别;
在bootstrap.yml中增加默认group组名。
# Nacos全局配置 server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 #nacos 服务注册中心地址 config: server-addr: localhost:8848 #nacos 作为配置中心地址 file-extension: yaml #指定yaml格式的配置 group: TEST_GROUP #${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension} #nacos-config-client-dev.yaml
新建两个命名空间“dev”和“test”
将命名空间ID写入代码的配置文件中(以“dev”命名空间为例)
# Nacos全局配置 server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 #nacos 服务注册中心地址 config: server-addr: localhost:8848 #nacos 作为配置中心地址 file-extension: yaml #指定yaml格式的配置 namespace: 48ac12df-3c55-4e1b-bed2-30d87e6da6c7 group: DEV_GROUP #${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension} #nacos-config-client-dev.yaml
在配置列表中的“dev”中建立配置
启动运行并测试
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!