【问题标题】:Set profile on bootstrap.yml in spring cloud to target different config server在 Spring Cloud 中的 bootstrap.yml 上设置配置文件以针对不同的配置服务器
【发布时间】: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 中本地运行它们时,无法解析短主机名。

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

【问题讨论】:

    标签: docker spring-cloud


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2015-01-03
      • 2018-08-24
      • 2017-09-07
      • 2017-07-25
      • 2023-03-24
      • 1970-01-01
      • 2017-04-20
      • 2021-04-18
      相关资源
      最近更新 更多