Springboot整合nacos

237 阅读1分钟

nacos本地安装:

下载对应的zip或tar包并解压:

unzip nacos-server-$version.zip

tar -xvf nacos-server-$version.tar.gz

到解压文件的bin目录:cd nacos/bin

不同环境的启动命令:

Linux/Unix/Mac

启动命令(standalone代表着单机模式运行,非集群模式):

sh startup.sh -m standalone

如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

bash startup.sh -m standalone

Windows

启动命令(standalone代表着单机模式运行,非集群模式):

startup.cmd -m standalone

访问首页:http://127.0.0.1:8848/nacos/#/login 默认账号密码:nacos nacos

关闭服务命令:

Linux/Unix/Mac

sh shutdown.sh

Windows

shutdown.cmd

或者双击shutdown.cmd运行文件

Springboot 整合nacos 存在版本兼容问题

yml 配置:

server: port: 8080 nacos: config: # 开启预加载配置 enable: true # 服务地址 server-addr: 127.0.0.1:8848 # 服务账号 username: nacos # 服务密码 password: nacos # data-id data-id: 222 # group group: DEFAULT_GROUP # 命名空间 namespace: 8a98a774-21ec-4b63-871c-21e5017e01ed # 配置文件类型 type: YAML # 最大重试次数 max-retry: 10 # 自动刷新 auto-refresh: true # 重试时间 config-retry-time: 2000 # 监听长轮询超时时间 config-long-poll-timeout: 46000

启动类上加注解: @NacosPropertySource(dataId = "222", autoRefreshed = true)

具体类的使用: @NacosValue(value = "${zhuyp:1}", autoRefreshed = true) private String serverName;