最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nacos入門過程的坑--獲取不到配置的值問題

 更新時間:2022年01月10日 08:40:06   作者:萬物皆字節(jié)  
這篇文章主要介紹了Nacos入門過程的坑--獲取不到配置的值問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Nacos獲取不到配置的值

namespace設(shè)計真實一個奇特的東西。用spring-cloud-starter-alibaba-nacos-config測試的時候,JAVA代碼里設(shè)置namespace必須使用那一串類似UUID的值,直接寫英文名稱一直獲取不到值(public namespace除外),這個問題折騰了我好幾天;網(wǎng)上的資料要么是寫的不全,要么是胡編亂造;

真不知道這種設(shè)計意欲何為

本地nacos

JAVA代碼

啟動類:

@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: /nacosdemo
 

bootstrap.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: yaml
 

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>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配置文件,通過@Value() 獲取時失敗了

在nacos中配置的是這樣的

verify:
?? ?qr_url: xxxxxxxx

但是在Controller中取值取不到

@Value("verify.qr_url")
privite String url;

震驚!取不到值!

為啥呢?難道是用的nacos的原因,百度一下,還是沒辦法解決,那我試試拿其他配置,結(jié)果,拿到了!

那就可以斷定,不是nacos的原因,那是啥原因呢

是我的命名不規(guī)范嗎?我改下吧

verify-url: xxxxxx

拿到了!

ok,解決了,就是我命名不規(guī)范,說不定人家naocs不認(rèn)你這個,問我為啥這么確定是nacos不認(rèn),因為我直接寫在本地application.yml里是可以讀取到的。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring-mybatis獲取mapper的四種方式匯總

    spring-mybatis獲取mapper的四種方式匯總

    這篇文章主要介紹了spring-mybatis獲取mapper的四種方式匯總,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 最新評論

    大理市| 福清市| 江源县| 万山特区| 乌苏市| 德令哈市| 休宁县| 鄄城县| 淮安市| 弥勒县| 绍兴市| 垦利县| 定结县| 黄冈市| 台前县| 洛浦县| 淅川县| 焉耆| 钟祥市| 工布江达县| 阳新县| 宁河县| 普安县| 淳化县| 巨野县| 平塘县| 玛沁县| 分宜县| 额济纳旗| 泸水县| 武清区| 桂阳县| 石林| 新昌县| 奉贤区| 台北市| 郴州市| 永胜县| 乌海市| 璧山县| 青河县|