๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’Š Java & Kotlin & Spring/- spring framework +

Spring Cloud Gateway ๋ฅผ ์ด์šฉํ•ด API Gateway๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์‹ค์Šตํ•ด๋ณด์ž (Eureka Server ๋ฅผ ์ด์šฉํ•ด Load Balancing ํ•˜๊ธฐ)

by Wonit 2021. 4. 24.

ํ•ด๋‹น ๊ธ€์€ Spring Cloud Netflix Eureka์— ์˜์กดํ•˜๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ํ™˜๊ฒฝ์„ ๋”ฐ๋ผํ•˜์‹œ๋ ค๋ฉด Eureka ๊ธ€์„ ํ™•์ธํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

๋ชฉ์ฐจ

  • Gateway Pattern
  • Gateway๊ฐ€ ๋„์ž…๋˜๋ฉด?
  • Spring Cloud Gateway
    • Gateway๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•
    • Gateway ๋‚ด๋ถ€ ๊ตฌ์กฐ
  • ์‹ค์Šตํ•˜๊ธฐ
    • ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ
    • ์˜์กด์„ฑ
    • ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์—ฐ๊ฒฐํ•˜๊ธฐ
    • Eureka Server ์—์„œ ๊ฐ๊ฐ ์ธ์Šคํ„ด์Šค ์ •๋ณด๋ฅผ ๋ฐ›์•„ ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ ํ•˜๊ธฐ

Gateway Pattern

 

Microservice ์—์„œ Gateway ํŒจํ„ด์€ ๊ฐ€์žฅ ํ”ํ•˜๊ณ  ์œ ์šฉํ•˜๊ฒŒ ์“ฐ๋Š” ํŒจํ„ด ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

 

Spring Cloud์™€ ๋ณ„๊ฐœ๋กœ Gateway ํŒจํ„ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์‚ฌํ•ญ์€ MSA์˜ ํ•ต์‹ฌ ๊ตฌ์„ฑ ์š”์†Œ - API Gateway ํŒจํ„ด ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ง€๋‚œ ์‹œ๊ฐ„ ์šฐ๋ฆฌ๋Š” Eureka๋กœ Discovery Pattern ์„ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

 

์ง€๋‚œ ์‹œ๊ฐ„์—์„œ ๊ตฌํ˜„ํ•œ Eureka Server-Client ์˜ ๊ตฌ์กฐ๋ฅผ ์ž ์‹œ ์‚ดํŽด๋ณด์ž.

 

 

๊ฐ๊ฐ์˜ ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค๋“ค์˜ ํฌํŠธ๋Š” 8812, 51728, 9271 ์€ Eureka Server์— Registration ๋˜์–ด์žˆ๋Š” ์ƒํƒœ๋กœ ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค๋“ค์˜ ํฌํŠธ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ Eureka Dashboard์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์˜€์—ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ์ƒํ™ฉ์ด๋ผ๋ฉด ์–ด๋–จ๊นŒ?

 

User-Service(8812 ํฌํŠธ) ์—์„œ login ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  Order-Service(51728 ํฌํŠธ) ์—์„œ ์ƒํ’ˆ์„ ์ฃผ๋ฌธ ํ•œ ๋’ค, Delivery-Service(9271) ํฌํŠธ์—์„œ ๋ฐฐ๋‹ฌ ์ฃผ๋ฌธ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

๋ฌธ์ œ ์ƒํ™ฉ์€ ํ•ด๋‹น ์ฃผ๋ฌธ๊ณผ ๋ฐฐ๋‹ฌ์„ ์œ„ํ•ด์„œ ๊ฐ๊ฐ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋“ค์€ ๋‹ค๋ฅธ ์„œ๋น„๋“ค์˜ ํฌํŠธ๋ฅผ ๋ชจ๋‘ ์•Œ๊ณ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ํฌํŠธ๊ฐ€ ๋ฐ”๋€๋‹ค๋ฉด ํฌํŠธ๊ฐ€ ๋ฐ”๋€ ์„œ๋ฒ„๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์„œ๋ฒ„๋Š” ๋ฐ”๋€ ํฌํŠธ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ๋‹ค์‹œ ๋ฐœ๋“œ -> ๋ฐฐํฌ๋ฅผ ํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ค.

 

