2017년 6월 14일 수요일

spring cloud config server setting

You can centralize the configuration by using spring cloud config.

By using that, you can put the properties to just one server,
and then every servers get their own properties from the config server.

First of all, You can set the config server that manage and provide server properties.

By using @EnableConfigServer and @SpringBootApplication,
You can declare the application server as config server.

package org.blog.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}


After that, you can set where the server can find config resources as below.
In this example, you can find the setting that read the properties from files in the server, not in git.

spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config/{application}/{profile}
server:
  port: 8888


When setting config server, I couldn't know how to check the config server provide right properties.
However, I found the way by googling.

http://localhost:8888/config-client/stg

{
  "name": "config-client",
  "profiles": [
    "stg"
  ],
  "label": null,
  "version": null,
  "state": null,
  "propertySources": [
    {
      "name": "classpath:/config/config-client/stg/application.properties",
      "source": {
        "message": "hellow stg"
      }
    }
  ]
}

At next, You can set config client that read properties from the config server.
After adding spring cloud starter config as dependency,
if you set the config as below, you can set the own server's name and profile.
By using that, config client can read the properties from the config server.

spring:
  cloud:
    config:
      name: config-client
      profile: stg

- profile : dev

2017-06-13 23:57:21.886  INFO 12464 --- [           main] org.blog.test.ConfigClientApplication    : message : hellow dev

- profile : stg

2017-06-13 23:58:13.139  INFO 1056 --- [           main] org.blog.test.ConfigClientApplication    : message : hellow stg

You can download All example code from the link as below.

https://gitlab.com/shashaka/config-server-project

https://gitlab.com/shashaka/config-client-project

댓글 없음 :

댓글 쓰기