春季框架未检测到对ConfigMap的更改。

huangapple go评论77阅读模式
英文:

Spring not picking up changes to ConfigMap

问题

我有一个使用Spring Boot的应用程序,我正在尝试使用以下方法实现即时配置更新。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.3.1-RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-kubernetes-config</artifactId>
    <version>1.1.4.RELEASE</version>
</dependency>

我尝试按照这篇文章 https://medium.com/swlh/kubernetes-configmap-confuguration-and-reload-strategy-9f8a286f3a44 进行操作,并且已经成功让 Spring 从 ConfigMap 中获取配置,但是,如果在应用程序运行时更新 ConfigMap,Spring 不会自动加载新配置。这是我的 bootstrap.yml 配置:

spring:
  cloud:
    kubernetes:
      config:
        enabled: true
        sources:
          - namespace: default
            name: hello-world
      reload:
        enabled: true
        mode: event
        strategy: refresh

我也尝试过使用 mode: polling,但是仍然没有效果。并且我已经添加了 view 角色权限:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "hello-world.fullname" . }}-view
roleRef:
  kind: ClusterRole
  name: view
  apiGroup: rbac.authorization.k8s.io
subjects:
  # Authorize specific service accounts:
  - kind: ServiceAccount
    name: {{ include "hello-world.serviceAccountName" . }}
    namespace: default

我在想,也许是我在 Java 中加载配置的方式有问题?

@ConfigurationProperties(prefix = "spring.app")
@Bean
public Properties appProperties() {
    return new Properties();
}

@Autowired
@Qualifier("appProperties")
private Properties props;

我的 ConfigMap 配置:

apiVersion: v1
kind: ConfigMap
metadata:
  name: hello-world
data:
  application.yml: |-
    spring:
      app:
        message: Hello    

然后我通过 props.getProperty("message") 来访问值。

更新:

所以我通过启用执行器的刷新端点来使配置变更生效:

management:
  endpoint:
    restart:
      enabled: true

但是现在我有一个新问题,这是否是必需的?有没有办法在不包含执行器的情况下使此功能工作?

英文:

I have an application that is using Springboot, I am trying to allow on the fly configuration updates using the following.

    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
        &lt;version&gt;2.3.1-RELEASE&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
        &lt;artifactId&gt;spring-cloud-starter-kubernetes-config&lt;/artifactId&gt;
        &lt;version&gt;1.1.4.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;

I have tried to follow this article https://medium.com/swlh/kubernetes-configmap-confuguration-and-reload-strategy-9f8a286f3a44, and have managed to get Spring pulling the config from the ConfigMap, however, if I update the ConfigMap when the application is running, spring does not pick it up. This is my bootstrap.yml

spring:
  cloud:
    kubernetes:
      config:
        enabled: true
        sources:
          - namespace: default
            name: hello-world
      reload:
        enabled: true
        mode: event
        strategy: refresh

I have also tried using mode: polling, but still no change. And I have added the view role.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include &quot;hello-world.fullname&quot; . }}-view
roleRef:
  kind: ClusterRole
  name: view
  apiGroup: rbac.authorization.k8s.io
subjects:
  # Authorize specific service accounts:
  - kind: ServiceAccount
    name: {{ include &quot;hello-world.serviceAccountName&quot; . }}
    namespace: default

I am thinking maybe it's the way I am loading my configuration in Java?

@ConfigurationProperties(prefix = &quot;spring.app&quot;)
@Bean
public Properties appProperties() {
    return new Properties();
}

@Autowired
@Qualifier(&quot;appProperties&quot;)
private Properties props;

My ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: hello-world
data:
  application.yml: |-
    spring:
      app:
        message: Hello

I am then accessing values like props.getProperty(&quot;message&quot;)

UPDATE:

So I managed to get the changes picked up, by enabling the actuator refresh endpoint:

management:
  endpoint:
    restart:
      enabled: true

But now I have a new question, is this necessary? Is there any way to get this to work without including the actuator?

答案1

得分: 1

请确保您在POM文件中具有以下依赖项及其版本。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

我曾经遇到类似的问题,添加了这个依赖后问题得到解决。

英文:

Please make sure you have the following dependency in your POM with a version.

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
&lt;/dependency&gt;

I had similar issue, which is resolved after adding this.

答案2

得分: 0

请确保将 @RefreshScope 添加到您希望使用配置映射中的值进行热重新加载和注入的 bean 中。

英文:

make sure to add @RefreshScope to the bean that you want to hot reload with inject with values from config maps.

huangapple
  • 本文由 发表于 2020年7月25日 21:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63088887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定