栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

cloud-alibaba的nacos坑

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cloud-alibaba的nacos坑

前置条件:
 
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
            2.2.1.RELEASE
 
获取配置中心命名导致获取不到文件

获取配置文件命名格式:

{nacos.config.prefix}.{nacos.config.file-extension}{nacos.config.prefix}-{spring.profiles.active}.{nacos.file-extension}

spring:
  application:
    name: gulimall-cart1
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: 39.99.234.10:8848
      config:
        server-addr: 39.99.234.10:8848
        file-extension: yaml
        prefix: gulimall-cart #推荐使用这个,因为name和他具有相同效果但语义不如prefix明确

最终获取配置文件名查看启动日志

@Value可以拿到值, @NacosValue拿不到值

请使用Value去进行获取
原因是应为:用的不是nacos原生,而是用的spring-cloud-starter-alibaba-nacos-config
官方说明:
After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment’s PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh .
所以按照阿里官网的意思,正常就应该用@Value而不应该用@NacosValue

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736004.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号