alibaba / spring-cloud-alibaba

Spring Cloud Alibaba provides a one-stop solution for application development for the distributed solutions of Alibaba middleware.
https://sca.aliyun.com
Apache License 2.0
27.83k stars 8.31k forks source link

nacos 修改配置不生效 #1474

Closed yqbboy closed 4 years ago

yqbboy commented 4 years ago

alibaba/nacos#1552 Issue Description

Describe what happened (or what feature you want)

使用nacos 1.1.4 docker mysql 单机版部署,总共遇到两个问题 1、修改配置后,配置不能及时生效,必须手动重启nacos(该问题每次都能复现) 2、当增加dataID 大于11条时,再次增加第12个配置时,新增的dataID 不能在列表上显示

Describe what you expected to happen

How to reproduce it (as minimally and precisely as possible)

Tell us your environment

docker-compose -f example/standalone-mysql.yaml up

Anything else we need to know?

docker compose

nacos:
    image: '10.30.161.199:5000/nacos/nacos-server:1.1.4'
    container_name: nacos-standalone
    environment:
      - MODE=standalone
      - SPRING_DATASOURCE_PLATFORM=mysql
      - MYSQL_MASTER_SERVICE_HOST=mysql
      - MYSQL_MASTER_SERVICE_DB_NAME=nacos-config
      - MYSQL_MASTER_SERVICE_PORT=3306
      - MYSQL_MASTER_SERVICE_USER=root
      - MYSQL_MASTER_SERVICE_PASSWORD=tKWH0FQGZmNjgQRyNq9stDS0IXDtxXmx
      - MYSQL_DATABASE_NUM=1
    ports:
      - "8848:8848"
      - "9555:9555"
    depends_on:
      - mysql
    restart: on-failure
    deploy:
      placement:
        constraints: [node.labels.nacos==true]
hehaipeng commented 4 years ago

我这里也遇到这样的问题了,请作者关注一下!

lwadsy commented 4 years ago

我这边也是这个问题

nkorange commented 4 years ago

非Docker部署,是否有同样问题?

lwadsy commented 4 years ago

我是本地单机跑的,有这个问题

nkorange commented 4 years ago

我是本地单机跑的,有这个问题

用的是Spring Cloud还是什么?麻烦把具体依赖贴一下

lwadsy commented 4 years ago

我是本地单机跑的,有这个问题

用的是Spring Cloud还是什么?麻烦把具体依赖贴一下 pom.xml

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

application.properties server.port=8081

servlet.context-path: /test

management.endpoints.web.exposure.include=* spring.cloud.nacos.config.server-addr=127.0.0.1:8848 spring.cloud.nacos.config.file-extension=properties

TestBean.java @Component @RefreshScope public class TestBean { @value("${a:0}") private String a; public String getA() { return a; } public void setA(String a) { this.a = a; } }

leach commented 4 years ago

windows服务器上配置的两个节点,也有这个问题 配置:

spring:
  #生效的配置文件:
  #开发环境请在开发工具中指定
  #测试环境、生产环境在启动命令中指定:--spring.profiles.active=sit
  profiles:
    active: sit
  #应用名称
  application:
    name: sdms
  cloud:
    nacos:
      config:
        server-addr: 192.168.52.24:8848,192.168.52.24:8849
        namespace: 201b00e4-a01d-48c0-bdd5-a74397711c16
        ext-config:
        - dataId: spring-${spring.profiles.active}.yml
          refresh: true
        - dataId: database-${spring.profiles.active}.yml
          refresh: true

springcloud配置:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${nacos-config-spring-cloud.version}</version>
        </dependency>
<nacos-config-spring-cloud.version>0.2.2.RELEASE</nacos-config-spring-cloud.version>
fesine commented 4 years ago

我单机模式启动,也发现此问题,配置参数修改发布后,只有第一次在客户端通知生效,第二次及之后的修改,客户端都不生效。使用的是spring cloud 方式

Ouyangan commented 4 years ago

单机模式 , spring-cloud项目同样的问题

nkorange commented 4 years ago

更新到最新的Spring Cloud客户端试下

Razor0719 commented 4 years ago

docker部署,同样的问题!

dennymou commented 4 years ago

碰到了同样的问题。k8s部署的 spring cloud模式下修改配置不生效

paderlol commented 4 years ago

