Java Docker compose和Spring引导配置服务器

Java Docker compose和Spring引导配置服务器,java,docker,spring-boot,Java,Docker,Spring Boot,我有一个工作的SpringBootWeb应用程序,它从SpringBootConfigServer获取其配置属性。我的应用程序有3个环境-dev、qa和prod。在我的资源文件夹中,我有3个文件bootstrap-dev.properties、bootstarp-qa.properties和bootstrap-prod.properties。下面显示了一个示例引导文件 spring.application.name=MyApp spring.cloud.config.uri=http://my.

我有一个工作的SpringBootWeb应用程序,它从SpringBootConfigServer获取其配置属性。我的应用程序有3个环境-dev、qa和prod。在我的资源文件夹中,我有3个文件bootstrap-dev.properties、bootstarp-qa.properties和bootstrap-prod.properties。下面显示了一个示例引导文件

spring.application.name=MyApp
spring.cloud.config.uri=http://my.config.server
spring.cloud.config.label=feature-branch-101
spring.output.ansi.enabled=ALWAYS
spring.profiles.active=dev
我需要在docker compose文件中放入哪些内容,这些文件可以从spring boot配置服务器读取配置并支持3种不同的环境。有人有样品吗?

这是他们的工作方式(见下面的文件)

我认为诀窍是在
app.yml
文件中设置要为应用程序的环境运行的配置文件。像这样:

SPRING_PROFILES_ACTIVE=prod,swagger
因此,从那时起,
application.yml
bootstrap.yml
就知道了要加载到SpringBoot中的环境属性

Dockerfile

FROM openjdk:8-jre-alpine

ENV SPRING_OUTPUT_ANSI_ENABLED=ALWAYS \
    DELAY_SLEEP=0 \
    JAVA_OPTS=""

# add directly the war
ADD *.war /app.war

EXPOSE 8080
CMD echo "The application will start in ${DELAY_SLEEP}s..." && \
    sleep ${DELAY_SLEEP} && \
    java ${JAVA_OPTS} -jar /app.war
app.yml

version: '2'
services:
    my-app:
        image: myapp
        environment:
            - SPRING_PROFILES_ACTIVE=prod,swagger
            - SPRING_DATASOURCE_URL=jdbc:mysql://my-app-mysql:3306/my-app?useUnicode=true&characterEncoding=utf8&useSSL=false
            - DELAY_SLEEP=10 # gives time for the database to boot before the application
        ports:
            - 8080:8080
    my-app-mysql:
        extends:
            file: mysql.yml
            service: my-app-mysql
spring:
    application:
        name: my-app
    profiles:
        # The commented value for `active` can be replaced with valid Spring profiles to load.
        # Otherwise, it will be filled in by maven when building the WAR file
        # Either way, it can be overridden by `--spring.profiles.active` value passed in the commandline or `-Dspring.profiles.active` set in `JAVA_OPTS`
        active: #spring.profiles.active#
    cloud:
        config:
            fail-fast: false # if not in "prod" profile, do not force to use Spring Cloud Config
            uri: http://admin:admin@localhost:8761/config
            # name of the config server's property source (file.yml) that we want to use
            name: MyApp
            profile: dev # profile(s) of the property source
            label: master # toggle to switch to a different version of the configuration as stored in git
            # it can be set to any label, branch or commit of the config source git repository
mysql.yml(如果需要)

bootstrap.yml

version: '2'
services:
    my-app:
        image: myapp
        environment:
            - SPRING_PROFILES_ACTIVE=prod,swagger
            - SPRING_DATASOURCE_URL=jdbc:mysql://my-app-mysql:3306/my-app?useUnicode=true&characterEncoding=utf8&useSSL=false
            - DELAY_SLEEP=10 # gives time for the database to boot before the application
        ports:
            - 8080:8080
    my-app-mysql:
        extends:
            file: mysql.yml
            service: my-app-mysql
spring:
    application:
        name: my-app
    profiles:
        # The commented value for `active` can be replaced with valid Spring profiles to load.
        # Otherwise, it will be filled in by maven when building the WAR file
        # Either way, it can be overridden by `--spring.profiles.active` value passed in the commandline or `-Dspring.profiles.active` set in `JAVA_OPTS`
        active: #spring.profiles.active#
    cloud:
        config:
            fail-fast: false # if not in "prod" profile, do not force to use Spring Cloud Config
            uri: http://admin:admin@localhost:8761/config
            # name of the config server's property source (file.yml) that we want to use
            name: MyApp
            profile: dev # profile(s) of the property source
            label: master # toggle to switch to a different version of the configuration as stored in git
            # it can be set to any label, branch or commit of the config source git repository

我建议将Compose文件更新为最新版本,而不是第2版。这是文件文档。