Decoupling DB connection strings from spring boot api (K8s, Docker, Spring, Java)

huangapple 未分类评论43阅读模式
英文:

Decoupling DB connection strings from spring boot api (K8s, Docker, Spring, Java)

问题

问题:我有一个非常简单的Spring Boot API,其中包含硬编码的数据库连接字符串。我想从我的K8s集群中的一个Pod中提供这些连接字符串,但我在告诉API从环境变量中读取时遇到了问题。

我的Dockerfile:

FROM gcr.io/distroless/java:8
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

这是db-secret.yaml文件:

apiVersion: v1
kind: Secret
metadata:
  name: db-secret
data:
  host: XXXX
  user: XXXX
  password: XXXX
  database: XXXX

这是我的API Pod部署文件中的环境部分:

          env:
            - name: ORIGIN
              value: https://myclient.app.com
            - name: HOST
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: host
            - name: PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: password
            - name: USERNAME
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: user
            - name: DATABASE
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: database

这是我的Java application.properties文件:

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=${HOST}/"todos"
spring.datasource.username=${USERNAME}
spring.datasource.password=${PASSWORD}

然而...当我尝试使用./mvnw clean package构建应用程序时,我收到以下错误消息:

  • java.lang.IllegalStateException: Failed to load ApplicationContext
  • BeanCreationException: Error creating bean with name 'entityManagerFactory'

我尝试通过命令行导出变量,但这并没有起作用。我知道Node.js有process.env.SOME_VAR这样的东西。Java有类似的吗?我已经在互联网上搜索了一段时间,并尝试了不同的解决方案,但目前为止没有什么作用。

英文:

Problem: I have a VERY simple spring boot api with hard coded db connection strings. I would like to feed these connection strings FROM a pod in my K8s cluster but I am having issues with telling the api to read from env variables.

My Dockerfile:

FROM gcr.io/distroless/java:8
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

This is the db-secret.yaml file:

apiVersion: v1
kind: Secret
metadata:
  name: db-secret
data:
  host: XXXX
  user: XXXX
  password: XXXX
  database: XXXX

This is the env section of my api pod my deployment file:

          env:
            - name: ORIGIN
              value: https://myclient.app.com
            - name: HOST
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: host
            - name: PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: password
            - name: USERNAME
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: user
            - name: DATABASE
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: database

This is my Java application.properties file:

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=${HOST}/"todos"
spring.datasource.username=${USERNAME}
spring.datasource.password=${PASSWORD}

However...when I try to build the app using ./mvnw clean package i get:

  • java.lang.IllegalStateException: Failed to load ApplicationContext
  • BeanCreationException: Error creating bean with name 'entityManagerFactory'

I tried exporting the variables via command line but that didn't work. I know node has process.env.SOME_VAR. Is there something like this for Java? I've been combing through the interwebs for a while and trying different solutions but not much is working at this point.

答案1

得分: 0

加载环境变量需要一些配置,否则就不会有任何内容在寻找 db-secret.yaml 文件。

一种方法是将配置值直接放入 application.propertiesapplication.yml 文件中。您还可能需要确保这些文件不会被提交到存储库中(例如添加到 .gitignore 中)。

英文:

Loading environment variables requires some configuration, otherwise there won't be anything looking for a db-secret.yaml file.

One approach is to place the configuration values directly into application.properties or application.yml. You will probably also need to make sure that those files are not committed into a repository (.e.g. added to .gitignore).

huangapple
  • 本文由 发表于 2020年4月10日 08:37:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/61132380.html
匿名

发表评论

匿名网友

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

确定