SpringBoot Nacos 安装、注解开发配置自动加载更新并监听
SpringBoot Nacos 安装及配置入门Demo
基于注解
- 安装
- 下载tar包
github https://github.com/alibaba/nacos/releases/tag/1.4.0 不过下载的有时候很慢
csdn https://download.csdn.net/download/HumorChen99/13344131 我发的,免费下
- 解压即可,在bin目录有startup.sh启动
#进入bin目录并以独立模式启动
./startup.sh -m standalone
- 在浏览器访问 http://ip:8848/nacos 账号密码默认都为nacos
- 添加一个测试配置 systemSwtich值设置为true
-
建立springboot项目
-
依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>springboot_nacos_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_nacos_demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-config-spring-boot-starter</artifactId> <version>0.2.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
application.yml
nacos: config: server-addr: 192.168.108.129:8848 server: port: 80
-
controller代码
autoRefreshed参数为是否自动刷新,开启后一旦配置更新即可同步更新
NacosValue(value = “${systemSwitch:false}”,autoRefreshed = true)
systemSwitch:false 配置:默认值,也就是查不到的时候返回这个默认的
package com.example.springboot_nacos_demo.controller;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/nacos")
@NacosPropertySource(dataId = "example",autoRefreshed = true)
public class NacosController {
@NacosValue(value = "${systemSwitch:false}",autoRefreshed = true)
private boolean systemSwitch;
@GetMapping("/systemSwitch")
public boolean getSystemSwitch(){
return this.systemSwitch;
}
}
-
浏览器打开查看内容
-
监听配置更新事件 @NacosConfigListener(dataId = “example”)
type为配置类型,可以为properties yaml json等等
一旦配置变更,该方法将收到消息,做自定义处理
@RestController
@RequestMapping("/nacos")
@NacosPropertySource(dataId = "example",autoRefreshed = true,type = ConfigType.JSON)
public class NacosController {
@NacosValue(value = "${systemSwitch:false}",autoRefreshed = true)
private boolean systemSwitch;
@NacosConfigListener(dataId = "example")
public void onMessage(String msg){
System.out.println(msg);
}
@GetMapping("/systemSwitch")
public boolean getSystemSwitch(){
return this.systemSwitch;
}
}
一旦配置被更改,监听的接口将立即收到消息。在示范里我们打印了传来的消息,是更新后的配置
- 访问 http://localhost/nacos/systemSwitch显示true
本文来自博客园,作者:HumorChen99,转载请注明原文链接:https://www.cnblogs.com/HumorChen/p/18039679
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~