碰到了同样的问题。k8s部署的 spring cloud模式下修改配置不生效

你有没有试过不使用任何容器

dennymou commented 4 years ago

碰到了同样的问题。k8s部署的 spring cloud模式下修改配置不生效

你有没有试过不使用任何容器

没有,兄弟你那可以吗?

canxer314 commented 4 years ago

nacos 1.1.4 windows server单机部署同样问题,没有接mysql

jianxuanbing commented 4 years ago

单机都有问题,mysql数据库里面的配置都变更了,获取配置依旧是以前旧的配置

hepan commented 4 years ago

docker nacos 1.2.0 cloud 2.2.0 增加 @RefreshScope 后可以刷新的

Fyee commented 4 years ago

spring.boot.version:2.1.12.RELEASE spring.cloud.version:Greenwich.SR5 同样的问题:nacos:1.2.1 偶尔出现服务重启读取的还是上次的值,只有重启nacos才能读取到最新的值。

dongwenting commented 4 years ago

同样的问题,本地,连MySQL,修改配置不生效,必须重启nacos nacos-server:1.2.1

fesine commented 4 years ago

同样的问题,本地,连MySQL,修改配置不生效,必须重启nacos nacos-server:1.2.1

重启,动态功能的意义就没有了。

batizhao commented 4 years ago
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>

Nacos 1.2.0 单机模式 嵌入式数据库

sh startup.sh -m standalone

目前的情况是,增加一个新的配置,必须要重启 nacos ,客户端应用才能加载。 修改配置是没有问题的。

dennymou commented 4 years ago

这。。。1W多star还没解决有点过了啊。。。

chuntaojun commented 4 years ago
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>

Nacos 1.2.0 单机模式 嵌入式数据库

sh startup.sh -m standalone

目前的情况是,增加一个新的配置,必须要重启 nacos ,客户端应用才能加载。 修改配置是没有问题的。

非cloud项目我都测试过能够监听到配置变更,你确定下是不是你的cloud的使用方式有问题

chuntaojun commented 4 years ago

请判断下[notify-listener] 这个日志有没有

whoamigodson commented 4 years ago

需要在bootstrap.properties中添加该配置,nacos修改配置发布后才会刷新到微服务中。 spring.cloud.nacos.config.refreshable-dataids=service-provider.properties

jerryi1 commented 4 years ago

cloud 项目,自己测试的发布没有效果,但是克隆了之后就生效了

zkzlx commented 4 years ago

refer Spring Cloud Alibaba Nacos Config

yuhuangbin commented 4 years ago

1、nacos-config相关的配置在bootstrap.yaml|properties|yml 2、开启data-id动态刷新 3、使用@RefreshScope注解

使用demo,请参照Nacos Config Example

yli042 commented 3 years ago

同1.14,这问题跟spring配置和注解啥的根本没关系啊,我新增一个配置或者修改已有配置,直接用curl命令都查不到,只能重启服务,就是nacos配置修改没动态生效。

pickjob commented 3 years ago
  1. 使用Restful API 看看有没有刷新, 未刷新
  2. 可以进入容器看看 logs/nacos.los 日志 ( docker container exec -it 容器名 /bin/bash ) 我的是 docker-compose 配的服务名 使用了 下划线 ( ) ,The character [] is never valid in a domain name. 可以参考下, 主要是错误日志没有打到 stdout, 没能发现异常,不友好.
chengqljava commented 3 years ago
2.2.5.RELEASE 2.2.1.RELEASE Hoxton.SR3 nacos 重启后 其它服务再次重启 com.alibaba.nacos.api.exception.NacosException: failed to req API:/api//nacos/v1/ns/service/list after all servers([127.0.0.1:8848]) tried: server is DOWN now, please try again later!
chengqljava commented 3 years ago
2.2.5.RELEASE 2.2.1.RELEASE Hoxton.SR3 nacos 单机启动完成 页面可以访问到 但启动服务 com.alibaba.nacos.api.exception.NacosException: failed to req API:/api//nacos/v1/ns/service/list after all servers([127.0.0.1:8848]) tried: server is DOWN now, please try again later!
chengqljava commented 3 years ago
2.2.5.RELEASE 2.2.1.RELEASE Hoxton.SR3 nacos 配置中心自动刷新无效 @RestController @RefreshScope // 开启动态刷新 public class ConfigController { @Value("${description.title:}") private String title; @Value("${hello:}") private String hello; @GetMapping("/description/title") public String descriptionTitle() { return title + "hello" + hello; } } 修改配置以后 刷新不到修改内容
xwsg commented 3 years ago
2.2.5.RELEASE 2.2.1.RELEASE Hoxton.SR3 nacos 配置中心自动刷新无效 @RestController @RefreshScope // 开启动态刷新 public class ConfigController { ``` @Value("${description.title:}") private String title; @Value("${hello:}") private String hello; @GetMapping("/description/title") public String descriptionTitle() { return title + "hello" + hello; } ``` } 修改配置以后 刷新不到修改内容

