Spring 未获取对 ConfigMap 的更改
Spring not picking up changes to ConfigMap
我有一个正在使用 Springboot 的应用程序,我正在尝试允许使用以下内容进行动态配置更新。
<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;
我的配置图:
apiVersion: v1
kind: ConfigMap
metadata:
name: hello-world
data:
application.yml: |-
spring:
app:
message: Hello
然后我访问 props.getProperty("message")
这样的值
更新:
所以我设法通过启用执行器刷新端点来获取更改:
management:
endpoint:
restart:
enabled: true
但是现在我有一个新的问题,这个有必要吗?有没有办法在不包括执行器的情况下让它工作?
请确保您的 POM 中有以下依赖版本。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
我有类似的问题,添加后解决了。
确保将 @RefreshScope
添加到要使用配置映射中的值注入热重载的 bean。
我有一个正在使用 Springboot 的应用程序,我正在尝试允许使用以下内容进行动态配置更新。
<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;
我的配置图:
apiVersion: v1
kind: ConfigMap
metadata:
name: hello-world
data:
application.yml: |-
spring:
app:
message: Hello
然后我访问 props.getProperty("message")
更新:
所以我设法通过启用执行器刷新端点来获取更改:
management:
endpoint:
restart:
enabled: true
但是现在我有一个新的问题,这个有必要吗?有没有办法在不包括执行器的情况下让它工作?
请确保您的 POM 中有以下依赖版本。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
我有类似的问题,添加后解决了。
确保将 @RefreshScope
添加到要使用配置映射中的值注入热重载的 bean。