reload configmap in Spring Boot 3 does not work

99 views Asked by At

Reload configmap to get properties is working well with Spring Boot 2.7, but it does not with Spring Boot 3.x. Anyone can help me? This is my codebase, https://github.com/lkseoqkr/configmaptest/tree/main

environment & dependency

  • minikube
  • Java17
  • org.springframework.boot 3.0.9
  • io.spring.dependency-management 1.1.3
  • org.springframework.cloud:spring-cloud-dependencies:2022.0.4
  • org.springframework.cloud:spring-cloud-starter-kubernetes-fabric8-all

application.properties

spring.application.name=configmaptest
spring.main.cloud-platform=kubernetes
spring.cloud.config.enabled=true

spring.cloud.kubernetes.config.enabled=true
spring.cloud.kubernetes.config.name=configmaptest
spring.cloud.kubernetes.config.namespace=default

spring.cloud.kubernetes.discovery.enabled=true
spring.cloud.kubernetes.discovery.all-namespaces=true
spring.cloud.kubernetes.discovery.namespaces=default

spring.cloud.kubernetes.reload.monitoring-config-maps=true
spring.cloud.kubernetes.reload.enabled=true
spring.cloud.kubernetes.reload.strategy=refresh

management.endpoint.refresh.enabled=true
management.endpoints.web.exposure.include=*

logging.level.org.springframework.cloud=debug

configmap-property.username=configmap-test_user
configmap-property.id=1234
configmap-property.enable=true

refresh-property.username=refresh-test_user
refresh-property.id=4321
refresh-property.enable=true

Application.java

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigmaptestApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigmaptestApplication.class, args);
    }
}

Controller.java

@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
@RestController
public class ConfigmaptestController {

  @Autowired
  private ConfigmapProperties configmapProperties;

  @GetMapping("configmap-properties")
  public String getConfigmapProperties() {
    return String.format("username: %s, id: %d, enable: %b",
        configmapProperties.getUsername(), configmapProperties.getId(), configmapProperties.getEnable());
  }
}

Configuration.java

@Configuration
@ConfigurationProperties(prefix = "configmap-property")
@Data
public class ConfigmapProperties {
  private String username;

  private Integer id;

  private Boolean enable;
}

deploy.yaml for minikube

---
apiVersion: v1
kind: List
items:
  - apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: configmaptest
      name: configmaptest
      namespace: default
    spec:
      ports:
        - name: http
          port: 8080
          targetPort: 8080
          nodePort: 30950
      selector:
        app: configmaptest
      type: LoadBalancer
  - apiVersion: v1
    kind: ServiceAccount
    metadata:
      labels:
        app: configmaptest
      name: configmaptest
      namespace: default
  - apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      labels:
        app: configmaptest
      name: configmaptest
      namespace: default
    roleRef:
      kind: ClusterRole
      apiGroup: rbac.authorization.k8s.io
      name: configmaptest
    subjects:
      - kind: ServiceAccount
        name: configmaptest
        namespace: default
  - apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      namespace: default
      name: configmaptest
    rules:
      - apiGroups: [""]
        resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
        verbs: ["get", "list", "watch"]
  - apiVersion: v1
    kind: ConfigMap
    metadata:
      name: configmaptest
      namespace: default
    data:
      application.properties: |-
        configmap-property.username=configmap-test_user
        configmap-property.id=1234
        configmap-property.enable=true
        refresh-property.username=refresh-test_user
        refresh-property.id=4321
        refresh-property.enable=true
  - apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: configmaptest
      namespace: default
      labels:
        app: configmaptest
    spec:
      selector:
        matchLabels:
          app: configmaptest
      template:
        metadata:
          name: configmaptest
          namespace: default
          labels:
            app: configmaptest
        spec:
          serviceAccountName: configmaptest
          containers:
            - name: configmaptest
              image: configmaptest:latest
              imagePullPolicy: IfNotPresent
              ports:
                - containerPort: 8080
0

There are 0 answers