@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별로 라우팅을 지정할 수 있다.
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;
}
}