Nacos入門過(guò)程的坑--獲取不到配置的值問(wèn)題
Nacos獲取不到配置的值
namespace設(shè)計(jì)真實(shí)一個(gè)奇特的東西。用spring-cloud-starter-alibaba-nacos-config測(cè)試的時(shí)候,JAVA代碼里設(shè)置namespace必須使用那一串類似UUID的值,直接寫英文名稱一直獲取不到值(public namespace除外),這個(gè)問(wèn)題折騰了我好幾天;網(wǎng)上的資料要么是寫的不全,要么是胡編亂造;
真不知道這種設(shè)計(jì)意欲何為
本地nacos

JAVA代碼
啟動(dòng)類:
@SpringBootApplication
public class NacosMain {
public static void main(String[] args) {
SpringApplication.run(NacosMain.class ,args);
}
}Controller類
@RestController
@RefreshScope
public class NacosController {
@Value("${uu:}")
private String name;
@GetMapping("/hello")
public String info(){
// System.out.println(name);
return name;
}
}application.yaml
server:
port: 10086
servlet:
context-path: /nacosdemobootstrap.yaml
spring:
application:
name: demo
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
namespace: 0519e084-652c-4b86-a43c-d2de2041ff28
group: DEFAULT_GROUP
file-extension: yamlpom
<?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>code-demoparent</artifactId>
<groupId>com.uu</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacosdemo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>父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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.uu</groupId>
<artifactId>code-demoparent</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>nacosdemo</module>
<module>loader</module>
<module>nacosclient</module>
<!--<module>attachment</module>-->
</modules>
<name>code-demoparent</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--spring-boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
-->
</dependencies>
</dependencyManagement>
</project>Nacos配置文件,通過(guò)@Value() 獲取時(shí)失敗了
在nacos中配置的是這樣的
verify: ?? ?qr_url: xxxxxxxx
但是在Controller中取值取不到
@Value("verify.qr_url")
privite String url;震驚!取不到值!
為啥呢?難道是用的nacos的原因,百度一下,還是沒辦法解決,那我試試拿其他配置,結(jié)果,拿到了!
那就可以斷定,不是nacos的原因,那是啥原因呢
是我的命名不規(guī)范嗎?我改下吧
verify-url: xxxxxx
拿到了!
ok,解決了,就是我命名不規(guī)范,說(shuō)不定人家naocs不認(rèn)你這個(gè),問(wèn)我為啥這么確定是nacos不認(rèn),因?yàn)槲抑苯訉懺诒镜豠pplication.yml里是可以讀取到的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
用Java設(shè)計(jì)實(shí)現(xiàn)多實(shí)例多庫(kù)查詢方式
這篇文章主要介紹了用Java設(shè)計(jì)實(shí)現(xiàn)多實(shí)例多庫(kù)查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
elasticsearch通過(guò)guice注入Node組裝啟動(dòng)過(guò)程
這篇文章主要為大家介紹了?elasticsearch通過(guò)guice注入Node組裝啟動(dòng)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
深入理解java內(nèi)置鎖(synchronized)和顯式鎖(ReentrantLock)
這篇文章主要介紹了Java多線程之內(nèi)置鎖(synchronized)和顯式鎖(ReentrantLock)的深入理解新的和用法,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
SpringBoot分頁(yè)查詢功能的實(shí)現(xiàn)方法
在實(shí)際的項(xiàng)目開發(fā)過(guò)程中,分頁(yè)顯示是很常見的頁(yè)面布局,所以學(xué)習(xí)如何實(shí)現(xiàn)分頁(yè)也是必要的,下面這篇文章主要給大家介紹了關(guān)于SpringBoot分頁(yè)查詢功能的實(shí)現(xiàn)方法,需要的朋友可以參考下2022-06-06
SpringCloud?Alibaba環(huán)境集成之nacos詳解
Spring?Cloud?Alibaba提供了越來(lái)越完善的各類微服務(wù)治理組件,比如分布式服務(wù)配置與注冊(cè)中心nacos,服務(wù)限流、熔斷組件sentinel等,本篇先來(lái)介紹SpringCloud?Alibaba環(huán)境集成之nacos詳解,需要的朋友可以參考下2023-03-03
java 將 list 字符串用逗號(hào)隔開拼接字符串的多種方法
這篇文章主要介紹了java 將 list 字符串用逗號(hào)隔開拼接字符串,本文給大家分享四種方法,每種方法通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
java實(shí)時(shí)監(jiān)控文件行尾內(nèi)容的實(shí)現(xiàn)
這篇文章主要介紹了java實(shí)時(shí)監(jiān)控文件行尾內(nèi)容的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