์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ ์šฐ๋ฆฌ๋Š” ํ•ฉ๋ฆฌ์ ์ธ ์ƒ๊ฐ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋งŒ์•ฝ ๋ชจ๋“  ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค์˜ ํฌํŠธ๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด? ํ˜น์€ ๋ชจ๋“  ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ํฌํŠธ๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋ฉด?

 

์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ Spring Gateway๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

Gateway๊ฐ€ ๋„์ž…๋˜๋ฉด?

 

์œ„์˜ ์ƒํ™ฉ์—์„œ Gateway๋ฅผ ๋„์ž…ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

 

User ์„œ๋ฒ„๊ฐ€ Order ์„œ๋ฒ„์— ์–ด๋– ํ•œ ์š”์ฒญ์„ ๋ณด๋‚ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž!

 

  1. User ์„œ๋ฒ„๋Š” Order ์„œ๋ฒ„์— ๋ณด๋‚ด์•ผํ•  ์š”์ฒญ์„ Gateway๋กœ ์ „๋‹ฌ
  2. Gateway๋Š” Eureka Server๋กœ Order ์„œ๋ฒ„์˜ ์ •๋ณด discovery
  3. Gateway๊ฐ€ Order Server๋กœ ์—ฐ๊ฒฐ

 

์ด๋ ‡๊ฒŒ ๋œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ์ ๋“ค์ด ์กด์žฌํ•œ๋‹ค.

 

  • ๊ฐ๊ฐ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋“ค์€ ์„œ๋กœ์˜ ํฌํŠธ ๋ฒˆํ˜ธ๋ฅผ ๋ชฐ๋ผ๋„ ๋œ๋‹ค.
  • Front ์—์„œ๋Š” ์š”์ฒญ์„ Gateway๋กœ๋งŒ ๋ณด๋‚ด๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— Gateway ํฌํŠธ๋งŒ ์•Œ๋ฉด ๋ชจ๋“  ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ชจ๋“  ์š”์ฒญ์€ Gateway ๋ฅผ ๊ฑฐ์น˜๊ธฐ ๋•Œ๋ฌธ์— ๋กœ๊ทธ๋ฅผ ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค.
  • Gateway ๊ฐ€ ์š”์ฒญ์˜ ์ง„์ž…์ ์ด๋ฏ€๋กœ ํ†ตํ•ฉ ์ธ์ฆ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ์žฅ์ ์„ ์ตœ์ดˆ์—๋Š” Spring Cloud Netflix Zuul์„ ์ด์šฉํ–ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ Netflix Zuul์€ Servlet MVC์™€ Tomcat์„ ์ด์šฉํ•˜๋Š” ์ด์œ  ๋•Œ๋ฌธ์— ํ˜„์žฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํ๋ฆ„์ธ ๋น„๋™๊ธฐ ํ†ต์‹  ๋กœ์ง์— ์ทจ์•ฝํ–ˆ์—ˆ๋‹ค.

 

๊ทธ์— ๋Œ€ํ•œ ๋Œ€์•ˆ์„ ์ฐพ๊ณ ์ž Spring Cloud๋Š” Tomcat์„ ๋Œ€์‹ ํ•˜์—ฌ Netty ์„œ๋ฒ„๋ฅผ ๋‚ด์žฅํ•œ Web Flux ๊ธฐ๋ฐ˜ Gateway ์ธ Spring Cloud Gateway๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋˜์—ˆ๋‹ค.

 

Spring Cloud Gateway

Spring Cloud Gateway ๊ณต์‹ ๋ฌธ์„œ

 

Spring Cloud Gateway๋Š” Spring WebFlux ์œ„์—์„œ API Gateway๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

 

Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

Spring Cloud Gateway (SCG)๋Š” ๊ฐ„ํŽธํ•˜๋ฉด์„œ๋„ ํšจ๊ณผ์ ์ธ API ๋ผ์šฐํŒ… ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๊ณ , ๋ณด์•ˆ ๋ชจ๋‹ˆํ„ฐ๋ง, ๋ฉ”ํŠธ๋ฆญ, ๋ณต์›๋ ฅ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค.

 

Spring Cloud Gateway ์—์„œ ์‚ฌ์šฉํ•˜๋Š” 3๊ฐ€์ง€ ๋ช…์นญ

 

์šฐ์„  Spring Cloud Gateway ์—์„œ ์‚ฌ์šฉํ•˜๋Š” 3๊ฐ€์ง€ ์šฉ์–ด์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์•Œ์•„๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

 

  1. Route
  2. Predicate
  3. Filter

 

