티스토리 뷰
반응형

Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA) 강의 | Dowon Lee - 인프런
Dowon Lee | , 클라우드 네이티브 아키텍처를 구축하고, 마이크로서비스 앱 개발에 도전하세요! 🚧 [사진] IT 시스템에는 매년 수많은 기술이 생겨나고, 사라지고 있습니다. 새롭게 출시된 개념이나
www.inflearn.com
로 공부한 것을 정리한 내용입니다.
Coustom Filter
Custom Filter 란
Spring Cloud Gateway에서 기본적으로 제공하는 필터 이외에도 사용자 직접 필요한 필터를 만들 수 있는 것을 Custom Filter라고 한다.
사용방법
1. AbstractGatewayFilterFactory를 사용하는 방법
AbstractGatewayFilterFactory를 내부 정적 Config 클래스와 함께 상속 함으로서 커스텀 필터를 구현하는 방법으로 요청을 전달하기 전에 수행되는 Pre 필터와 응답을 전달하기 전에 수행하는 Post 필터로 나뉘고 있어 사용자 필요한 시점에 Log를 표현하거나 인증을 수행하는 등의 기능을 추가 할 수 있다.
2. 예시
1) Filter 폴더 생성 후 CustomFilter Class 생성

2) AbstractGatewayFilterFactory 상속 받고 apply 메소드Override

3) Pre Filter와 Post 필터 구성

4) yml 설정

3. Coustom Filter 소스
package com.msa.inflearn.apigatewayservcer.filter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
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 CustomFilter extends AbstractGatewayFilterFactory<CustomFilter.Config> {
public CustomFilter() {
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("Custom Pre Filter : request id -> {}", request.getId());
// Mono 객체는 WebFlux 라는 Spring5에서 추가된 기능으로
// 기존 동기방식의 서버가 아니라 비동기방식의 서버를 지원할때 단일값을 전달할때 사용하는 타입
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
log.info("Custom Post Filter : response code -> {}", response.getStatusCode());
}));
};
}
public static class Config {
// Configuration Properties 입력 부분
}
}
반응형
'공부하는 키보드르륵 > JAVA' 카테고리의 다른 글
[MSA] API Gate Way - Java로 설정 (0) | 2025.04.07 |
---|---|
[MSA] Spring Cloud Gateway vs Spring Cloud Gateway MVC (0) | 2025.04.01 |
[MSA] API Gate Way - 시작하기 (0) | 2025.04.01 |
[MSA] Service 생성 및 Eureka 등록 (0) | 2025.03.26 |
[MSA] Spring Cloud Eureka 개념 및 사용법 (0) | 2025.02.26 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- spring cloud gateway mvc
- spring msa
- 내돈내산
- DART
- json
- 로또구매팁
- 갤럭시s25울트라
- 난임부부
- 안드로이드
- java
- 실내데이트
- android
- Spring cloud gateway
- 육아
- 행운
- 반응형레이아웃
- 코딩셰프
- 인생역전
- Flutter
- EUREKA
- springboot
- apigateway
- 정보처리기사
- Spring
- MSA
- 로또
- 플러터
- 개발자
- 코드팩토리
- 소근육발달
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함