如何在springboot中设置连接mysql的动态值?



目前,我在我的springbootapplication.dev.yaml:

datasource:
url: jdbc:mysql://mysql/$DB_HOST?useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver

我想在用户名,密码,$DB_HOST字段中添加一个动态值,以便它可以从Kubernetes中的秘密文件中选择这些值。Kubernetes中的secrets文件使用base64加密

在Spring Boot中,任何属性都可以被同名的环境变量覆盖,将字符更改为大写,将点更改为下划线。

例如数据源。url可以通过设置一个环境变量来覆盖,比如在Kubernetes

中定义的DATASOURCE_URL。来源:https://developers.redhat.com/blog/2017/10/04/configuring-spring-boot-kubernetes-secrets设置