记录一次使用nacos修改配置不生效的经过

记录一次使用nacos修改配置不生效的经过

bootstrap文件中的内容如下

spring:
  application:
    name: cloud-center-web
  cloud:
    nacos:
      discovery:
        enabled: true
        service: ${spring.application.name}
        server-addr: 192.168.31.151:8848
      config:
        enabled: true
        encode: UTF-8
        file-extension: yaml

application文件中的内容如下

server:
  port: 8080
  servlet:
    context-path: /

spring:
  profiles:
    active: dev

在nacos中添加配置文件

Data ID的格式为

${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

文件格式选bootstrap中写的格式就好

内容修改为如下

server:
  port: 8083
  servlet:
    context-path: /web

启动代码查看控制台打印日志

这时,启动报错了

DEBUG org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set
	at org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor.postProcessEnvironment(ConfigDataMissingEnvironmentPostProcessor.java:82)
	at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:102)
	at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:87)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85)
	at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
    ...
    
***************************
APPLICATION FAILED TO START
***************************

Description:

No spring.config.import property has been defined

Action:

Add a spring.config.import=nacos: property to your configuration.
	If configuration is not required add spring.config.import=optional:nacos: instead.
	To disable this check, set spring.cloud.nacos.config.import-check.enabled=false.

百度之后在怕pom文件中添加bootstrap的依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

再次启动,查看日志

Tomcat started on port(s): 8080 (http) with context path ''

发现打印的日志并没有按照nacos中指定的配置文件中的配置运行

INFO 10180 --- [           main] c.a.c.n.refresh.NacosContextRefresher    : [Nacos Config] Listening config: dataId=cloud-center-web.yaml, group=DEFAULT_GROUP
INFO 10180 --- [           main] c.a.c.n.refresh.NacosContextRefresher    : [Nacos Config] Listening config: dataId=cloud-center-web-dev.yaml, group=DEFAULT_GROUP
INFO 10180 --- [           main] c.a.c.n.refresh.NacosContextRefresher    : [Nacos Config] Listening config: dataId=cloud-center-web, group=DEFAULT_GROUP

看日志中也说了有监听对应的配置文件,表示不能理解,再次百度,查GitHub中nacos的issue列表,无果.

最后结果是没有指定nacos.config.server-addr的路径,把修改后的配置重新贴上来

spring:
  application:
    name: cloud-center-web
  cloud:
    nacos:
      discovery:
        enabled: true
        service: ${spring.application.name}
        server-addr: 192.168.31.151:8848
      config:
        enabled: true
        encode: UTF-8
        file-extension: yaml
        server-addr: ${spring.cloud.nacos.discovery.server-addr}

重启应用

Tomcat started on port(s): 8083 (http) with context path '/web'

查看日志,发现nacos配置已生效,问题解决.


参考:

  1. https://nacos.io/zh-cn/docs/next/v2/ecology/use-nacos-with-spring-cloud
  2. https://blog.csdn.net/fjptzlb/article/details/134053057
  3. 第2节-Springcloud-Nacos注册中心与配制中心的使用_哔哩哔哩_bilibili(https://www.bilibili.com/video/BV18e4y1d7ns/?p=2&spm_id_from=pageDriver)
  4. https://github.com/alibaba/nacos/issues
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值