我试图告诉 Spring 从 jar 外部的文件添加属性。
我有 docker compose 文件,我在其中为 Spring Boot 应用程序定义环境变量。
sample-service:
build:
context: "./sample-service/app"
dockerfile: Dockerfile
container_name: sample-service
restart: always
mem_limit: 400000000
env_file: .env
environment:
- "SPRING_PROFILE=dev"
- "APPLICATION_NAME=sample-service"
- "SPRING_CONFIG_ADDITIONAL-LOCATION=file:/app/sample-service-config.yml"
- "JAVA_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5006"
ports:
- 5006:5006
当我在调试器中时,我执行自定义代码。
System.getenv("SPRING_PROFILE")
它返回dev
。当我运行时
System.getenv("APPLICATION_NAME")
它返回sample-service
。当我运行时
System.getenv("SPRING_CONFIG_ADDITIONAL-LOCATION")
它返回null
。
更新
有人建议docker不支持连字符 https://github.com/docker/compose/issues/2370
最佳答案
您还可以使用以下格式:
env_file: .env
environment:
- SPRING_PROFILE=dev
- APPLICATION_NAME=sample-service
- SPRING_CONFIG_ADDITIONAL-LOCATION="file:/app/sample-service-config.yml"
- JAVA_OPTIONS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5006"
关于java - Spring 配置附加位置作为 docker-compose 中的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55955586/