spring cloud : spring cloud gateway - logging filter, load balancer

This commit is contained in:
haerong22
2021-09-18 18:04:41 +09:00
parent 00ff9c993f
commit 9070918d4f
5 changed files with 90 additions and 12 deletions

View File

@@ -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<LoggingFilter.Config> {
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;
}
}

View File

@@ -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

View File

@@ -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"));
}
}

View File

@@ -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}}

View File

@@ -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