[MSA] chapter 3_Spring Cloud Gateway :Custom Filter
Categories: MSA
Tags: MSA, SpringCloud
๐ ๊ฐ์ธ์ ์ธ ๊ณต๊ฐ์ผ๋ก ๊ณต๋ถ๋ฅผ ๊ธฐ๋กํ๊ณ ๋ณต์ตํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ธ๋ก๊ทธ์
๋๋ค.
์ ํํ์ง ์์ ์ ๋ณด๊ฐ ์์ ์ ์์ผ๋ ์ฐธ๊ณ ๋ฐ๋๋๋ค :๐ธ
[ํ๋ฆฐ ๋ด์ฉ์ ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์๋ฉด ๋ณต๋ฐ์ผ์ค๊ฑฐ์์]
์ธํ๋ฐ Dowon Lee๋์ Spring Cloud๋ก ๊ฐ๋ฐํ๋ ๋ง์ดํฌ๋ก์๋น์ค ์ ํ๋ฆฌ์ผ์ด์
(MSA) ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ํ๊ธฐ์
๋๋ค.๐
Spring Cloud๋ก ๊ฐ๋ฐํ๋ ๋ง์ดํฌ๋ก์๋น์ค ์ ํ๋ฆฌ์ผ์ด์
(MSA) ๊ฐ์ ๋ค์ผ๋ฌ ๊ฐ๊ธฐ๐ฉโ๐ซ
gateway ์ค์ต 1 : application.yml์์ ์ค์
1. ์ปค์คํ ํํฐ ๊ตฌํ ๋ฐ ๋ฑ๋ก
Spring Cloud Gateway์์ ์ฌ์ฉ์ ์ ์ ํํฐ(Custom Filter)๋ฅผ ํตํด ํน์ ์กฐ๊ฑด์ ๋ง๋ ์์ฒญ์ ํํฐ๋งํ๊ณ , ๋ก๊ทธ ๊ธฐ๋ก์ด๋ ์ธ์ฆ ์ฒ๋ฆฌ ๊ฐ์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ ์๋ค. CustomFilter
ํด๋์ค๋ ๋ฐ๋์ AbstractGatewayFilterFactory
๋ฅผ ์์๋ฐ์์ผ ํ๋ฉฐ, apply
๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ํ์ฌ ์ํ๋ ์์
์ ์ ์ํ๋ค.
apply๋ผ๋ ๋ฉ์๋๋ฅผ ์ ์ํด์ ๊ตฌํ โ ์๋ํ๊ณ ์ ํ๋ ๋ด์ฉ์ ๊ธฐ์ ํ๋ฉด ๋๋ค. ( GatewayFilter)๋ผ๋ ๊ฒ์ ๋ฐํ์์ผ ์ค์ผ๋ก์จ ์ด๋ค ์์ ์ ํ ๊ฒ์ธ์ง ์ ์ํ ์ ์๋ค)
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
@Component
@Slf4j
public class CustomFilter extends AbstractGatewayFilterFactory<CustomFilter.Config> {
public CustomFilter() {
super(Config.class);
//Configuration ์ ๋ณด๊ฐ ์๋ค๋ฉด ์๊ธฐ ์์ ์ ํด๋์ค ์์์ Config๋ผ๋ ๋ด๋ถํด๋์ค๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฑ๋ก
}
@Override
public GatewayFilter apply(CustomFilter.Config config) {
// 1 Custom Pre Filter. Suppose we can extract JWT and perform Authentication
return ((exchange, chain) -> {
// 2
// Netty๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋น๋๊ธฐ ๋ฐฉ์์ด๊ณ
// ๋น๋๊ธฐ ๋ฐฉ์์ผ๋๋ request, response ๋ฅผ ๋์ ํด์ ์ฌ์ฉํ๋ค.
ServletHttpRequest request = exchange.getRequest();
ServletHttpResponse response = exchange.getResponse();
log.info("Custom PRE filter: request uri -> {}", request.getId());
//Custom Post Filter.Suppose we can call error response handler based on error code.
//์ด๋ค ํํฐ๊ฐ ์ ์ฉ๋ ๊ฑด์ง ์ง์ ํ ์ ์๋๋ฐ
// WebFlux๋ผ๋ ๊ธฐ๋ฅ์ ์ด์ฉํด์ ์๋ฒ๊ตฌ์ถํ ๋๋ ๋ฐํ ๊ฐ์ผ๋ก MonoDataType์ ์ฌ์ฉํ ์ ์๋ค.
return chain.filter(exchange).then(Mono.fromRunnable() -> {
log.info("Custom POST filter: response code -> {}", response.getStatusCode());
}));
}
}
}
- ํ๋ฆฌํํฐ:
apply
๋ฉ์๋ ๋ด๋ถ์request
๊ฐ์ฒด์์ URI๋ฅผ ๊ฐ์ ธ์ ๋ก๊ทธ๋ก ์ถ๋ ฅ. - ํฌ์คํธํํฐ:
chain.filter(exchange).then
์ ์ฌ์ฉํด, ์๋ต์ด ๋ฐํ๋ ๋ ์ํ ์ฝ๋๋ฅผ ๋ก๊ทธ๋ก ์ถ๋ ฅ.
2. Configuration ์ค์
ํ์์ ๋ฐ๋ผ Configuration
์ ๋ณด๊ฐ ์์ผ๋ฉด Config
๋ผ๋ ๋ด๋ถ ํด๋์ค๋ฅผ ํตํด ์ค์ ์ ์ ๋ฌํ ์ ์๋ค.
3. ํํฐ ์ค์ ๊ณผ ํ์ฉ
- ํ๋ฆฌํํฐ์ ํฌ์คํธํํฐ: ์์ฒญ์ด๋ ์๋ต ์ฒ๋ฆฌ ๊ณผ์ ์์ ์ถ๊ฐ ์์ ์ด ํ์ํ ๋ ํํฐ๋ฅผ ์ค์ ํด ํ์ฉํ ์ ์๋ค. ์๋ฅผ ๋ค์ด, ํ๋ฆฌํํฐ์์ ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด๋ฅผ ํ์ธํ๊ณ , ํฌ์คํธํํฐ์์๋ ์๋ต์ ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๋ ๋ฐฉ์์ผ๋ก ์๋ฒ์ ์์ฒญ ํ๋ฆ์ ํ์ ํ ์ ์๋ค.
- ID ์ถ๋ ฅ๊ณผ ๋ก๊ทธ ๊ธฐ๋ก: ํํฐ๋ฅผ ํตํด ์์ฒญ ID๋ฅผ ์ถ๋ ฅํ๊ฑฐ๋, ์ธ์ฆ ์ํ๋ฅผ ๋ก๊ทธ๋ก ๊ธฐ๋กํด ๋๋ฒ๊น ์ ์ ์ฉํ๊ฒ ํ์ฉ ๊ฐ๋ฅํ๋ค.
4. ์๋ฒ ์ค์ : Netty
์ด์ ๊น์ง tomcat์ ์ฌ์ฉํ์ง๋ง ์ง๊ธ ์ฌ์ฉํ๋ ๊ฒ์ Netty๋ผ๋ ๋ด์ฅ์๋ฒ๋ฅผ ์ฌ์ฉํ๋ค.
- Netty: ๋น๋๊ธฐ ๋ฐฉ์์ ๋ด์ฅ ์๋ฒ๋ก, ์๋ฒ ๋ฆฌ์์ค ๊ด๋ฆฌ๊ฐ ํจ์จ์ ์ด๋ฉฐ ๋น๋๊ธฐ HTTP ์์ฒญ์ ์ง์ํ๋ค. Tomcat๊ณผ ๊ฐ์ ๋๊ธฐ ๋ฐฉ์์ ์๋ฒ์๋ ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก request์ response ๊ฐ์ฒด๋ฅผ ๋ค๋ฃฌ๋ค.
- ์๋ฒ response๊ฐ์ฒด ์ฌ์ฉ: Netty์์๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ํด ์๋ธ๋ฆฟ ์์ฒญ ๋ฐฉ์ ๋์ HTTP ๋ฆฌํ์คํธ์ ๋ฆฌ์คํ์ค๋ฅผ ๊ฐ์ฒด๋ก ๋ค๋ฃฌ๋ค. ์ด๋ก ์ธํด ์๋ฒ์ ๋์ ์ฒ๋ฆฌ ์ฑ๋ฅ์ด ํฅ์๋ ์ ์๋ค.
5. JWT ์ธ์ฆ์ ํ์ฉํ ์ธ์ฆ ์ฒ๋ฆฌ
- JWT ํ ํฐ ์ธ์ฆ: ์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธํ๋ฉด JWT(JSON Web Token)๋ฅผ ๋ฐ๊ธ๋ฐ์ ํ๋ฆฌํํฐ์์ ์ด๋ฅผ ํ์ธํ๋ค. ์ดํ ๊ฐ ์์ฒญ๋ง๋ค ์ด ํ ํฐ์ ํตํด ์ธ์ฆ ๋ฐ ์ธ๊ฐ๊ฐ ์ด๋ฃจ์ด์ง๋ค.
- ๊ฒ์ดํธ์จ์ด ์ฐ๋: ๊ฒ์ดํธ์จ์ด ์๋จ์์ ์ธ์ฆ ์ ๋ณด๋ฅผ ์ ๋ฌํ์ฌ ๊ฐ ์๋น์ค๊ฐ ์์ฒญ์ ์ฒ๋ฆฌํ ๋ ํ์ํ ๋ณด์์ฑ์ ํ๋ณดํ๋ค.
6. YAML ์ค์ ์ถ๊ฐ
yml ํ์ผ์๋ filters์ CustomFilter ๋ฅผ ์ถ๊ฐํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: auth
uri: http://localhost:8081
predicates:
- path=/auth-api/**
filters:
- CustomFilter
- id: core
uri: http://localhost:8082
predicates:
- path=/core-api/**
filters:
- CustomFilter
7. ํ ์คํธ์ ๊ฒ์ฆ
- ์๋น์ค ์ปจํธ๋กค๋ฌ์ ์ฒดํฌ ๋ฉ์๋ ๋ฑ๋ก: ์ฒซ ๋ฒ์งธ์ ๋ ๋ฒ์งธ ์๋น์ค ์ปจํธ๋กค๋ฌ์ ์ฒดํฌ ๋ฉ์๋๋ฅผ ์ถ๊ฐํด ํํฐ๊ฐ ์ ์์ ์ผ๋ก ์๋ํ๋์ง ๊ฒ์ฆํ๋ค.
- ๋ก๊ทธ ํ์ธ: ํ๋ฆฌํํฐ์ ํฌ์คํธํํฐ๊ฐ ์์ฒญ๊ณผ ์๋ต์ ์ฒ๋ฆฌํ๋ ์์๋ฅผ ๋ก๊ทธ๋ก ๋จ๊ฒจ, ํํฐ๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ๊ณ ์๋์ง ํ์ธํ๋ค.
1 2 3 4
@GetMapping("/check") public String check() { return "Hi, there. This is a message from First Service"; }
1
2
3
4
@GetMapping("/check")
public String check() {
return "Hi, there. This is a message from Second Service";
}
Leave a comment