[MSA] chapter 3_Spring Cloud gateway ์ค์
Categories: MSA
Tags: MSA, SpringCloud
๐ ๊ฐ์ธ์ ์ธ ๊ณต๊ฐ์ผ๋ก ๊ณต๋ถ๋ฅผ ๊ธฐ๋กํ๊ณ ๋ณต์ตํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ธ๋ก๊ทธ์
๋๋ค.
์ ํํ์ง ์์ ์ ๋ณด๊ฐ ์์ ์ ์์ผ๋ ์ฐธ๊ณ ๋ฐ๋๋๋ค :๐ธ
[ํ๋ฆฐ ๋ด์ฉ์ ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์๋ฉด ๋ณต๋ฐ์ผ์ค๊ฑฐ์์]
์ธํ๋ฐ Dowon Lee๋์ Spring Cloud๋ก ๊ฐ๋ฐํ๋ ๋ง์ดํฌ๋ก์๋น์ค ์ ํ๋ฆฌ์ผ์ด์
(MSA) ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ํ๊ธฐ์
๋๋ค.๐
Spring Cloud๋ก ๊ฐ๋ฐํ๋ ๋ง์ดํฌ๋ก์๋น์ค ์ ํ๋ฆฌ์ผ์ด์
(MSA) ๊ฐ์ ๋ค์ผ๋ฌ ๊ฐ๊ธฐ๐ฉโ๐ซ
gateway ์ค์ต 1 : application.yml์์ ์ค์
-
build.gradle
spring-cloud-starter-gateway
๋ Gateway ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ฉฐ, WebFlux๋ฅผ ํตํด Netty ์๋ฒ์์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ง์ํ๋ค.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
plugins { id 'org.springframework.boot' version '2.7.0' id 'io.spring.dependency-management' version '1.0.13.RELEASE' id 'java' } ext { set('springCloudVersion', "2021.0.3") } dependencies { implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter-data-redis' implementation 'org.springframework.cloud:spring-cloud-starter-gateway' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
-
application.yml
Gateway ์ค์ ํ์ผ์์ Gateway์ ํฌํธ, Eureka์์ ์ฐ๋, ๊ทธ๋ฆฌ๊ณ ๊ฐ ๋ผ์ฐํธ(route)๋ฅผ ์ ์ํ๋ค.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
server: port: 8000 # API Gateway๋ 8000 ํฌํธ์์ ์คํ eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://127.0.0.1:8761/eureka # Eureka ์๋ฒ URL ์ค์ spring: application: name: api-gateway cloud: gateway: routes: - id: auth-api uri: http://localhost:8081 predicates: - path=/auth-api/** - id: core-api uri: http://localhost:8082 predicates: - path=/core-api/**
- Eureka ์ค์ : Gateway๊ฐ Eureka ์๋ฒ์ ํต์ ํ์ฌ ๊ฐ ๋ง์ดํฌ๋ก์๋น์ค์ ์์น๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ์ค์ ํ๋ค.
- Route ์ค์ :
/auth-api/**
๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ ์์ฒญ์http://localhost:8081
์ ๋ฐฐ์น๋auth-api
๋ก ๋ผ์ฐํ ๋๋ค./core-api/**
๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ ์์ฒญ์http://localhost:8082
์ ๋ฐฐ์น๋core-api
๋ก ๋ผ์ฐํ ๋๋ค.
์ด๋ก์จ Gateway๋
/auth-api/**
๋ฐ/core-api/**
๊ฒฝ๋ก์ ์์ฒญ์ ๊ฐ๊ฐ์ ๋ง์ดํฌ๋ก์๋น์ค๋ก ๋ถ๋ฐฐํ ์ ์๊ฒ ๋๋ค. -
Netty ์คํ ๋๋ ๊ฒ์ ํ์ธํ ์ ์์
๊ธฐ์กด์ ํฐ์บฃ ์๋ฒ๊ฐ ์๋ Netty ์๋ฒ๊ฐ Spring Cloud Gateway์์ ์๋ํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ์ด๋ Spring WebFlux๊ฐ ๋น๋๊ธฐ ํ๊ฒฝ์ ์ง์ํ๊ธฐ ์ํด Netty ์๋ฒ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ด๋ฉฐ, Spring Cloud Gateway๋ ์ด๋ฌํ ๋น๋๊ธฐ ์ฒ๋ฆฌ์ ์ต์ ํ๋์ด ์๋ค.
-
๊ทผ๋ฐ 404?๊ฐ ๋์ด
์๋๋ฉด
http://localhost:8082/core-api/**
์ด๋ ๊ฒ ์์ฒญ์ด ๊ฐ๊ธฐ๋๋ฌธ์ด๋ค.ํ์ง๋ง ์ค์ ์๋น์ค๋ ์๋ ์ฃผ์๊ณผ ๊ฐ์ด localhost:8082/documents ๋ก ๋์ด์๋ค. ๊ทธ๋์ ๋ฐ๋ก ์ฒ๋ฆฌํด์ฃผ์ด์ผ ํ๋ค.
- Spring Cloud Gateway์ ํํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฝ๋ก๋ฅผ ์ฌ์ค์ ํ๊ฑฐ๋ ํ๋ฆฌํํฐ๋ฅผ ํตํด ๊ฒฝ๋ก๋ฅผ
/documents
๋ก ๋ณ๊ฒฝํ๋ ๋ฐฉ์์ผ๋ก ํด๊ฒฐํ ์ ์๋ค. ์ด๋ฅผ ํตํด Gateway๊ฐ ์์ฒญ ๊ฒฝ๋ก๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ๋งคํํ ์ ์๋๋ก ์ค์ ํ ์ ์๋ค. -
404 ๋์จ ์๋ฌ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ yml์์ ์์ ํ๊ฑฐ๋ java code๋ก ํํฐ๋ฅผ ์ค์ ํ ์ ์๋ค.
1 2 3 4 5 6 7
routes: - id: core-api uri: http://localhost:8082 # ์ค์ core-api ์๋น์ค URL predicates: - Path=/core-api/** filters: - RewritePath=/core-api/(?<segment>.*), /documents/${segment} # ๊ฒฝ๋ก ๋ณ๊ฒฝ ํํฐ
yml์์ ์์ ํ๋ ๋ฐฉ๋ฒ ์ธ์ java code๋ก ํํฐ๋ฅผ ์ค์ ํ ์ ์๋๋ฐ ์๋์ ๊ฐ๋ค.
gateway ์ค์ต 1 : java code ๋ก ์ค์ (Spring Cloud Gateway์ ํํฐ ๊ตฌ์ฑ ์์)
Spring Cloud Gateway๋ ๋ง์ดํฌ๋ก์๋น์ค ์ํคํ ์ฒ์์ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๊ฐ ๋ง์ดํฌ๋ก์๋น์ค๋ก ๋ผ์ฐํ ํ๋ ์ญํ ์ ํ๋ค. ์ด ๊ณผ์ ์์ ์์ฒญ์ ์ ์ฒ๋ฆฌํ๊ฑฐ๋ ํ์ฒ๋ฆฌํ ์ ์๋ ํํฐ ๊ธฐ๋ฅ์ ์ ๊ณตํด, ์์ฒญ์ ํ๋ฆ์ ์ ์ฐํ๊ฒ ์ ์ดํ ์ ์๋ค.
์ฃผ์ ํํฐ ๊ตฌ์ฑ ์์
- ํ๋ ๋์ผ์ดํธ(Predicate): ํด๋ผ์ด์ธํธ ์์ฒญ์ด ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ํ๋จํ๋ ์ญํ ์ ํ๋ฉฐ, ์กฐ๊ฑด์ ๋ฐ๋ผ ์์ฒญ์ ๋ผ์ฐํ ํ ์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ค. ์๋ฅผ ๋ค์ด, URL ๊ฒฝ๋ก๋ ํค๋ ๊ฐ์ ๋ฐ๋ผ ๋ค๋ฅธ ์๋น์ค๋ก ๋ผ์ฐํ ํ ์ ์๋ค.
- ํ๋ฆฌํํฐ(Pre-Filter): ์์ฒญ์ด ๊ฐ ๋ง์ดํฌ๋ก์๋น์ค๋ก ์ ๋ฌ๋๊ธฐ ์ ์ ์คํ๋๋ ํํฐ๋ก, ์์ฒญ ํค๋ ์ถ๊ฐ๋ ์์ฒญ ๋ฐ์ดํฐ ์์ ๋ฑ์ ์์ ์ ์ํํ๋ค.
- ํฌ์คํธํํฐ(Post-Filter): ์์ฒญ์ด ๋ง์ดํฌ๋ก์๋น์ค์์ ์ฒ๋ฆฌ๋ ํ์ ์คํ๋๋ ํํฐ๋ก, ์๋ต ํค๋ ์ถ๊ฐ๋ ์๋ต ๋ฐ์ดํฐ ์์ ๋ฑ์ ์์ ์ ์ํํ๋ค.
์์ฒญ ์ ๋ณด๊ฐ ๋ค์ด์ค๊ฒ ๋๋ฉด ๊ทธ๊ฒ ์ด๋ค ๊ฒ์ธ์ง ํ๋จํ๋ ๊ฒ ํ๋ฆฌํฐ์ผ์ดํธ๊ณ ํ๋ฆฌํํฐ๋ ์ด๋ค์์ ์ด ์ผ์ด๋๊ธฐ ์ฌ์ ํํฐ, ์ด๋ค ์ฒ๋ฆฌ๊ฐ ๋๋ ๋ค์์ ํธ์ถ๋๋ ํฌ์คํธํํฐ๊ฐ ๋๋ ์ง๋ค.
์ฝ๋ ์์๋ฅผ ํตํ Spring Cloud Gateway ์ค์
Spring Cloud Gateway์์ ๊ฒฝ๋ก์ ํํฐ๋ฅผ ์ค์ ํ๋ ์ฝ๋๋ก, ๊ฐ ๋ง์ดํฌ๋ก์๋น์ค๋ก์ ์์ฒญ์ ํน์ ๊ฒฝ๋ก์ ๋ฐ๋ผ ๋ผ์ฐํ
ํ๋ค. ์๋ฅผ ๋ค์ด, /auth-api/**
๊ฒฝ๋ก๋ก ๋ค์ด์จ ์์ฒญ์ auth-api
์๋น์ค๋ก, /core-api/**
๊ฒฝ๋ก๋ก ๋ค์ด์จ ์์ฒญ์ core-api
์๋น์ค๋ก ๋ผ์ฐํ
๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Configuration
public class RouteLocatorConfig {
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder, JwtFilter jwtFilter) {
return builder.routes()
.route("auth-api", r -> r.path("/auth-api/**")
.filters(f -> f.addRequestHeader("first-request", "first-request-header")
.addResponseHeader("first-response", "first-response-header"))
.uri("http://localhost:8081/"))
.route("core-api", r -> r.path("/core-api/**")
.filters(f -> f.addRequestHeader("second-request", "second-request-header")
.addResponseHeader("second-response", "second-response-header"))
.uri("http://localhost:8082/"))
.build();
}
}
- ํํฐ ์ค์
addRequestHeader
: ํด๋ผ์ด์ธํธ๊ฐ ๋ณด๋ธ ์์ฒญ์ ํค๋๋ฅผ ์ถ๊ฐํ๋ค. ์๋ฅผ ๋ค์ด, โfirst-requestโ ํค๋๋ฅผ โfirst-request-headerโ ๊ฐ์ผ๋ก ์ถ๊ฐํ์ฌ ํ๋ฆฌํํฐ์์ ์ฒ๋ฆฌํ ์ ์๋ค.addResponseHeader
: ์์ฒญ ์ฒ๋ฆฌ๊ฐ ์๋ฃ๋ ํ ์๋ต์ ํค๋๋ฅผ ์ถ๊ฐํ๋ค. ์๋ฅผ ๋ค์ด, โfirst-responseโ ํค๋๋ฅผ ์๋ต์ ํฌํจํด ํฌ์คํธํํฐ์์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐํํ ์ ์๋ค.
- ๋ผ์ฐํ
ํ๋ฆ
- ํด๋ผ์ด์ธํธ ์์ฒญ: ํด๋ผ์ด์ธํธ๋ API Gateway๋ฅผ ํตํด
/auth-api/**
๋๋/core-api/**
๊ฒฝ๋ก๋ก ์์ฒญ์ ๋ณด๋ธ๋ค. - Spring Cloud Gateway ์ฒ๋ฆฌ: Gateway๋ ๊ฒฝ๋ก์ ๋ฐ๋ผ ์์ฒญ์ ๋ผ์ฐํ ํ๊ณ , ํํฐ๋ฅผ ์ ์ฉํด ํค๋๋ฅผ ์ถ๊ฐํ๋ค.
- ํผ์คํธ ์๋น์ค์ ์ธ์ปจ๋ ์๋น์ค: ๊ฐ๊ฐ์ ๊ฒฝ๋ก์ ๋ฐ๋ผ 8081 ํฌํธ์์ ๋์ํ๋
auth-api
์ 8082 ํฌํธ์์ ๋์ํ๋core-api
์๋น์ค๋ก ์์ฒญ์ด ์ ๋ฌ๋๋ค. - ์๋ต ๋ฐํ: ๊ฐ ์๋น์ค์์ ์์ฒญ์ ์ฒ๋ฆฌํ ํ ์๋ต์ ๋ฐํํ๋ฉฐ, ํฌ์คํธํํฐ๋ฅผ ํตํด ์๋ต ํค๋๊ฐ ์ถ๊ฐ๋๋ค.
- ํด๋ผ์ด์ธํธ ์์ฒญ: ํด๋ผ์ด์ธํธ๋ API Gateway๋ฅผ ํตํด
ํ ์คํธ ๋ฐ ์๋ ๊ณผ์
- ํ
์คํธ ํฌํธ: Gateway๊ฐ 8000 ํฌํธ์์ ์๋ํ๋ฉฐ, ํด๋ผ์ด์ธํธ๋ ์ด ํฌํธ๋ฅผ ํตํด ์์ฒญ์ ๋ณด๋ธ๋ค. ์๋ฅผ ๋ค์ด,
http://localhost:8000/auth-api/...
๋ก ์์ฒญ์ ๋ณด๋ด๋ฉด, Spring Cloud Gateway๊ฐ ์์ฒญ์ 8081 ํฌํธ์auth-api
๋ก ๋ผ์ฐํ ํ๋ค. - ํค๋ ํ์ธ: Spring Cloud Gateway์์ ์ถ๊ฐํ ์์ฒญ ๋ฐ ์๋ต ํค๋ ๊ฐ(
first-request-header
๋ฐfirst-response-header
๋ฑ)์ ํผ์คํธ ์๋น์ค์ ์ธ์ปจ๋ ์๋น์ค์์ ํ์ธํ๊ณ , ์ฒ๋ฆฌ ํ ์๋ต์ผ๋ก ๋ฐํํ ์ ์๋ค.
์์ฝ๐ฉโ๐ซ
Spring Cloud Gateway๋ ํด๋ผ์ด์ธํธ ์์ฒญ์ ํน์ ๋ง์ดํฌ๋ก์๋น์ค๋ก ๋ผ์ฐํ ํ๋ฉฐ, ํ๋ฆฌํํฐ์ ํฌ์คํธํํฐ๋ฅผ ํตํด ์์ฒญ ๋ฐ ์๋ต์ ์ ์ฐํ๊ฒ ๊ด๋ฆฌํ ์ ์๋ค. ์ด๋ฅผ ํตํด API Gateway๋ ๋ผ์ฐํ ์ธ์๋, ๋ณด์, ๋ก๊น , ์๋ต ์บ์ฑ ๋ฑ ๋ค์ํ ๊ธฐ๋ฅ์ ์ํํ๋ฉฐ, MSA ํ๊ฒฝ์์์ ์ ์ฐํ ์๋น์ค ํต์ ์ ์ง์ํ๋ค.
Leave a comment