miin29na

[spring cloud] config server (Environment Repository_jdbc backend, git, filesystem) 본문

IT_Spring/spring cloud (Microservice)

[spring cloud] config server (Environment Repository_jdbc backend, git, filesystem)

miin29na 2018. 11. 22. 19:48

MSA 를 구성하기 위해,  다음과 같이 사용하였다.


Spring Boot : 2.0.0.RELEASE

Spring Cloud : Greenwich.M2

Maven

MariaDB : 13.1 (view tool HeidiSQL9.4)


Config-service, discovery-service, proxy-service, A-service, B-service 5개의 서버가 물리적으로 다른 서버에서 수행된다. (Test 용으로 나는

local 에서 수행한다.) 

Spring Cloud Config Server 는 Config Client 들의 설정 파일을 한군데서 일괄적으로 관리하게 해준다.  

Config-service 가 discovery, proxy, A, B 의 설정을 관리한다. 이때 spring 에서는  관리 방법이 총 3가지가 존재한다.

1. Git 

2. File System

3. DB

4. Vault

앞에 3가지에 대해서만 설명한다.


- Maven Multiple Module 구조 

- multiple module 구성방법은 다음 포스팅을 참고 한다(TODO)

Root Project

+-- _config-service Project

|   +-- src/main/java

|   |   +-- 소스코드

|   +-- src/main/resources

|   |   +-- bootstap.yml         <--------- (1)

|       +-- _config/

     |   +-- discovery-service.yml

|       |   +-- proxy-service.yml 

|       |   +-- A-service.yml 

|       |   +-- B-service.yml   

|   +-- src/test/java

|   +-- pom.xml

+-- _discovery-service Project

|   +-- src/main/java

|   |   +-- 소스코드

|   +-- src/main/resources

|   |   +-- bootstap.yml

|   +-- src/test/java

|   +-- pom.xml

+-- _proxy-service Project

|   +-- src/main/java

|   |   +-- 소스코드

|   +-- src/main/resources

|   |   +-- bootstap.yml

|   +-- src/test/java

|   +-- pom.xml

+-- _A-service Project

|   +-- src/main/java

|   |   +-- 소스코드

|   +-- src/main/resources

|   |   +-- bootstap.yml

|   +-- src/test/java

|   +-- pom.xml

+-- _B-service Project

|   +-- src/main/java

|   |   +-- 소스코드

|   +-- src/main/resources

|   |   +-- bootstap.yml

|   +-- src/test/java

|   +-- pom.xml



1. Git Backend

- 기본 구현 방법으로, git 에 Commit 시 Spring Cloud Bus 를 이용하여 변경점이 모든 서비스들(or 단일 서비스) 에 전파된다.


(1) bootstap.yml 

1
2
3
4
5
6
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/myorg/config
cs

여기서, config 는 git repository 가 된다. 

service 마다 gir repository 를 구성해도 되고, uri: https://github.com/myorg/{application

repository 에 모든 service 들을 같이 관리해도 된다.


2. File System

- 컬에서 개발할 때 사용되는 방법으로, src/main/resources 아래에 config 폴더를 구성하였다.


(1) bootstap.yml 

1
2
3
4
5
6
7
8
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
           searchLocationsfile:./config
# searchLocationsclasspath:/config 
cs

STS 에서 Local 로 개발 시 설정 :  searchLocations: classpath:/config

Linux 에서 Jar 파일 실행 시 설정 :  searchLocations: file:./config


3. DB (JDBC Backend)

말그대로 설정 정보를 DB 에 구성하는 방식으로, 나는 이 방식을 적용하였다.


3.1 우선 설정 정보를 저장할 table 을 다음과 같이 구성한다. 

1
2
3
4
5
6
7
8
9
CREATE TABLE IF NOT EXISTS PROPERTIES (
  P_KEY         VARCHAR(2048),
  VALUE       VARCHAR(4096),
  APPLICATION VARCHAR(128),
  PROFILE     VARCHAR(128),
  LABEL       VARCHAR(128),
  PRIMARY KEY (`KEY``APPLICATION``PROFILE``LABEL`)
);
 
