【发布时间】:2020-07-01 04:45:09
【问题描述】:
我正在我的 docker-compose.yaml 文件中设置一个环境变量,并希望在我的 Spring Boot 的 application.yaml 中使用该变量的值。有人告诉我做类似的事情
app:
auth:
tokenSecret: tokensecretvaluehere
tokenExpirationMsec: 864000000
oauth2:
sso:
url: ${SSO_URL}
(其中 SSO_URL 在我的 docker-compose.yaml 中定义)在我的 Spring application.yaml 中。但是,当我运行 docker-compose up --build 时,这会导致错误,因为它找不到该变量(错误类似于:无法解析值“${SSO_URL}”中的占位符 SSO_URL)。这是我的 docker-compose.yaml 的一个示例:
api:
restart: always
ports:
- "8080:8080"
links:
- redis
- db
environment:
- SERVER_SERVLET_SESSION_COOKIE_DOMAIN=localhost
- SSO_URL=myvaluehere
我被要求不要在 Java 中使用 System.getenv 函数,而是像上面一样设置变量。从那里我只需使用 @Value 注释在我的 Java 代码中获取它,如下所示:
@Value("${app.oauth2.sso.url}")
private String ssoUrl;
这更多的是application.yaml:
heb:
togglr:
jwt:
secret:
id: 101
session:
seconds: 600
tokenheader: X-TOGGLR-TOKEN
logging:
level:
com:
heb: debug
default: debug
path: logs
server:
error:
whitelabel:
enabled: false
port: 8080
servlet:
context-path: /togglr-api
use-forward-headers: true
spring:
application:
name: togglr_api
freemarker:
enabled: false
groovy:
template:
enabled: false
jmx:
enabled: false
main:
banner-mode: 'off'
thymeleaf:
cache: false
security:
oauth2:
client:
registration:
github:
clientId:
clientSecret:
redirectUri:
scope:
- user:email
- read:user
app:
auth:
tokenSecret:
tokenExpirationMsec: 864000000
oauth2:
sso:
url: ${SSO_URL}
【问题讨论】:
标签: spring-boot docker docker-compose