Route (๋ผ์šฐํŠธ)

๋ชฉ์ ์ง€์˜ URI์™€ Predicates๋ผ๋Š” ์กฐ๊ฑด๋“ค์˜ ๋ชฉ๋ก ๊ทธ๋ฆฌ๊ณ  ํ•„ํ„ฐ๋“ค์„ ์ด์šฉํ•˜์—ฌ ์–ด๋–ค ๊ณณ์œผ๋กœ Routing ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๋ช…์‹œํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.


์šฐ๋ฆฌ๋Š” Routes ์— ๋Œ€ํ•œ ์†์„ฑ๊ณผ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ Spring Cloud๊ฐ€ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•˜๋ผ๊ณ  ์ง€์‹œํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

Predicate

์‰ฝ๊ฒŒ ์กฐ๊ฑด์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.


์•„๋ž˜์—์„œ ์šฐ๋ฆฌ๋Š” Predicates ๋ฅผ ์ž‘์„ฑํ•  ๊ฒƒ์ธ๋ฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

 

predicated: -Path=/user/**

 

๊ทธ๋Ÿผ user ๋ผ๋Š” ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•œ ๊ฒƒ์ด๋‹ค.

 

Filter

๋“ค์–ด์˜ค๋Š” ์š”์ฒญ๊ณผ ์‘๋‹ต, Request, Response์„ ํŠน์ • ํ•„ํ„ฐ๋ฅผ ํƒ€๊ฒŒ ํ•จ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๊ฑฐ๋‚˜ ํ—ค๋”๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๊ณ , ํ•ด๋‹น ํ•„ํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ๋กœ๊ทธ ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜๊ฒŒ ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค.

 

Spring Cloud Gateway์˜ ๊ตฌ์กฐ

 

Spring Cloud Gateway๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

 

  1. Client ๋Š” Spring Cloud Gateway ์— ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.
  2. Gateway Handler Mapping ์—์„œ ํ•ด๋‹น ์š”์ฒญ์— ๋Œ€ํ•œ Route์™€ Predicates๊ฐ€ ์ผ์น˜ํ•œ๋‹ค๊ณ  ํŒ๋‹จํ•˜๋ฉด ํ•ด๋‹น ์š”์ฒญ์€ Gateway Web handler๋กœ ๋ณด๋‚ด์ง„๋‹ค.
  3. handler ์—์„œ Filter Chain ์„ ์ด์šฉํ•ด์„œ ์‚ฌ์ „ ํ•„ํ„ฐ ํ˜น์€ ์‚ฌํ›„ ํ•„ํ„ฐ๋กœ ๋‚˜๋ˆ„์–ด ๋™์ž‘ํ•œ๋‹ค.
  4. ํ•„ํ„ฐ๋ง์ด ๋œ ํ›„ ์‹ค์ œ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค์—๊ฒŒ ์ „๋‹ฌ๋œ๋‹ค.

 

Spring Cloud Gateway ๋‚ด๋ถ€ ๋“ค์—ฌ๋‹ค๋ณด๊ธฐ

 

Spring Cloud Gateway๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€์˜ ํŒŒํŠธ๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

  • Gateway Handler Mapping
    • Gateway ๊ฐ€ CLient๋กœ ๋ถ€ํ„ฐ ์–ด๋–ค ์š”์ฒญ์ด ์™”๋Š”์ง€ ํ™•์ธํ•˜๊ณ  Mapping ํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • Predicate
    • Handler Mapping ์‹œ์— ํ•„์š”ํ•œ Uri ์ •๋ณด๋‚˜, Path ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๋Š” ์ฃผ์ฒด๊ฐ€ ๋œ๋‹ค.
  • Filter
    • Handler Mapping์ด ๋œ ํ›„ ๋“ค์–ด์˜จ ์š”์ฒญ์— ๋Œ€ํ•œ ํ•„ํ„ฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • 2๊ฐœ์˜ ํ•„ํ„ฐ๋กœ ํฌ๊ฒŒ ๋‚˜๋‰˜๋ฉฐ ์‚ฌ์ „(Pre Filter)์™€ ์‚ฌํ›„(Post Filter)๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.
      • Pre Filter
        • ํŠน์ • ์ž‘์—…์ด ์ผ์–ด๋‚˜๊ธฐ ์ „์— ์ง€์ •
      • Post Filter
        • ํŠน์ • ์ž‘์—…์ด ๋๋‚œ ํ›„์— ์ง€์ •
    • ์ด๋Ÿฐ ํ•„ํ„ฐ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” yml ์„ค์ • ํŒŒ์ผ์—๋„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ณ  java code ์—์„œ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค

 

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

 

์ง€๋‚œ ์‹œ๊ฐ„์— ๋งŒ๋“ค์—ˆ๋˜ ํด๋” ์•„๋ž˜์— gateway-service ๋ผ๋Š” Spring ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์ž.

 

์˜์กด์„ฑ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

์šฐ๋ฆฌ๊ฐ€ ์ง€๋‚œ ์‹œ๊ฐ„์— ํ–ˆ๋˜ ํ”„๋กœ์ ํŠธ์— Gateway๋ฅผ ๋„์ž…ํ•ด๋ณด์ž.

 

์šฐ์„  Gateway๋ฅผ Eureka Client๋กœ ๋™์ž‘์‹œํ‚ค๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ Eurke Client ์˜์กด์„ฑ๊ณผ Spring Cloud Gateway ์˜์กด์„ฑ์„ ํ•จ๊ป˜ ์ถ”๊ฐ€์‹œ์ผœ๋ณด์ž.

 

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์—ฐ๊ฒฐํ•˜๊ธฐ

ํ•ด๋‹น ์‹ค์Šต ๋‚ด์šฉ์€ Spring Cloud Netflix Eureka์— ์˜์กดํ•˜๋Š” ๋‚ด์šฉ ์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ํ™˜๊ฒฝ์„ ๋”ฐ๋ผํ•˜์‹œ๋ ค๋ฉด Eureka ๊ธ€์„ ํ™•์ธํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

 

์•ž์„œ ๊ณ„์† ์ด์•ผ๊ธฐํ–ˆ์ง€๋งŒ Gateway๋„ ํ•˜๋‚˜์˜ ์„œ๋น„์Šค์ด๋‹ค.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ Eureka Server์— ๋“ฑ๋ก์‹œ์ผœ์„œ Dashboard ์—์„œ ํ•œ ๋ฒˆ์— ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ Eureka Discover Client๋กœ ๋“ฑ๋ก์‹œ์ผœ์ฃผ์ž.

 

์—ญ์‹œ application.properties ํŒŒ์ผ์„ application.yml ํŒŒ์ผ๋กœ ๋ณ€๊ฒฝ์‹œ์ผœ ์„ค์ • ์ •๋ณด๋“ค์„ ์ถ”๊ฐ€ํ•ด์ฃผ์ž.

 

server:
  port: 8000

eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka

spring:
  application:
    name: gateway-service

 

๊ทธ๋ฆฌ๊ณ  Eureka์™€ ๋‹ค๋ฅธ Microservice ๋“ค์„ ์‹คํ–‰์‹œ์ผœ์„œ Eureka Dashboard์— ์ž˜ ๋‚˜์˜ค๋Š”์ง€ ๋จผ์ € ํ™•์ธํ•ด๋ณด์ž.

 

  1. Eureka Server ์‹คํ–‰
  2. User Microservice ์‹คํ–‰
  3. Order Microservice ์‹คํ–‰
  4. Gateway service ์‹คํ–‰

 

๊ทธ๋Ÿผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด Eureka Dashboard ์— ์ž˜ ๋“ฑ๋ก๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด์ œ ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค๋ฅผ Gateway๋กœ ์—ฐ๊ฒฐํ•ด๋ณด์ž.

 

์„œ๋น„์Šค๋ฅผ Gateway ๋กœ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € Gateway Service ์— application.yml์„ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.

 

yml์— ๋“ค์–ด๊ฐ€์•ผ ํ•  ์„ค์ • ์ •๋ณด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  • spring.cloud.gateway.routes : ๋ผ์šฐํŒ…์— ๋Œ€ํ•œ ์ •๋ณด
  • spring.cloud.gateway.routes.id : ํ•ด๋‹น ๋ผ์šฐํŒ…์ด ์–ด๋– ํ•œ ์ด๋ฆ„์œผ๋กœ ๋ผ์šฐํŒ… ๋  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์ด๋ฆ„
  • spring.cloud.gateway.routes.uri : ํ˜„์žฌ ๋ผ์šฐํŒ…์„ ์–ด๋””์— ํฌ์›Œ๋”ฉ ์‹œํ‚ฌ ๊ฒƒ์ธ์ง€๋ฅผ ๋ช…์‹œํ•ด์ค€๋‹ค.
  • spring.cloud.gateway.routes.predicates : ์กฐ๊ฑด์‹์œผ๋กœ ํŠน์ • ์š”์ฒญ์ด predicates์— ๋งž๋Š” ์กฐ๊ฑด์œผ๋กœ ๋“ค์–ด์˜ค๋ฉด ํ•ด๋‹น route๋ฅผ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์„ ์ง€์ •ํ•œ๋‹ค.
server:
  port: 8000

eureka:
  client:
    fetch-registry: true # ์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ ํ™œ์„ฑํ™”
    register-with-eureka: true # ์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ ํ™œ์„ฑํ™”
    service-url:
      defaultZone: http://localhost:8761/eureka # ์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ๋กœ ๋“ฑ๋ก

spring:
  application:
    name: gateway-service

  cloud:
    gateway:
      routes:
        - id: user-service
          uri: http://localhost:64412 # ํฌ์›Œ๋”ฉํ•  ์ฃผ์†Œ, http://localhost:8000/user ๋กœ ๋“ค์–ด์˜ค๋ฉด http://localhost:64412 ๋กœ ํฌ์›Œ๋”ฉ
          predicates:
            - Path=/user/** # ํ•ด๋‹น gateway ์„œ๋ฒ„์˜ /user/**๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์€ user-service๋กœ ์ธ์‹ํ•˜๊ฒ ๋‹ค๋Š” ์กฐ๊ฑด

        - id: order-service
          uri: http://localhost:54412 # ํฌ์›Œ๋”ฉ ํ•  ์ฃผ์†Œ, http://localhost:8000/order ๋กœ ๋“ค์–ด์˜ค๋ฉด http://localhost:54412 ๋กœ ํฌ์›Œ๋”ฉ
          predicates:
            - Path=/order/** # ํ•ด๋‹น gateway ์„œ๋ฒ„์˜ /order/**๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์€ order-service๋กœ ์ธ์‹ํ•˜๊ฒ ๋‹ค๋Š” ์กฐ๊ฑด

 

์œ„์˜ ์ƒํƒœ๊ฐ€ ๋œ๋‹ค๋ฉด ์ด์ œ Gateway ๋กœ ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ์ด predicates์— ์ถฉ์กฑํ•œ๋‹ค๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์ง€์ •ํ•œ uri๋กœ ํฌ์›Œ๋”ฉ์„ ํ•˜๋Š” ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ฃผ์˜ํ•ด์•ผ ํ•  ๊ฒƒ์ด ์žˆ๋‹ค.

 

ํฌ์›Œ๋”ฉ์„ ํ•  ๋•Œ, http://localhost:8000/order/info ๋กœ ์š”์ฒญ์ด ๋“ค์–ด์™”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํฌ์›Œ๋”ฉ์ด ๋œ๋‹ค.

 

ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ์˜ ์„œ๋น„์Šค ์ธ์Šคํ„ด์Šค์˜ Request Mapping ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋˜์–ด์žˆ๋‹ค.

// ์ƒ๋žต
public class UserServiceApplication {
    // ์ƒ๋žต
    @GetMapping("/info")
    public String info(@Value("${server.port}") String port) {
        return "User ์„œ๋น„์Šค์˜ ๊ธฐ๋ณธ ๋™์ž‘ Port: {" + port + "}";
    }
}

// ์ƒ๋žต
public class OrderServiceApplication {
    // ์ƒ๋žต
    @GetMapping("/info")
    public String info(@Value("${server.port}") String port) {
        return "Order ์„œ๋น„์Šค์˜ ๊ธฐ๋ณธ ๋™์ž‘ Port: {" + port + "}";
    }
}

๋งŒ์•ฝ ์ด ์ƒํƒœ์—์„œ ๊ทธ๋Œ€๋กœ ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค๋ฉด 404 Error ๋ฅผ ๋งž๊ฒŒ ๋œ๋‹ค.

 

๋‹น์—ฐํžˆ ์šฐ๋ฆฌ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋“ค์€ localhost/info ๋กœ๋งŒ Request Mapping์ด ๋˜์–ด์žˆ๋Š”๋ฐ Gateway๋Š” localhost/user/info ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด์ฃผ๋‹ˆ..

 

์ง€๊ธˆ์€ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ UserService์™€ OrderService์˜ RequestMapping ์ •๋ณด๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋„๋ก ํ•˜์ž.

 

๋‹ค์Œ์— ๊ฐ€์„œ RewitePath๋ฅผ ์ด์šฉํ•˜์—ฌ RequestMapping ์ •๋ณด๋ฅผ ๋ฐ”๊พธ์ง€ ์•Š๊ณ  ๊ฒฝ๋กœ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณผ ๊ฒƒ์ด๋‹ค.

// ์ƒ๋žต
public class UserServiceApplication {
    // ์ƒ๋žต
    @GetMapping("/user/info")
    public String info(@Value("${server.port}") String port) {
        return "User ์„œ๋น„์Šค์˜ ๊ธฐ๋ณธ ๋™์ž‘ Port: {" + port + "}";
    }
}

// ์ƒ๋žต
public class OrderServiceApplication {
    // ์ƒ๋žต
    @GetMapping("/order/info")
    public String info(@Value("${server.port}") String port) {
        return "Order ์„œ๋น„์Šค์˜ ๊ธฐ๋ณธ ๋™์ž‘ Port: {" + port + "}";
    }
}

 

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ๊ฐ๊ฐ ์ž˜ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

Eureka Server ์—์„œ ๊ฐ๊ฐ ์ธ์Šคํ„ด์Šค ์ •๋ณด๋ฅผ ๋ฐ›์•„ ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ ํ•˜๊ธฐ

์ง€๊ธˆ ์กฐ๊ธˆ ์•„์‰ฌ์šด ๋ถ€๋ถ„์ด ์žˆ๋‹ค.


Eureka Server ์—์„œ ๊ฐ๊ฐ ์ธ์Šคํ„ด์Šค ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š”๋ฐ, Gateway ์—์„œ๋Š” ์ง์ ‘ ํฌํŠธ์™€ ์„œ๋ฒ„์˜ ์ฃผ์†Œ๋ฅผ ๋ช…์‹œํ•ด์ฃผ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์•„์˜ˆ Eureka Server๋กœ ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ์„ ์ˆ˜ํ–‰ํ•ด๋ณด์ž.

 

๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” Eureka Dashboard์— ์žˆ๋Š” Application Name ์„ ์•Œ์•„์•ผ ํ•œ๋‹ค.

 

 

๊ฐ๊ฐ ORDER-SERVICE์™€ USER_SERVICE ๋กœ ๋˜์–ด ์žˆ์œผ๋‹ˆ application.yml ์—์„œ ํฌ์›Œ๋”ฉํ•  ์ฃผ์†Œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜์ž.

 

spring:
  # ์ƒ๋žต
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://USER-SERVICE
          predicates:
            - Path=/user/**
        - id: order-service
          uri: lb://ORDER-SERVICE
          predicates:
            - Path=/order/**

๊ทธ๋Ÿผ User-Service ๊ฐ€ Scale-Out ๋˜๋”๋ผ๋„, ์ฆ‰ ํฌํŠธ๊ฐ€ ์—ฌ๋Ÿฌ ๋‚˜๋‰œ๋‹ค๊ฑฐ๋‚˜ ํ˜น์€ uri ์ •๋ณด๊ฐ€ ๋ณ€๊ฒฝ๋˜๋”๋ผ๋„ Gateway ์—์„œ๋Š” ๊ทธ๊ฑธ ์•Œ ํ•„์š”๊ฐ€ ์—†๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

์ด๊ฒŒ ๋ฐ”๋กœ Service Discovery ์˜ ๋งค๋ ฅ์ด๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ๋™์ž‘์‹œ์ผœ๋ณด๋ฉด ์ž˜ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ์˜ค๋Š˜์€ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ Spring Cloud Gateway ๋ฅผ ๋งŒ์ ธ๋ณด๊ณ  ๊ฐ๊ฐ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋“ค๊ณผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋™์ž‘์‹œ์ผœ ๋ณด์•˜๋‹ค.

 

์•„์ง Gateway ์˜ ์•„์ฃผ ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ๋งŒ ๋‹ค๋ค˜์œผ๋ฏ€๋กœ ๋‹ค์Œ ์‹œ๊ฐ„ ๋ถ€ํ„ฐ Filter๋ฅผ ์ด์šฉํ•ด Request์™€ Response๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

๋Œ“๊ธ€