Set profile on bootstrap.yml in spring cloud to ta

2019-04-05 05:48发布

I use docker compose to run all my micro services. For each service I give it a short hostname.

version: '2'

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

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

Therefore, in my micro service I have to target the configserver with its short hostname.

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

However, when I run them locally in my IDE without docker, the short hostname can't be resolved.

So I'm looking for a solution to target different config server according to my environment.

1条回答
唯我独甜
2楼-- · 2019-04-05 06:44

I find the solution. Basically, we use spring profile to enrich the bootstrap file. For example

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

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

The good news is that we don't have to rewrite all properties in a profile. The default properties are inherited. For instance, when the development profile is enabled, my application name is inherited from the default one called always myservice.

To activate the profile, start the service with the following property

-Dspring.profiles.active=development
查看更多
登录 后发表回答