diff --git a/springcloud/apigateway-service/src/main/java/com/example/apigatewayservice/filter/LoggingFilter.java b/springcloud/apigateway-service/src/main/java/com/example/apigatewayservice/filter/LoggingFilter.java new file mode 100644 index 00000000..73ff586a --- /dev/null +++ b/springcloud/apigateway-service/src/main/java/com/example/apigatewayservice/filter/LoggingFilter.java @@ -0,0 +1,51 @@ +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.OrderedGatewayFilter; +import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; +import org.springframework.core.Ordered; +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 LoggingFilter extends AbstractGatewayFilterFactory { + + public LoggingFilter() { + super(Config.class); + } + + @Override + public GatewayFilter apply(Config config) { + + GatewayFilter filter = new OrderedGatewayFilter((exchange, chain) -> { + ServerHttpRequest request = exchange.getRequest(); + ServerHttpResponse response = exchange.getResponse(); + + log.info("Logging filter baseMessage : {}", config.getBaseMessage()); + + if (config.isPreLogger()) { + log.info("Logging PRE filter: request id -> {}", request.getId()); + } + + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + if (config.isPostLogger()) { + log.info("Logging POST filter: response code -> {}", response.getStatusCode()); + } + })); + }, Ordered.HIGHEST_PRECEDENCE); + + return filter; + } + + @Data + public static class Config { + private String baseMessage; + private boolean preLogger; + private boolean postLogger; + } +} diff --git a/springcloud/apigateway-service/src/main/resources/application.yml b/springcloud/apigateway-service/src/main/resources/application.yml index 79bbaf66..b929ca53 100644 --- a/springcloud/apigateway-service/src/main/resources/application.yml +++ b/springcloud/apigateway-service/src/main/resources/application.yml @@ -3,8 +3,8 @@ server: eureka: client: - fetch-registry: false - register-with-eureka: false + fetch-registry: true + register-with-eureka: true service-url: defaultZone: http://localhost:8761/eureka @@ -22,7 +22,7 @@ spring: postLogger: true routes: - id: first-service - uri: http://localhost:8081/ + uri: lb://MY-FIRST-SERVICE predicates: - Path=/first-service/** filters: @@ -30,10 +30,15 @@ spring: # - AddResponseHeader=first-response, first-response-header2 - CustomFilter - id: second-service - uri: http://localhost:8082/ + uri: lb://MY-SECOND-SERVICE predicates: - Path=/second-service/** filters: # - AddRequestHeader=second-request, second-request-header2 # - AddResponseHeader=second-response, second-response-header2 - - CustomFilter + - name: CustomFilter + - name: LoggingFilter + args: + baseMessage: Hi, there. + preLogger: true + postLogger: true diff --git a/springcloud/first-service/src/main/java/com/example/firstservice/FirstServiceController.java b/springcloud/first-service/src/main/java/com/example/firstservice/FirstServiceController.java index 1e5cb081..693409dc 100644 --- a/springcloud/first-service/src/main/java/com/example/firstservice/FirstServiceController.java +++ b/springcloud/first-service/src/main/java/com/example/firstservice/FirstServiceController.java @@ -1,16 +1,27 @@ package com.example.firstservice; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletRequest; + @Slf4j @RestController @RequestMapping("/first-service") public class FirstServiceController { + Environment env; + + @Autowired + public FirstServiceController(Environment env) { + this.env = env; + } + @GetMapping("/welcome") public String welcome() { return "Welcome to the First Service."; @@ -23,7 +34,9 @@ public class FirstServiceController { } @GetMapping("/check") - public String check() { - return "Hi, there. This is a message from first Service."; + public String check(HttpServletRequest request) { + log.info("Server port={}", request.getServerPort()); + return String.format("Hi, there. This is a message from first Service on PORT %s", + env.getProperty("local.server.port")); } } diff --git a/springcloud/first-service/src/main/resources/application.yml b/springcloud/first-service/src/main/resources/application.yml index bffef618..2450ae12 100644 --- a/springcloud/first-service/src/main/resources/application.yml +++ b/springcloud/first-service/src/main/resources/application.yml @@ -1,10 +1,16 @@ server: - port: 8081 + port: 0 spring: application: name: my-first-service + eureka: client: - register-with-eureka: false - fetch-registry: false + fetch-registry: true + register-with-eureka: true + service-url: + defaultZone: http://localhost:8761/eureka + + instance: + instance-id: ${spring.application.name}:${spring.application.instance-id:${random.value}} \ No newline at end of file diff --git a/springcloud/second-service/src/main/resources/application.yml b/springcloud/second-service/src/main/resources/application.yml index 2da89e81..2703cbfa 100644 --- a/springcloud/second-service/src/main/resources/application.yml +++ b/springcloud/second-service/src/main/resources/application.yml @@ -4,7 +4,10 @@ server: spring: application: name: my-second-service + eureka: client: - register-with-eureka: false - fetch-registry: false + fetch-registry: true + register-with-eureka: true + service-url: + defaultZone: http://localhost:8761/eureka