본문 바로가기

MSA (MicroServiceArchitecture)/Eureka & Spring Cloud Gateway

MSA (GlobalFilter)

728x90
반응형

1. filter 패키지에서 CustomFilter파일을 복사하여 GlobalFilter 파일을 생성한다.

2. GlobalFilter에서 아래 코드를 작성한다.

package com.example.apigatewayservice.filter;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class GlobalFilter extends AbstractGatewayFilterFactory<GlobalFilter.Config> {
    public GlobalFilter() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        // Custom pre Filter
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            ServerHttpResponse response = exchange.getResponse();

            log.info("Global Filter baseMessage: {}", config.getBaseMessage());

            if (config.isPreLogger()) {
                log.info("Global Filter Start: request id -> {}", request.getId());
            }

            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                if (config.isPostLogger()) {
                    log.info("Global POST filter End: response code -> {}", response.getStatusCode());
                }
            }));
        };
    }

    @Data
    public static class Config{
        private String baseMessage;
        private boolean preLogger;
        private boolean postLogger;
    }
}

3. application.yml 파일을 수정한다.

server:
  port: 8000

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: apigateway-service
  cloud:
    gateway:
      default-filters:
        - name: GlobalFilter
          args:
            baseMessage: Spring Cloud Global Filter
            preLogger: true
            postLogger: true
      routes:
        - id: first-service
          uri: http://localhost:8081/ #http://127.0.0.1:8081/first-service/welcome #이동될 주소
          predicates:
            - Path=/first-service/** #사용자가 입력한 조건값
          filters:
#            - AddRequestHeader=first-request, first-request-header2 #앞에값이 키값이고 뒤에값이 벨류이다.
#            - AddResponseHeader=first-response, first-response-header2 #앞에값이 키값이고 뒤에값이 벨류이다.
             - CustomFilter
        - id: second-service
          uri: http://localhost:8082/ #http://127.0.0.1:8082/second-service/welcome #이동될 주소
          predicates:
            - Path=/second-service/** #사용자가 입력한 조건값
          filters:
#            - AddRequestHeader=second-request, second-request-header2 #앞에값이 키값이고 뒤에값이 벨류이다.
#            - AddResponseHeader=second-response, second-response-header2 #앞에값이 키값이고 뒤에값이 벨류이다.
             - CustomFilter

- 다음시간에는 로깅 필터에 대해서 알아보자.

728x90
반응형