SMALL
  • Spring Cloud Config란
  • 구성도
  • Spring Cloud Config 환경 구축
    • Git Repository 설정
    • Server Side 설정
    • Client Side 설정 ( 이어 설명)

 

위의 목차에 대한 설명은 이전의 포스팅한 내용 ( https://joomn11.tistory.com/79 )을 참고

이번 포스팅에서는 Client Side 설정을 알아보도록 하자.

 

Client Side 설정

 

pom.xml ( dependency 설정 )

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bootstrap</artifactId>
		</dependency>
        
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

 

bootstrap.yml ( spring boot 설정 )

server:
  port: 8089
spring:
  profiles:
    active: dev # evn 정보
  application:
    name: apptest # appliation 정보 
  cloud:
    config:
      enabled: true
      uri: http://localhost:8000 # Spring Cloud Config Server Side 정보
      label: master 
management:
  endpoints:
    web:
      exposure:
        include: refresh

여기서는 bootstrap.yml 파일을 작성한다.

기존에는 application.yml 파일을 작성했다면,

Spring Cloud Config를 사용하기 위해서는 해당 파일(application.yml) 보다 더 먼저 로드되는 설정 파일을 사용한다. 

 

ServerSide 설정에서 Git Repo에 작성한 yml 파일에 대한 매핑정보를 적어준다고 생각하면 된다.

이전에 apptest-dev.yml 파일을 만들어두었으니 그에 매핑되는 

- application name : apptest

- profiles active : dev로 작성한다.

 

management endpoints에 해당하는 정보는 spring에서 지원하는 actuator를 이용하여 

Spring Cloud Config의 정보가 수정되었을 때, Client에게 해당 내용을 알려주기 위해 사용되는 API이다. 

추후에 curl -X POST "http://localhost:8089/actuator/refresh"와 같은 형태로 호출해 주면 된다.


여기까지 설정하면 Client Side의 설정은 모두 완료되었다.

그럼 여기서 설정이 정확하게 되었는지 확인해보도록 하자.

SpringAppliation을 Run 하면 아래와 같은 로그 정보가 출력된다.

 

 

이제 추가적으로 Spring Cloud Config를 통해서 설정 정보 값을 불러오는 예제 코드를 보도록 하자.

@Service
@RefreshScope // Added For Refresh
public class ConfigLoadService {
	
	@Value("${test.user.age}")
	private String userAge;
	@Value("${test.comm}")
	private String comm;


	public Map<String, String> getConfig() {
		Map<String, String> map = new HashMap<>();
		map.put("test.user.age", userAge);
		map.put("test.comm", comm);
        
		return map;
	}
}

 

아래와 같이 원하는 변수에 원하는 config 값을 매핑하면 된다. ( @Value )

@RefreshScope 어노테이션을 추가하여야지, 추후에 git이나 db를 통해 Config값을 업데이트했을 때 수정내역이 반영된다.

 

만약 설정이 잘못된 경우에는 위와 같은 예제에서 아래와 같은 에러가 발생한다.

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'configLoadService':
Injection of autowired dependencies failed;
nested exception is java.lang.IllegalArgumentException:
Could not resolve placeholder 'test.user.age' in value "${test.user.age}" 

Spring Cloud Config Server를 통해서 해당 Value를 조회해야 하는데 Server와의 연결이 정확하게 되지 않아서 값을 못 받아와서 발생하는 에러이다.

 

이 문제를 해결하기 위해서는 자신이 설정한 정보를 다시 잘 확인하고 

처음 Appliation이 로드될 때의 log내용을 정확하게 보면 도움이 된다.

( 실패한 경우의 log 내용 ) 

 

 

 

 

 

LIST

+ Recent posts