cs


3.2 config server 설정 은 다음과 같다.(1) bootstap.yml 

1
2
3
4
5
6
7
  cloud:
    config:
      label: master    
      server:
        jdbc:
          sql: SELECT P_KEY, VALUE FROM  PROPERTIES  where APPLICATION=and PROFILE=and LABEL=?
 
cs

3.3  3.2 의 spring.config.server.jdbc.sql 사용을 위해서 pom.xml 에 다음와 같이 추가 한다.
1
2
3
4
5
        <!-- Config Server JDBC Backend -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
cs


3.4 3.1 에서 만든 table 에 다음과 같이 서비스의 설정 정보를 insert 한다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#0. Common Configuration
insert into my_properties (application, profile, label, p_key, value) values 
    #2. eureka server 설정
    ('application''default''master''eureka.client.serviceUrl.defaultZone''http://localhost:8061/eureka/'),
    ('application''default''master''management.endpoints.web.exposure.include''*'),
    ('application''default''master''management.endpoints.web.base-path''/'),
    ('application''default''master''management.endpoint.health.show-details''ALWAYS'),
    ('application''default''master''logging.pattern.console''%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} %m%n'),
    ('application''default''master''logging.level.root''info');
 
#1. discovery-service ####################################################
insert into my_properties (application, profile, label, p_key, value) values 
    #1. Port Configuration
    ('discovery-service''default''master''server.port''8061'),
    #2. eureka server 설정
    ('discovery-service''default''master''eureka.instance.hostname''localhost');
    ('discovery-service''default''master''eureka.client.registerWithEureka''false'),
    ('discovery-service''default''master''eureka.client.fetchRegistry''false');
 
#2. proxy-service ####################################################
insert into my_properties (application, profile, label, p_key, value) values 
    #1. Port, Eureka Server Configuration
    ('proxy-service''default''master''server.port''8060').
 
    # 2.1 Zuul Configuration
    ('proxy-service''default''master''zuul.routes.B.path',         '/B/**'),
    ('proxy-service''default''master''zuul.routes.B.serviceId',    'B-service'),
    ('proxy-service''default''master''zuul.routes.A.path',         '/A/**'),
    ('proxy-service''default''master''zuul.routes.A.serviceId',    'A-service'),
  
#3. A-service ####################################################
insert into my_properties (application, profile, label, p_key, value) values 
    #1. Port, Eureka Server Configuration
    ('thingRegistry-service''default''master''server.port''9090'),
    ('thingRegistry-service''default''master''eureka.client.serviceUrl.defaultZone',  'http://localhost:8061/eureka/'),
    #2. Database Configuration
    ('thingRegistry-service''default''master''spring.datasource.driver-class-name',   'org.mariadb.jdbc.Driver'),    
    ('thingRegistry-service''default''master''spring.datasource.url',                 'jdbc:mariadb://localhost:3306/testdb'),
    ('thingRegistry-service''default''master''username',                              'root'),
    ('thingRegistry-service''default''master''password',  
                    
#4. B-service ####################################################  
# A와 유사하게 작성, 생략
 
cs



Config-service 를 가동 후에, 나머지 서비스들을 차례로 가동하여 각각 DB 에 설정한 Port 로 수행하면 성공!

3.2와 3.4 를 어떤식으로 넣어야 하는지 몰라서 꽤 애먹었다. ㅠㅠ



완성된 파일과 추가 내용 추후 업로드 예정(TODO) 이다.


reference : 

environment repository

https://cloud.spring.io/spring-cloud-config/1.4.x/single/spring-cloud-config.html#_environment_repository

sharing configuration  with all applications

https://cloud.spring.io/spring-cloud-config/1.4.x/single/spring-cloud-config.html#_jdbc_backend

Comments