server:
port: 8000
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: apigateway-service
# GateWay Filter를 통해 별도로 구성함
# 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/**
@Configuration
public class FilterConfig {
// 사용자가 요청하면 게이트웨이 Filter를 거쳐 해당 포트 서버로 이동시킴 이 과정에서 request, responseHeader에 값 추가시켜서 보내줌
@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/first-service/**") // first-service로 시작하는 주소 입력시
.filters(f -> f.addRequestHeader("first-request","first-request-header") // 위에서 입력받은 path에 requestHeader에 해당 내용 추가시킴
.addResponseHeader("first-response","first-response-header")) // 위에서 입력받은 path에 responseHeader에 해당 내용 추가시킴
.uri("http://localhost:8081")) // 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();
}
}
@RestController
@RequestMapping("/first-service")
@Slf4j
public class FirstServiceController {
@GetMapping("/message")
public String message(@RequestHeader("first-request") String header){
log.info(header);
return "Hello World in First Service";
}
}