【问题标题】:How can we set different environment profile in spring boot?我们如何在 Spring Boot 中设置不同的环境配置文件?
【发布时间】:2019-01-10 22:57:54
【问题描述】:

我正在使用spring boot 处理microservices。我有大约 5 个microservices。所以我将spring cloud config server配置为集中配置。它运行良好。

配置服务器配置

spring.profiles.active=native

spring.cloud.config.server.native.search-locations=classpath:/common-config

server.port=8888

现在的问题是我有两个环境 dev 和 test 并且我创建了 application-test.properties 和 application-dev.properties 根据 spring 记录我需要设置配置文件像

春季简介

spring.profiles.active=test

但它已经设置为原生那么我如何加载多个配置文件。

请帮帮我..

【问题讨论】:

    标签: spring spring-cloud-config spring-profiles


    【解决方案1】:

    您可以在每个应用程序的运行时将活动配置文件名称作为环境变量传递。这将优先于属性文件中列出的活动配置文件。实际上,您可以对任何属性执行此操作。如果你使用 docker 来启动你的应用,你可以从你的 Dockerfile 或者你的 docker-compose.yml 中传入它

    【讨论】:

    • 是的,我试过了,但它没有获取配置文件并抛出与 db url 相关的错误
    • 嗯好的!请您提供一个如何传递环境变量的示例?哦,可能值得将配置文件名称移动到 bootstrap.properties 文件,因为它之前已加载
    • stackoverflow.com/questions/40981861/… 上的示例可能在这里有用
    【解决方案2】:

    您可以设置 spring.profiles.active=$ACTIVE_SPRING_PROFILE 并设置 OS 环境 ACTIVE_SPRING_PROFILE = test

    【讨论】:

      【解决方案3】:

      您可以在 Spring 中使用多个配置文件。试试:

      spring.profiles.active=dev,native
      

      查看更多:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles

      【讨论】:

      • 它给我同样的错误“确定数据库类型 NONE 的嵌入式数据库 url”(当前没有活动的配置文件)
      • 您可能没有提供足够的信息来自动配置数据源。或者,如果您不使用数据库访问 - 您不应该将 spring-boot-starter-data-jpa 添加到 pom (有时可能会被错误地复制到那里)。无论如何,没有源代码很难猜测。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      • 1970-01-01
      相关资源
      最近更新 更多