【发布时间】:2020-07-25 13:23:56
【问题描述】:
我有一个使用 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
但是现在我有一个新问题,这有必要吗?有什么方法可以在不包括执行器的情况下使其工作?
【问题讨论】:
-
从概念的角度来看,1)外部调用 spring boot 应用程序并告诉它刷新上下文 2)没有端点可以调用 spring boot 应用程序来告诉它刷新,而是不断轮询具有配置时间的配置,如果它更改并刷新上下文。所以我猜你已经完成了第一个,我不知道 spring boot 是否已经为 2) 内置了机制,或者你必须使用
@Scheduler构建它 -
正在重新加载配置,但使用此属性的 Spring bean 未刷新。我们是否需要添加额外的配置以使依赖的 bean 也得到刷新?。
标签: java spring spring-boot spring-cloud-config configmap