spring-cloud / spring-cloud-kubernetes

Kubernetes integration with Spring Cloud Discovery Client, Configuration, etc...
Apache License 2.0
3.46k stars 1.03k forks source link

Why is dynamic refresh not working? Please help #1683

Closed jmilktea closed 2 months ago

jmilktea commented 2 months ago

Describe the bug Dynamic refresh did not take effect. It was found that the EventBasedConfigMapChangeDetector bean was not injected into the spring container.

Sample My project is very simple. I want to implement the refresh function of k8s config modification. However, I found that it can be read when it starts, but the memory value cannot be refreshed when the k8s config map is modified. After logging, no error log was found. I can find "ConfigReloadAutoConfiguration.ConfigReloadAutoConfigurationBeans#configMapPropertyChangeEventWatcher matched" from the log, buf found that the spring container does not have EventBasedConfigMapChangeDetector this bean.What is the problem?

source code:

        @Bean
        @ConditionalOnBean(Fabric8ConfigMapPropertySourceLocator.class)
        @Conditional(EventReloadDetectionMode.class)
        public ConfigurationChangeDetector configMapPropertyChangeEventWatcher(ConfigReloadProperties properties,
                ConfigurationUpdateStrategy strategy,
                Fabric8ConfigMapPropertySourceLocator fabric8ConfigMapPropertySourceLocator) {

            return new EventBasedConfigMapChangeDetector(this.environment, properties, this.kubernetesClient, strategy,
                    fabric8ConfigMapPropertySourceLocator);
        }
@Data
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "k8s")
public class TestData {
    private String name = "default";
}

my application.yml config:

server:
  port: 8020
  tomcat:
    mbeanregistry:
      enabled: true

spring:
  application:
    name: myapp
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER
  main:
    allow-circular-references: true
  cloud:
    kubernetes:
      reload:
        enabled: true
        strategy: refresh
        mode: event
      config:
        enabled: true
        namespace: test01
        include-not-ready-addresses: true
        include-profile-specific-sources: false
        name: myapp

debug: true

management:
  endpoints:
    web:
      exposure:
        include: '*'

my application dependency:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes-fabric8-all</artifactId>
            <version>2.1.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

k8s config map,I have granted the default serviceaccount the [get list watch] permission

apiVersion: v1
data:
  myapp.yml: |-
    k8s:
      name: default8
kind: ConfigMap
metadata:
  creationTimestamp: '2024-08-06T07:30:17Z'
  name: myapp
  namespace: test01
  resourceVersion: '5552564831'
  uid: 533880b0-6c25-4332-a197-8fb09f00f4b4
wind57 commented 2 months ago

I will gladly take a look and help here, but for that can you please create a github demo with an application and clear instructions what you are trying to do? I mean, in this way, we could clone it and see what is going on

jmilktea commented 2 months ago

@wind57 I have uploaded the code, please take a look https://github.com/jmilktea/spring-cloud-kubernetes

wind57 commented 2 months ago

you are using a version that we no longer support. You will need to upgrade to spring cloud 3.x.x, otherwise there is no one who will help as there are no fixes in the version that you use

jmilktea commented 2 months ago

It is a pity that we are still using this version. The code is also very simple, but I don’t know what went wrong. The EventBasedConfigMapChangeDetector bean is not injected into the spring container. @wind57

wind57 commented 2 months ago

sorry, I can't help you, that version is out of life. unless you upgrade, no one here can do anything.