iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Nacos入门过程的坑--获取不到配置的值问题
  • 610
分享到

Nacos入门过程的坑--获取不到配置的值问题

2024-04-02 19:04:59 610人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录Nacos获取不到配置的值本地nacosJAVA代码Nacos配置文件,通过@Value() 获取时失败了在nacos中配置的是这样的但是在Controller中取值取不到是我的

Nacos获取不到配置的值

namespace设计真实一个奇特的东西。用spring-cloud-starter-alibaba-nacos-config测试的时候,JAVA代码里设置namespace必须使用那一串类似UUID的值,直接写英文名称一直获取不到值(public namespace除外),这个问题折腾了我好几天;网上的资料要么是写的不全,要么是胡编乱造;

真不知道这种设计意欲何为

本地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的原因,百度一下,还是没办法解决,那我试试拿其他配置,结果,拿到了!

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

是我的命名不规范吗?我改下吧

verify-url: xxxxxx

拿到了!

ok,解决了,就是我命名不规范,说不定人家naocs不认你这个,问我为啥这么确定是nacos不认,因为我直接写在本地application.yml里是可以读取到的。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Nacos入门过程的坑--获取不到配置的值问题

本文链接: https://www.lsjlt.com/news/161993.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作