【问题标题】:Docker compose and Spring Boot Config serverDocker compose 和 Spring Boot 配置服务器
【发布时间】:2017-12-29 12:16:37
【问题描述】:

我有一个工作的 Spring Boot Web 应用程序,它从 Spring Boot 配置服务器获取其配置属性。我的应用程序有 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 种不同的环境。有人有样品吗?

【问题讨论】:

    标签: java docker spring-boot


    【解决方案1】:

    JHispter 的人就是这样做的(请参阅下面的文件)

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

    SPRING_PROFILES_ACTIVE=prod,swagger
    

    所以,从那时起,application.ymlbootstrap.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
    

    mysql.yml(如果需要)

    version: '2'
    services:
        my-app-mysql:
            image: mysql:5.7.18
            # volumes:
            #     - ~/volumes/my-app/mysql/:/var/lib/mysql/
            environment:
                - MYSQL_USER=root
                - MYSQL_ALLOW_EMPTY_PASSWORD=yes
                - MYSQL_DATABASE=my-app
            ports:
                - 3306:3306
            command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8 --explicit_defaults_for_timestamp
    

    bootstrap.yml

    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。这是Docker Compose 文件文档。
    猜你喜欢
    • 2021-05-14
    • 2020-10-08
    • 2014-12-20
    • 2018-11-28
    • 1970-01-01
    • 2018-07-27
    • 2019-04-12
    • 2018-12-21
    • 2016-02-02
    相关资源
    最近更新 更多