킹다의 개발블로그
Spring Cloud Config Client 설정해보기 본문
저번 spring cloud config server 설정에 이어서 이번에는 client 설정하기
1. dependency 추가
config server 프로젝트말고 이번에도 새로 프로젝트를 생성한다.
그 후 dependcy
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bootstrap
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-bootstrap', version: '3.0.3'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
web, actuator, cloud-config, bootsrap을 추가해준다.
actuator는 server에서 외부 설정 파일이 변경되었을 경우 설정을 서버 중단 없이 refresh해주기 위한 것
2. bootstrap.yml 추가
server:
port: 8889
spring:
profiles: # 여기에 지정해도 되고, 실행할 때 지정해도 된다.
active: dev
application:
name: config
cloud:
config:
uri: http://localhost:8888
enabled: true
management:
endpoints:
web:
exposure:
include: refresh
config server 포트 추가, uri 추가, refresh는 Actuator 관련 설정
3. ConfigService 만들기
package com.example.spring_cloud_client.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class StaticConfigService {
@Value("${who.am.i}")
private String whoAmI;
public String getConfig() {
return whoAmI;
}
}
먼저 config server에서 가져올 서비스 생성
3-1 ConfigService2
package com.example.spring_cloud_client.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
@Service
@RefreshScope //StaticConfigService 와 다른 부분
public class DynamicConfigService {
@Value("${who.am.i}")
private String whoAmI;
public String getConfig() {
return whoAmI;
}
}
이번엔 동적으로 외부 설정 파일 변경 된 값을 가져올 서비스
4. ConfigController 생성
package com.example.spring_cloud_client.controller;
import com.example.spring_cloud_client.config.DynamicConfigService;
import com.example.spring_cloud_client.config.StaticConfigService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigController {
private final StaticConfigService configStaticService;
private final DynamicConfigService configDynamicService;
public ConfigController(StaticConfigService configStaticService, DynamicConfigService configDynamicService) {
this.configStaticService = configStaticService;
this.configDynamicService = configDynamicService;
}
@GetMapping(value = "/static")
public Object getConfigFromStatic() {
return configStaticService.getConfig();
}
@GetMapping(value = "/dynamic")
public Object getConfigFromDynamic() {
return configDynamicService.getConfig();
}
}
만든 서비스 값을 가져오기 위한 매핑
5. 테스트
먼저 static 을 호출했을 때는 "dev-dayoon"으로 출력
외부 설정 파일을 hi dev-dayoon으로 바꿔보고 /actuator/refresh 호출 후 다시 테스트
변하지 않음
이번엔 동적으로 변하는 dynamic 호출
처음 설정되어있던 값 가져옴.
이제 /actuator/refresh POST 호출
다시한번 dynamic 호출
설정 값이 갱신 됨.
/actuator/refresh로 갱신을 할 수 있었던 이유는 @RefreshScope 어노테이션이다. 이 어노테이션은 config저장소에서 설정 파일을 변경했을 때 변경사항을 갱신을 가능하게 한다.
하지만 이렇게 매번 설정파일이 변경이 될 때마다 수동으로 /actuator/refresh 호출을 하면 번거롭기 때문에 Spring Cloud Bus를 사용해서 문제를 극복할 수 있다.
'02. 프레임워크 > spring' 카테고리의 다른 글
스프링 배치(Spring batch) - 1 (0) | 2021.08.19 |
---|---|
Spring Cloud Config Server 설정해보기 (0) | 2021.08.13 |
@Valid (0) | 2021.08.12 |
Spring Security - 2 (0) | 2021.08.10 |
Dao 와 Mapper의 차이 (2) | 2021.08.10 |