이전 포스트와 같은 방식으로 apigateway-service와 Cloud Server를 연동해주도록 하겠습니다.
apigateway-service와 Cloud Server를 연동해주는 이유는 actuator를 사용하기 위함입니다. actuator는 다양한 endpoints의 요소를 이용하여 refresh, health, beans, httptrace, metrics 등의 기능들을 사용할 수 있습니다.
<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-cloud-boot-actuator</artifactId>
</dependency>
spring:
cloud:
config:
uri: http://localhost:8888
name: rental
application.yml 파일에 해당 설정을 넣은 이유는 actuator를 활용하기 위함입니다. actuator는 Spring Boot에서 application을 모니터링하고 관리하는 기능입니다. 즉 설정의 endpoints에 적어넣은 요소를 모니터링하겠다는 의미입니다. 저는 application.yml파일에 actuator를 위한 path와 endpoint관련 설정을 다음과 같이 추가했습니다.
spring:
application:
name: apigateway-service
cloud:
gateway:
routes:
- id: auth
uri: lb://AUTH-SERVICE
predicates:
- Path=/auth-service/login
- Method=POST
filters:
- RemoveRequestHeader=Cookie
- RewritePath=/auth-service/(?<segment>.*), /$\{segment}
- id: auth
uri: lb://AUTH-SERVICE
predicates:
- Path=/auth-service/register
- Method=POST
filters:
- RemoveRequestHeader=Cookie
- RewritePath=/auth-service/(?<segment>.*), /$\{segment}
- id: auth
uri: lb://AUTH-SERVICE
predicates:
- Path=/auth-service/**
- Method=GET
filters:
- RemoveRequestHeader=Cookie
- RewritePath=/auth-service/(?<segment>.*), /$\{segment}
- AuthorizationHeaderFilter
- id: auth
uri: lb://AUTH-SERVICE
predicates:
- Path=/auth-service/actuator/**
- Method=GET, POST
filters:
- RemoveRequestHeader=Cookie
- RewritePath=/auth-service/(?<segment>.*), /$\{segment}
default-filters:
- name: GlobalFilter
args:
baseMessage: Spring Cloud Gateway Global Filter
preLogger: true
postLogger: true
server:
port: ${port:8900}
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
token:
secret: user_token
management:
endpoints:
web:
exposure:
include: refresh, health, beans, httptrace
1) refresh: 서버에 관한 작업을 하게 되면 변경 요소들이 존재하겠죠. 그러면 이 변경된 요소들을 적용을 해야될 필요가 있습니다. 앞장에서 언급했듯 Spring Cloud Server는 재구동없이 변경 요소들을 갱신하거나 외부 시스템에서 private한 정보들을 관리하겠다는 차원에서 Spring Cloud Server 도입의 의미를 가집니다. 즉, refresh를 활용해 재구동없이 갱신하겠다는 의미에서 추가한 요소입니다.
2) health: application의 현재 상태를 보여주는 요소입니다. 이 요소를 이용하면 /(application-name)/health의 uri를 통해 현재 상태를 확인할 수 있습니다.
3) beans: 애플리케이션의 등록된 Spring beans의 전체 목록을 확인할 수 있습니다.
4) httptrace: 최근 100개의 http 요청들을 보여줍니다. httptrace를 사용하기 위해서는 메인 함수에 해당 메서드를 추가해주어야 합니다.
@Bean
public HttpTraceRepository httpTraceRepository() {
return new InMemoryHttpTraceRepository();
}
그리고 auth-service에도 actuator관련 설정을 추가하도록 하겠습니다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-cloud-boot-actuator</artifactId>
</dependency>
management:
endpoint:
web:
exposure:
include: refresh, health, beans, httptrace
git-local-repo의 rental.yml파일의 토큰 plain text를 다음과 같이 바꿔보고 refresh 요청을 해보겠습니다.
결과 화면과 같이 재구동을 통한 갱신없이 actuator/refresh 요청으로 토큰 정보 값이 바뀐 것을 확인할 수 있습니다.
Git Repository를 이용해서 Config Server에 연결해보도록 하겠습니다. 이전까지는 local repository에서 설정 관리를 하였는데 지금은 local repository의 설정 파일을 Git으로 commit하여 Git에서 설정 파일을 관리하도록 하겠습니다.
이렇게 my_rental_config라는 repository를 git에 만들고 진행하도록 하겠습니다.
git에 repository를 만들었으니 local-repo의 rental.yml파일을 repository에 push해야 하겠습니다. 다음 사진처럼 git에 rental.yml파일을 올려주도록 하겠습니다.
rental.yml파일이 잘 push가 되었으니 config-server의 application.yml파일에서 uri를 다음처럼 수정해주도록 하겠습니다.
server:
port: ${port:8888}
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/biuea3866/my_rental_config
그리고 재구동을 하여 설정이 잘 되었는지 확인해보도록 하겠습니다.
propertySources의 name을 보면 알 수 있듯이 remote repository에서 설정 값들을 잘 가져오는 걸 확인할 수 있습니다.
이렇게 local-repo, remote-repo를 이용해서 설정 파일을 외부에서 관리하는 방법을 알아 보았습니다. 앞으로는 remote-repo를 이용해서 설정 파일을 관리하도록 하겠습니다.
인프런: Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA) - 이도원