官方參考文檔
https://nacos.io/zh-cn/docs/quick-start-spring-boot.html
本人實(shí)踐
1、新建一個(gè)spring boot項(xiàng)目
我的spirngboot版本為2.5.6
2、添加一下依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${nacos.version}</version>
</dependency>
3、添加配置
nacos.config.server-addr=192.168.17.173:8848
nacos.config.context-path=/nacos
nacos.config.username=nacos
nacos.config.password=nacos
nacos.config.data-id=example
nacos.config.auto-refresh=true
4.編寫測試代碼
@Controller
@RequestMapping("config")
public class ConfigController {
@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
private boolean useLocalCache;
@RequestMapping(value = "/get", method = GET)
@ResponseBody
public boolean get() {
return useLocalCache;
}
}
踩坑記錄一:nacos-config-spring-boot-starter版本不對
nacos-config-spring-boot-starter 0.2.8以下, nacos-client 是1.x的版本
nacos-config-spring-boot-starter 0.2.9開始, nacos-client 是2.X的版本
踩坑記錄二:namespace配置不對
由于我沒有配置namespace,默認(rèn)的事public,因此我在配置里面加了下面這條配置導(dǎo)致錯(cuò)誤文章來源:http://www.zghlxwxcb.cn/news/detail-685570.html
nacos.config.namespace=public
應(yīng)該使用命名空間ID,此處有很多人踩坑,后來者一定要注意哦。文章來源地址http://www.zghlxwxcb.cn/news/detail-685570.html
到了這里,關(guān)于Spring Boot 中 Nacos 配置中心使用實(shí)戰(zhàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!