Spring配置其他位置作为docker-compose中的环境变量

时间:2019-05-02 15:20:06

标签: java spring-boot docker-compose

我试图让Spring从jar之外的文件中添加属性。

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

我有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

2 个答案:

答案 0 :(得分:0)

您还可以使用以下格式:

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"

参考  -Environment variable for docker compose

答案 1 :(得分:0)

嗯,这太疯狂了……

我更改了启动该Java应用程序的Dockerfile,因此现在我可以将其他配置位置作为命令行参数传递。我还添加了一个bash脚本来启动我的Java应用。

Dockerfile

declare @table table (MY_XML xml)

insert into @table select
'<DN_CHARGE_LINES>
    <DN_CHARGE_LINE LINE_ID="201903297661150">
        <CHARGE_NAME>HANDLING CHARGE</CHARGE_NAME>
        <CURRENCY_CODE>USD</CURRENCY_CODE>
        <RATE>75</RATE>
        <QTY>1</QTY>
        <LINE_AMOUNT>75</LINE_AMOUNT>
        <JOB_ID>201903137354913</JOB_ID>
    </DN_CHARGE_LINE>
    <DN_CHARGE_LINE LINE_ID="123123123123123">
        <CHARGE_NAME>HANDLING CHARGE 2</CHARGE_NAME>
        <CURRENCY_CODE>EUR</CURRENCY_CODE>
        <RATE>100</RATE>
        <QTY>2</QTY>
        <LINE_AMOUNT>10</LINE_AMOUNT>
        <JOB_ID>201903137565987</JOB_ID>
    </DN_CHARGE_LINE>
    <DN_CHARGE_LINE LINE_ID="123456789123456">
        <CHARGE_NAME>HANDLING CHARGE 3</CHARGE_NAME>
        <CURRENCY_CODE>USD</CURRENCY_CODE>
        <RATE>3</RATE>
        <QTY>4</QTY>
        <LINE_AMOUNT>30</LINE_AMOUNT>
        <JOB_ID>201903137111222</JOB_ID>
    </DN_CHARGE_LINE>
</DN_CHARGE_LINES>'

select  
     cn.s.value('@LINE_ID'        , 'nvarchar(max)') AS LINE_ID
    ,cn.s.value('CHARGE_NAME[1]'  , 'nvarchar(max)') AS CHARGE_NAME
    ,cn.s.value('CURRENCY_CODE[1]', 'nvarchar(max)') AS CURRENCY_CODE
    ,cn.s.value('RATE[1]'         , 'nvarchar(max)') AS RATE
    ,cn.s.value('QTY[1]'          , 'nvarchar(max)') AS QTY
    ,cn.s.value('JOB_ID[1]'       , 'nvarchar(max)') AS JOB_ID
from @table
CROSS APPLY MY_XML.nodes('//DN_CHARGE_LINE') cn(s)

startJavaApp.sh

ENTRYPOINT ./startJavaApp.sh

看起来像将其作为命令行参数传递一样有效。