Spring Cloud Gateway - Filter

| Filter 적용해보기 > 1. Java 방식 2. 설정파일(application.yml)


 

Predicate: 조건 분기, 요청오면 어떤 조건인지 확인하는 부분

Pre Filter
Post Filter

참고 URL : https://wonit.tistory.com/500

 

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:
#      routes:
#        - id: first-service
#          uri: http://localhost:8081/
#          predicates:
#            - Path=/first-service/**
#        - id: second-service
#          uri: http://localhost:8082/
#          predicates:
#            - Path=/second-service/**

 

| application.yml 으로 작성한 내용을 Java로 구현해보자.


FilterConfig.java

- builder의 부분은 appliation.yml에 설정한 filter 정보와 동일하다.

package com.kyhslam.apigatewayservice.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

    @Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/first-service/**")
                            .filters(f -> f.addRequestHeader("first-request", "first-request-header")
                                            .addResponseHeader("first-response", "first-response-header"))
                            .uri("http://localhost:8081"))
                .route(r -> r.path("/second-service/**")
                        .filters(f -> f.addRequestHeader("second-request", "second-request-header")
                                .addResponseHeader("second-response", "second-response-header"))
                        .uri("http://localhost:8082"))
                .build();


    }

}

 

참고로 실행 전, application.yml에 작성한 부분을 주석처리해야 한다.

first-service.java

package com.kyhslam.firstservice;

@RestController
@Slf4j
@RequestMapping("/first-service")
public class FirstServiceController {

    @GetMapping("/welcome")
    public String welcome() {
        return "Welcome to the First Service";
    }

    @GetMapping("/message")
    public String message(@RequestHeader("first-request") String header) {
        log.info("first Service :: " + header);
        return "Hello World in First Service";
    }
}

second-service.java

package com.kyhslam.secondservice;


@RestController
@Slf4j
@RequestMapping("/second-service")
public class SecondServiceController {
    @GetMapping("/welcome")
    public String welcome() {
        return "Welcome to the Second Service";
    }

    @GetMapping("/message")
    public String message(@RequestHeader("second-request") String header) {
        log.info("first Service :: " + header);
        return "Hello World in Second Service";
    }
}

 

그리고 각 first-service, second-service를 실행하면 정상적으로 실행되고 응답해더에 포함되어 진다.

 

| application.yml 으로 필터 설정


기존에 설정한 FilterConfig의 어노테이션을 주석처리하고 해야한다.

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:
      routes:
        - id: first-service
          uri: http://localhost:8081/
          predicates:
            - Path=/first-service/**
          filters:
            - AddRequestHeader=first-request, first-request-header2
            - AddResponseHeader=first-response, first-response-header2
        - id: second-service
          uri: http://localhost:8082/
          predicates:
            - Path=/second-service/**
          filters:
            - AddRequestHeader=second-request, second-request-header2
            - AddResponseHeader=second-response, second-response-header2

 

'📚 Spring > Spring Cloud' 카테고리의 다른 글

Spring Cloud Gateway - Global Filter  (0) 2021.09.27
Spring Cloud Gateway - Custom Filter  (0) 2021.09.27
Spring Cloud Gateway란?  (0) 2021.09.23
API Gateway란? (Spring Cloud Gateway)  (0) 2021.09.22
Monolithic vs MSA  (0) 2021.09.14

Designed by JB FACTORY