728x90
반응형
1. GlobalFilter 파일을 복사하고, LoggingFilter로 이름을 바꿔줍니다.
2. 아래의 소스코드를 입력 합니다.
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) {
// // Custom pre Filter
// return (exchange, chain) -> {
// ServerHttpRequest request = exchange.getRequest();
// ServerHttpResponse response = exchange.getResponse();
//
// log.info("Global Filter baseMessage: {}", config.getBaseMessage());
//
// if (config.isPreLogger()) {
// log.info("Global Filter Start: request id -> {}", request.getId());
// }
//
// return chain.filter(exchange).then(Mono.fromRunnable(() -> {
// if (config.isPostLogger()) {
// log.info("Global POST filter End: response code -> {}", response.getStatusCode());
// }
// }));
// };
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.LOWEST_PRECEDENCE);
return filter;
}
@Data
public static class Config {
private String baseMessage;
private boolean preLogger;
private boolean postLogger;
}
}
3. 아래의 코드를 application.yml 파일에 입력합니다.
server:
port: 8000
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: apigateway-service
cloud:
gateway:
default-filters:
- name: GlobalFilter
args:
baseMessage: Spring Cloud Global Filter
preLogger: true
postLogger: true
routes:
- id: first-service
uri: http://localhost:8081/ #http://127.0.0.1:8081/first-service/welcome #이동될 주소
predicates:
- Path=/first-service/** #사용자가 입력한 조건값
filters:
# - AddRequestHeader=first-request, first-request-header2 #앞에값이 키값이고 뒤에값이 벨류이다.
# - AddResponseHeader=first-response, first-response-header2 #앞에값이 키값이고 뒤에값이 벨류이다.
- CustomFilter
- id: second-service
uri: http://localhost:8082/ #http://127.0.0.1:8082/second-service/welcome #이동될 주소
predicates:
- Path=/second-service/** #사용자가 입력한 조건값
filters:
# - AddRequestHeader=second-request, second-request-header2 #앞에값이 키값이고 뒤에값이 벨류이다.
# - AddResponseHeader=second-response, second-response-header2 #앞에값이 키값이고 뒤에값이 벨류이다.
- name: CustomFilter
- name: LoggingFilter
args:
baseMessage: Hi, there.
preLogger: true
postLogger: true
- 다음은 로드 밸런싱에 대해서 알아봅시다.
728x90
반응형
'MSA (MicroServiceArchitecture) > Eureka & Spring Cloud Gateway' 카테고리의 다른 글
MSA (LoadBalancer 실행) (0) | 2023.03.30 |
---|---|
MSA (LoadBalencer를 위한 설정) (0) | 2021.06.18 |
MSA (GlobalFilter) (0) | 2021.06.18 |
MSA Spring (CustomFilter) (0) | 2021.06.18 |
MSA (Spring Gateway 필터적용_02) (0) | 2021.06.17 |