Software Architecture/MSA

[MSA ๊ตฌ์ถ•ํ•˜๊ธฐ] 6. Spring Cloud Gateway ๋ผ์šฐํŒ… ์„ค์ •ํ•˜๊ธฐ (API Gateway)

bu119 2024. 3. 18. 09:00
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ”Š ์šฐ๋ฆฌ๋Š” Spring Cloud๋ฅผ ํ™œ์šฉํ•˜์—ฌ MSA๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค.

๐Ÿ”Š ์ด๋ฒˆ์—๋Š”, API Gateway์˜ ๋ผ์šฐํŒ…์„ ์„ค์ •ํ•  ๊ฒƒ์ด๋‹ค. 

 

API Gateway ํŒŒํŠธ 2

 

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์„œ๋ฒ„๋ฅผ 2๊ฐœ ์ด์ƒ ์ค€๋น„ํ•˜์ž.

 

๋ผ์šฐํŒ…์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  1. application.properties๋ฅผ ํ†ตํ•œ ๊ฒฝ๋กœ ์„ค์ •
  2. application.yml๋ฅผ ํ†ตํ•œ ๊ฒฝ๋กœ ์„ค์ •
  3. Config ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•œ ๊ฒฝ๋กœ ์„ค์ • (์ง์ ‘ Java ์ฝ”๋“œ ์ž‘์„ฑ)

 

Spring Cloud Gateway ๊ณต์‹๋ฌธ์„œ์˜ 5๋ฒˆ์งธ ์นดํ…Œ๊ณ ๋ฆฌ 5. Route Predicate Factories์— ๋‹ค์–‘ํ•œ ๋ผ์šฐํŒ…์ „๋žต์ด ์žˆ๋‹ค.

 

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

https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-between-route-predicate-factory

 

Spring Cloud Gateway

This project provides an API Gateway built on top of the Spring Ecosystem, including: Spring 6, Spring Boot 3 and Project Reactor. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them

docs.spring.io

 

๐Ÿ”Š ์šฐ๋ฆฌ๋Š” application.yml ์„ค์ •์„ ํ†ตํ•ด Path๊ธฐ๋ฐ˜์˜ ๋ผ์šฐํŒ…์„ ์„ค์ •ํ•  ๊ฒƒ์ด๋‹ค.


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

์•ž์„œ ์ƒ์„ฑํ•œ 3๊ฐœ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๊ฐ€ ์žˆ๋‹ค.

  1. user-service
  2. beverage-service
  3. community-service

๊ฐ ์„œ๋น„์Šค์— ๋žœ๋คํฌํŠธ๋ฅผ ์ง€์ •ํ–ˆ์ง€๋งŒ API Gateway ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ๊ฐ ์„œ๋น„์Šค์— ํฌํŠธ๋ฅผ ์ง€์ •ํ•œ๋‹ค.

1. user-service์˜ ํฌํŠธ๋ฒˆํ˜ธ๋Š” 8081๋ฒˆ์œผ๋กœ ์ง€์ •

# user-service - application.yml

# ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ํฌํŠธ ์ง€์ •
server:
  port: 8081

# ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ์ง€์ •
spring:
  application:
    name: user-service

 

2. beverage-service์˜ ํฌํŠธ๋ฒˆํ˜ธ๋Š” 8082๋ฒˆ์œผ๋กœ ์ง€์ •

# beverage-service - application.yml

server:
  port: 8082

spring:
  application:
    name: beverage-service

 

3. community-service์˜ ํฌํŠธ๋ฒˆํ˜ธ๋Š” 8083๋ฒˆ์œผ๋กœ ์ง€์ •

# community-service - application.yml

server:
  port: 8083

spring:
  application:
    name: community-service

 

 

Spring Cloud Gateway ๋ผ์šฐํŒ… ์„ค์ •ํ•˜๊ธฐ

API Gateway์˜ ํฌํŠธ ๋ฒˆํ˜ธ๋Š” 8000๋ฒˆ์œผ๋กœ ์ง€์ •ํ•œ๋‹ค.

# apigateway-service - application.yml

server:
  port: 8000

spring:
  application:
    name: apigateway-service
  cloud:
    gateway:
      routes:
        - id: user-service
          # ์–ด๋””๋กœ ๊ฐˆ๊บผ๋‹ˆ?
          uri: http://localhost:8081/
          # ์กฐ๊ฑด์ ˆ
          predicates:
            - Path=/user-service/**
        - id: beverage-service
          uri: http://localhost:8082/
          predicates:
            - Path=/beverage-service/**
        - id: community-service
          uri: http://localhost:8083/
          predicates:
            - Path=/community-service/**
  • spring: cloud: gateway: routes:
    • ๋ผ์šฐํŒ… ์„ค์ •
  • spring: cloud: gateway: routes: - id:
    • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ณ ์œ  ์ด๋ฆ„
    • ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ id๊ฐ’์œผ๋กœ ์ž„์˜๋กœ ์ž‘์„ฑํ•ด๋„ ๋ฌด๊ด€ํ•˜๋‹ค.
  • spring: cloud: gateway: routes: - uri:
    • ์ง„์ž… ์ฃผ์†Œ
  • spring: cloud: gateway: routes: - predicates:  
    • ๋ผ์šฐํŒ… ์กฐ๊ฑด (์กฐ๊ฑด์ ˆ)
  • spring: cloud: gateway: routes: - predicates: - Path =
    • Path๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ผ์šฐํŒ…์„ ์„ค์ •
    • ex) - Path = /user-service/**
    • http://localhost:8000/user-service/ ์œผ๋กœ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์œ ๋ ˆ์นด ์„œ๋ฒ„์—์„œ uri์— ํ•ด๋‹นํ•˜๋Š” ์ฃผ์†Œ๋ฅผ ์ฐพ์•„ ๊ทธ๊ณณ์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.

 

Spring Cloud Gateway ์„œ๋ฒ„  ์‹คํ–‰

๊ฐ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์„œ๋ฒ„๋ฅผ ๋จผ์ € ์‹คํ–‰ํ•œ ํ›„์— API Gateway ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

์ด๋Œ€๋กœ ๋ผ๋ฉด "http://localhost:8081/"๊ณผ "http://localhost:8000/user-service/"๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚˜์•ผ ํ•œ๋‹ค.

 

1. user-service ์„œ๋ฒ„ ์‹คํ–‰

user-service์˜ "http://localhost:8081/"์˜ ์ฃผ์†Œ๋กœ ์‹คํ–‰ํ•˜๋ฉด "user-service"๋ผ๋Š” ๊ธ€์ž๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

 

2. apigateway-service ์„œ๋ฒ„ ์‹คํ–‰

apigateway-service์˜ "http://localhost:8000/user-service/"์˜ ์ฃผ์†Œ๋กœ API๋ฅผ ์‹คํ–‰ํ•˜๋ฉด  "user-servicer"๋ผ๋Š” ๊ธ€์ž๊ฐ€ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค.

 

๐Ÿ”Š "http://localhost:8081/"๊ณผ "http://localhost:8000/user-service/"๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚˜์•ผ ํ•˜๋Š”๋ฐ ์œ„์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณด๋ฉด ๋‹ค๋ฅด๋‹ค. ๊ทธ ์ด์œ ๋ฅผ ์•Œ์•„๋ณด์ž.

728x90
๋ฐ˜์‘ํ˜•