同样的问题,解决了么?

tigerba commented 3 years ago
2.2.5.RELEASE 2.2.1.RELEASE Hoxton.SR3 nacos 配置中心自动刷新无效 @RestController @RefreshScope // 开启动态刷新 public class ConfigController { ``` @Value("${description.title:}") private String title; @Value("${hello:}") private String hello; @GetMapping("/description/title") public String descriptionTitle() { return title + "hello" + hello; } ``` } 修改配置以后 刷新不到修改内容

同样的问题,解决了么?

同样的问题,解决了吗?

theodore414 commented 3 years ago

是不是必须要实现监听器才可以啊

ab2h commented 3 years ago

遇到同样的问题,感觉像是nacos服务端的事件发布及文件缓存有问题,必需重启nacos,跟client没啥关系

liu1491129734 commented 3 years ago

nacos 1.1.4版本,服务器是windows server 2012 ,当修改了nacos配置,重启项目,读取的还是之前的配置,这不可能是项目的问题,项目在linux系统跑没一点问题。项目启动从nacos读取配置文件有问题呀,读取的一直是原来的配置,大神解决一下呀。

liu1491129734 commented 3 years ago

同1.14,这问题跟spring配置和注解啥的根本没关系啊,我新增一个配置或者修改已有配置,直接用curl命令都查不到,只能重启服务,就是nacos配置修改没动态生效。

同1.1.4,你的解决了吗?,这个问题真的有点头疼呀。

theodore414 commented 3 years ago

同1.14,这问题跟spring配置和注解啥的根本没关系啊,我新增一个配置或者修改已有配置,直接用curl命令都查不到,只能重启服务,就是nacos配置修改没动态生效。

同1.1.4,你的解决了吗?,这个问题真的有点头疼呀。

我们工程用了 nettyserver 实现了 applicationlistener 但其实没用 然后就删了就可以更新了。 没具体研究为啥,希望nacos团队关注一下

beijingzhangwei commented 3 years ago
@NacosPropertySource(dataId = "rec-read", autoRefreshed = true)
// 启动类
@NacosValue(value = "${read_v2_openIds}",autoRefreshed = true)
private String[] readV2OpenIdList;

// 属性配置 即:@NacosPropertySource和@NacosValue中都要配置autoRefreshed设置为true才行。

beijingzhangwei commented 3 years ago
@NacosPropertySource(dataId = "rec-read", autoRefreshed = true)
// 启动类
@NacosValue(value = "${read_v2_openIds}",autoRefreshed = true)
private String[] readV2OpenIdList;

// 属性配置 即:@NacosPropertySource和@NacosValue中都要配置autoRefreshed设置为true才行。 本着不说上下文就是耍流氓的行为:补充依赖环境

org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE

and nacos version:

com.alibaba.boot
        <artifactId>nacos-config-spring-boot-starter</artifactId>
        <version>0.2.7</version>
xwsg commented 3 years ago

目前发现两种可能出现配置不生效的情况:

  1. 实现了CommandLineRunner 或者 ApplicationRunner, 并且在run方法中执行了阻塞任务,导致nacos无法正常工作

    public class SomeRunner implements CommandLineRunner {
    
    @Override
    public void run(String... args) throws Exception {
        // 这里执行了同步阻塞任务
    }
    }

    可以另起一个线程解决,比如new Thread

    public void run(String... args) throws Exception {
    new Thread(() -> {
        // 同步阻塞任务
    }).start();
    }
  2. 使用了 jasypt-spring-boot-starter 升级至最新版本可解决,目前最新版本为3.0.3
faithgreen332 commented 3 years ago

