在 Spring Cloud 中的 bootstrap.yml 上设置配置文件以针对不同的配置服务器

Posted

技术标签:

【中文标题】在 Spring Cloud 中的 bootstrap.yml 上设置配置文件以针对不同的配置服务器【英文标题】:Set profile on bootstrap.yml in spring cloud to target different config server 【发布时间】:2016-08-24 09:51:00 【问题描述】:

我使用 docker compose 来运行我所有的微服务。对于每项服务,我都会给它一个简短的主机名。

version: '2'

services: 
  config:
    image: springbox-config-server
    restart: always
    ports:
     - "8890:8890"

  discovery:
    image: springbox-eureka
    restart: always
    ports:
     - "8763:8763"

因此,在我的微服务中,我必须使用短主机名来定位配置服务器。

spring:
  application:
    name: myservice
  cloud:
    config:
      uri: http://config:8890
      fail-fast: true

但是,当我在没有 docker 的 IDE 中本地运行它们时,无法解析短主机名。

所以我正在寻找一种解决方案,根据我的环境针对不同的配置服务器。

【问题讨论】:

【参考方案1】:

我找到了解决方案。基本上,我们使用 spring profile 来丰富 bootstrap 文件。例如

spring:
  application:
    name: myservice
  cloud:
    config:
      uri: http://config:8890
      fail-fast: true

---
spring:
  profiles: development
  cloud:
    config:
      uri: http://localhost:8890

好消息是我们不必重写配置文件中的所有属性。默认属性是继承的。例如,启用开发配置文件后,我的应用程序名称继承自名为 always myservice 的默认名称。

要激活配置文件,请使用以下属性启动服务

-Dspring.profiles.active=development

【讨论】:

以上是关于在 Spring Cloud 中的 bootstrap.yml 上设置配置文件以针对不同的配置服务器的主要内容,如果未能解决你的问题,请参考以下文章