英文:
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.
<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>
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 "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
I am thinking maybe it's the way I am loading my configuration in Java?
@ConfigurationProperties(prefix = "spring.app")
@Bean
public Properties appProperties() {
return new Properties();
}
@Autowired
@Qualifier("appProperties")
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("message")
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.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论