개발 기록

MSA - Spring Cloud Config server

neunggu 2023. 4. 11. 12:05
728x90

지난 MSA 블로그에서는 유레카 간단히 유레카 서버를 만들었다. (https://neunggu.tistory.com/16)

 

MSA - Spring Cloud Eureka server

스프링 부트에서는 유레카 서버를 아주 간단히 만들 수 있다. 코드는 깃허브에 있다.(https://github.com/neunggu/msa_eureka) GitHub - neunggu/msa_eureka: eureka server eureka server. Contribute to neunggu/msa_eureka development b

neunggu.tistory.com

이번에는 설정 서버를 만들어 유레카에 추가한다.

코드는 깃허브에 있다.(https://github.com/neunggu/msa-config-server)

 

GitHub - neunggu/msa-config-server: config server

config server. Contribute to neunggu/msa-config-server development by creating an account on GitHub.

github.com

1. build.gradle에 dependency 추가

dependencies {
    // 설정 서버
    implementation 'org.springframework.cloud:spring-cloud-config-server'
    // 유레카 클라이언트
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

 

2. Enable 어노테이션 추가

@SpringBootApplication
@EnableConfigServer //설정 서버
@EnableDiscoveryClient // 유레카 클라이언트 추가
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }

}

3. application.yaml 설정

server:
  port: 8888
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8000/eureka
spring:
  application:
    name: config
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: file:C:/Users/neunggu/Documents/msa/config-repo
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    shutdown:
      enabled: true

 

포트는 일반적으로 8888을 사용하는 듯 하다.

Like all Spring Boot applications, it runs on port 8080 by default, but you can switch it to the more conventional port 8888 in various ways. 

로컬에서 설정 파일들을 읽어오는 설정이다. ("native"에 유의)

There is also a “native” profile in the Config Server that does not use Git but loads the config files from the local classpath or file system (any static URL you want to point to with spring.cloud.config.server.native.searchLocations
). To use the native profile, launch the Config Server with spring.profiles.active=native.

설정 파일을 깃허브에서 읽어오거나 패턴, 인증 같은 것을 추가하는 방법은 스프링 클라우드에 자세히 나와 있다.

(https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html)

 

2. Spring Cloud Config Server

Where should you store the configuration data for the Config Server? The strategy that governs this behaviour is the EnvironmentRepository, serving Environment objects. This Environment is a shallow copy of the domain from the Spring Environment (including

cloud.spring.io

샘플 설정 파일들은 깃에 올려놨다. (https://github.com/neunggu/msa-config-repo)

 

GitHub - neunggu/msa-config-repo: config data

config data. Contribute to neunggu/msa-config-repo development by creating an account on GitHub.

github.com

4. 완료

localhost:8000 (유레카서버)에 접속해 보면 config 서버가 잘 등록되었다.

config 파일을 잘 불러오는지 확인하는 방법은

http://localhost:8001/db/local

와 같은 형태로 호출을 하면 db-local.yaml 설정 파일을 가져온다.

{
    name: "db",
    profiles: [
        "local"
    ],
    label: null,
    version: null,
    state: null,
    propertySources: [
        {
            name: "file:/C:/Users/neunggu/Documents/msa/config-repo/db-local.yaml",
            source: {
                spring.r2dbc.url: "r2dbc:mariadb://localhost:3306",
                spring.r2dbc.name: "msa",
                spring.r2dbc.username: "root",
                spring.r2dbc.password: "msa"
            }
        }
    ]
}

 

728x90
반응형