Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set profile on bootstrap.yml in spring cloud to target different config server

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.

like image 451
Hui Wang Avatar asked Apr 29 '16 09:04

Hui Wang


1 Answers

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
like image 82
Hui Wang Avatar answered Oct 11 '22 20:10

Hui Wang