1.尝试过了,k8s部署的nacos,单实例的时候,改了配置,刷新能及时生效,但是多实例了就不行,看了pod里的cluster.conf,多实例的时候这个文件里配置是空的,不知道是不是在创建的时候哪里没配置对,倒是有一个想法是在yaml的command里手动写进去集群信息,具体没试过。 2.单实例的时候,修改配置是nacos-config服务打印日志有 Refresh keys changed:[所修改的key的集合],多实例的时候,打印的是group [],刷新的group的集合数组是空的。 3.集群的时候,exec进去看了各个pod里的data里的config-info里的数据是不一样的,pv文件夹下的各个pod对应的值也是各自是各自的,但是msql里的数据一直是最新的,感觉nacos-config客户端访问配置的时候根本没去到mysql就返回了,看起来要集群配置的时候配置的pv应该配置成多个pod共享一个(但是不知道怎么配),还是不知道怎么解决集群的问题。 4.没办法,就配置单实例来用了。

kevin-onepiece commented 3 years ago

我的解决是看nacos.log,发现里面有个 2021-08-02 16:31:01,601 ERROR [notify-exception] target:172.20.10.3:8848 dataId:nacos-config-example.properties group:DEFAULT_GROUP ts:1627892099953 ex:java.net.ConnectException: Timeout connecting to [/172.20.10.3:8848]

这个ip是我切换网络后,上一个网络下的地址,所以不要切换网络,就没问题了。

zhang929084299 commented 2 years ago

①controller加上@RefreshScope,让其可以支持读取实时配置 ②sale-server-loc.yaml,这个文件需要带上你的环境,命名规则:微服务名+环境+.yaml。

flover28 commented 2 years ago

将需要刷新的属性单独放在一个配置中 @Component @RefreshScope

使用@Autowired 注入

cc132 commented 2 years ago

我一开始也遇到了这个问题,即在springcloud接入方式下使用@Value+@RefreshScope无法获取修改之后的配置数据,自己maven依赖的版本如下:

2.6.4 2.2.6.RELEASE 2021.0.3 nacos服务端版本2.1.1(这边是自己学习的例子,因此nacos客户端和服务端版本并没有完全一致) 参考Nacos官网中的demo代码如下: @RestController @RequestMapping("/config") @RefreshScope public class ConfigController { @Value("${useLocalCache:1111}") private String useLocalCache; @GetMapping("/cache") public String get() { return useLocalCache; } } 经过我运行之后,在nacos服务页面更改对应dateId中的useLocalCache之后,IDEA中console日志台确实有了一些数据更新信息,但是日志中的dataId是错误的,dataId的组成由{spring.application.name}-{spring.profiles.active}-{file-extension}这三部分组成。后来我查看我的**spring.application.name是放在了appliacation.yml中,不是放在bootstrap.yml中**,将sprg.application.name放到bootstrap.yml下可以获取则可以获取到动态更新的数据 个人猜测可能获取更新数据时,nacos里面生成dataID时指定了spring.application.name必须从bootstrap中获取。。。
cedricye commented 8 months ago

我一开始也遇到了这个问题,即在springcloud接入方式下使用@value+@RefreshScope无法获取修改之后的配置数据,自己maven依赖的版本如下: 2.6.4</spring.boot.version> 2.2.6.RELEASE</alibaba.cloud.version> 2021.0.3</spring.cloud.version> nacos服务端版本2.1.1(这边是自己学习的例子,因此nacos客户端和服务端版本并没有完全一致)

参考Nacos官网中的demo代码如下: @RestController @RequestMapping("/config") @RefreshScope public class ConfigController { @value("${useLocalCache:1111}") private String useLocalCache;

@GetMapping("/cache")
public String get() {
    return useLocalCache;
}

}

经过我运行之后,在nacos服务页面更改对应dateId中的useLocalCache之后,IDEA中console日志台确实有了一些数据更新信息,但是日志中的dataId是错误的,dataId的组成由{spring.application.name}-{spring.profiles.active}-{file-extension}这三部分组成。后来我查看我的spring.application.name是放在了appliacation.yml中,不是放在bootstrap.yml中,将sprg.application.name放到bootstrap.yml下可以获取则可以获取到动态更新的数据

个人猜测可能获取更新数据时,nacos里面生成dataID时指定了spring.application.name必须从bootstrap中获取。。。

我把application.name放到bootstrap.yaml下解决了问题。