namespace设计真实一个奇特的东西。用spring-cloud-starter-alibaba-nacos-config测试的时候,java代码里设置namespace必须使用那一串类似uuid的值,直接写英文名称一直获取不到值(public namespace除外),这个问题折腾了雪人出装我好几天;网上的资料要么是写的不全,要么是胡编乱造;
真不知道这种设计意欲何为
启动类:
@springbootapplicationpublic class nacosmain { public static void main(string[] args) { springapplication.run(nacosmain.class ,args); } }
controller类
@restcontroller@refreshscopepublic class nacoscontroller { @value("${uu:}") private string name; @getmapping("/hello") public string info(){ // system.out.println(name); return name; }}
application.yaml
rver: port: 10086 rvlet: context-path: /nacosdemo
bootstrap.yaml
spring: application: name: demo cloud: nacos: config: rver-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="/d/file/titlepic/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> &爱戴个人资料lt;groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-alibaba-nacos-config</artifactidwin7屏保> </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.relea</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="/d/file/titlepic/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&g对不起的英文怎么写t; <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-dependencies</artifactid> <version>2.3.0.relea</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.relea</version> <type>pom</type> <scope>import</scope> </dependency> --> </dependencies> </dependencymanagement></project>
verify: qr_url: xxxxxxxx
@value("verify.qr_url")privite string url;
震惊!取不到值!
为啥呢?难道是用的nacos的原因,百度一下,还是没办法解决,那我试试拿其他配置,结果,拿到了!
那就可以断定,不是nacos的原因,那是啥原因呢
verify-url: xxxxxx
拿到了!
ok,解决了,就是我命名不规范,说不定人家naocs不认你这个,问我为啥这么确定是nacos不认,因为我直接写在本地application.yml里是可以读取到的。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 13:27:10,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/19757b2041c86e362a9fe104dcccb234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Nacos入门过程的坑–获取不到配置的值问题.doc
本文 PDF 下载地址:Nacos入门过程的坑–获取不到配置的值问题.pdf
留言与评论(共有 0 条评论) |