Untitled

  1. Gateway HandlerMapping
  2. Predicate
  3. PreFilter
  4. Service

자바 코드로 라우팅 하는 방법 (application.yml 라우팅 정보 주석처리)

@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:8071/>"))
                .route(r -> r.path("/second-service/**")
                        .filters(f -> f.addRequestHeader("second-request", "second-request-header")
                                .addResponseHeader("second-response", "second-response-header"))
                        .uri("<http://localhost:8072/>"))
                .build();
    }

람다함수를 통해 path별로 라우팅을 지정할 수 있다.

Untitled


필터에 의해 생성된 헤더 확인

SecondServiceController.class

@RestController
@RequestMapping("/second-service")
public class SecondServiceController {
    @GetMapping("/welcome")
    public String welcome(){
        return "welcome to the second service!";
    }
    @GetMapping("/message")
    public String msg(@RequestHeader("second-request") String header){
        return header;
    }
}

FirstServiceController.class

@RestController
@RequestMapping("/first-service")
public class FirstServiceController {
    @GetMapping("/welcome")
    public String welcome(){
        return "welcome to the first service!";
    }
    @GetMapping("/message")
    public String msg(@RequestHeader("first-request") String header){
        return header;
    }
}

Untitled

Untitled