Software Architecture/MSA

[MSA ๊ตฌ์ถ•ํ•˜๊ธฐ] 8. Spring Cloud Gateway์— ํ•„ํ„ฐ ์ ์šฉํ•˜๊ธฐ (API Gateway)

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

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

๐Ÿ”Š ์ด๋ฒˆ์—๋Š”, Spring Cloud Gateway Routes์˜ Filters๋ฅผ ์„ค์ •ํ•  ๊ฒƒ์ด๋‹ค.

 

API Gateway ํŒŒํŠธ 4

 

https://bu119.tistory.com/94

 

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

๐Ÿ”Š ์šฐ๋ฆฌ๋Š” Spring Cloud๋ฅผ ํ™œ์šฉํ•˜์—ฌ MSA๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. ๐Ÿ”Š ์ด๋ฒˆ์—๋Š”, API Gateway์˜ ๋ผ์šฐํŒ…์„ ์„ค์ •ํ•  ๊ฒƒ์ด๋‹ค. API Gateway ํŒŒํŠธ 2 ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์„œ๋ฒ„๋ฅผ 2๊ฐœ ์ด์ƒ ์ค€๋น„ํ•˜์ž. ๋ผ์šฐํŒ…์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” 3๊ฐ€

bu119.tistory.com

Spring Cloud Gateway ๋ผ์šฐํŒ…์„ ์„ค์ •ํ–ˆ์ง€๋งŒ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์ œ๋Œ€๋กœ ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์•˜๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋Š” Gateway ์„ค์ • ๊ฒฝ๋กœ ๋ฐ ํ”„๋ก์‹œ ์„ค์ •๊ณผ ์‹ค์ œ ์„œ๋น„์Šค ์ฝ”๋“œ ๊ฐ„์˜ ๋ถˆ์ผ์น˜ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ–ˆ๋‹ค.

 

https://bu119.tistory.com/97

 

[MSA ๊ตฌ์ถ•ํ•˜๊ธฐ] 7. Spring Cloud Gateway๋ฅผ ํ™œ์šฉํ•œ ๊ฒฝ๋กœ ์ˆ˜์ • (API Gateway)

๐Ÿ”Š ์šฐ๋ฆฌ๋Š” Spring Cloud๋ฅผ ํ™œ์šฉํ•˜์—ฌ MSA๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. ๐Ÿ”Š ์•ž์„œ, API Gateway์˜ ๋ผ์šฐํŒ…์„ ์„ค์ •ํ–ˆ๋Š”๋ฐ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋‹ค๋ฅด๊ฒŒ ๋‚˜ํƒ€๋‚ฌ๋‹ค. ๊ทธ ์ด์œ ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. API Gateway ํŒŒํŠธ 3 ์šฐ์„ , ์ž‘์„ฑํ•œ ์ฝ”

bu119.tistory.com

Gateway ์„ค์ • ๊ฒฝ๋กœ์™€ ์„œ๋น„์Šค ๊ฒฝ๋กœ ์ผ์น˜๋ฅผ ์œ„ํ•ด user-service์˜ ์ปจํŠธ๋กค๋Ÿฌ ๊ฒฝ๋กœ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ๋‹จ์ˆœํžˆ ๊ฒฝ๋กœ๋ฅผ ๋ผ์šฐํŒ… ๋Œ€์ƒ์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

โ“๊ฒฝ๋กœ ์ˆ˜์ •์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉด, user-service์˜ ๋ชจ๋“  ์ปจํŠธ๋กค๋Ÿฌ์— @RequestMapping("/user-service") ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•œ๋‹ค. ๋‹ค๋ฅธ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋“ค๋„ ๊ฐ ์„œ๋น„์Šค์˜ ์ด๋ฆ„์œผ๋กœ @RequestMapping์„ ์ถ”๊ฐ€ํ•ด์•ผ ํžˆ๊ณ  ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„์ด ์ƒ๋‹นํžˆ ๋น„ํšจ์œจ์ ์ด๋ผ๊ณ  ๋Š๊ปด์กŒ๋‹ค. ๊ตฌ๊ธ€ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค. Gateway์˜ Filter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ URI ๊ฒฝ๋กœ๋ฅผ ์žฌ์ž‘์„ฑํ•˜์—ฌ ์š”์ฒญ์„ ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค๋กœ ๋ผ์šฐํŒ… ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.


โญAPI Gateway ๋ผ์šฐํŒ… ํ•„ํ„ฐ ์„ค์ •์„ ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ?

ํด๋ผ์ด์–ธํŠธ๊ฐ€ http://localhost:8000/user-service/์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋ƒˆ์„ ๋•Œ http://localhost:8081/user-service/๋กœ ํ”„๋ก์‹œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” user-service์˜ ๋ชจ๋“  ์ปจํŠธ๋กค๋Ÿฌ์—@RequestMapping("/user-service")์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•œ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ์ ‘๊ทผ ๋ฐฉ์‹์€ ๋งŽ์€ ์–‘์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๊ณ , ์œ ์ง€๋ณด์ˆ˜์— ์–ด๋ ค์›€์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ์„œ๋น„์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•  ๋•Œ๋งˆ๋‹ค ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

 

๋”ฐ๋ผ์„œ, Gateway์˜ Filter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ URI ๊ฒฝ๋กœ๋ฅผ ์žฌ์ž‘์„ฑํ•˜์—ฌ ์š”์ฒญ์„ ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค๋กœ ๋ผ์šฐํŒ… ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํด๋ผ์ด์–ธํŠธ๊ฐ€ http://localhost:8000/user-service/๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด, Gateway์˜ Filter๊ฐ€ ์š”์ฒญ์˜ URI๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ http://localhost:8081๋กœ ํ”„๋ก์‹œ ํ•˜๊ฒŒ ๋œ๋‹ค.

 

๋‹ค์‹œ ๋งํ•ด, ์•„๋ž˜์˜ ํ˜•ํƒœ๋กœ ๋ผ์šฐํŒ… ํ•˜๋ ค๋ฉด Gateway์˜ Filter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ URI ๊ฒฝ๋กœ๋ฅผ ์žฌ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. 

http://localhost:8000/user-service/name  โžก๏ธ  http://localhost:8081/name

  


 

 

Spring Cloud Gateway Filter ์ ์šฉ ๋ฐฉ๋ฒ•

Filter ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  1. yml ๋˜๋Š” properties ํŒŒ์ผ์— ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
  2. Java ์ฝ”๋“œ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ”Š ํ˜„์žฌ, ์šฐ๋ฆฌ๋Š” yml ํŒŒ์ผ์„ ํ†ตํ•ด Spring Cloud Gateway Filter๋ฅผ ์ •์˜ํ•  ๊ฒƒ์ด๋‹ค.

 

๋ฐ˜์‘ํ˜•

์šฐ๋ฆฌ์˜ ์„œ๋น„์Šค์—๋Š” 3๊ฐœ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๊ฐ€ ์žˆ๋‹ค.

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

 

2. application.yml๋ฅผ ํ†ตํ•œ ๊ฒฝ๋กœ ์„ค์ •

apigateway-service์˜ application.yml์— filters ์ถ”๊ฐ€

RewritePath ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ๊ฒŒ์ดํŠธ์›จ์ด๊ฐ€ ์š”์ฒญ์„ ํ”„๋ก์‹œํ•  ๋•Œ ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๊ฒฝ๋กœ๋กœ ์š”์ฒญ์„ ์žฌ์ž‘์„ฑํ•œ๋‹ค.

# apigateway-service: application.yml

spring:
  application:
    name: apigateway-service
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: http://localhost:8081
          predicates:
            - Path=/user-service/**
          filters:
            - RewritePath=/user-service/(?<segment>.*),/$\{segment} # ์ถ”๊ฐ€
  • RewritePath ํ•„ํ„ฐ๋Š” ์š”์ฒญ์˜ ๊ฒฝ๋กœ๋ฅผ ์žฌ์ž‘์„ฑํ•œ๋‹ค.
  • =/user-service/(?<segment>.*)๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฝ๋กœ๋ฅผ ๋‹ค์‹œ ์“ฐ๋Š” ํŒจํ„ด์„ ์ •์˜ํ•œ๋‹ค.
    • ์—ฌ๊ธฐ์„œ /user-service/ ๋‹ค์Œ์— ์˜ค๋Š” ๋ชจ๋“  ๊ฒƒ์„ ํฌํ•จํ•˜๋Š” ๋ถ€๋ถ„์„ ์บก์ฒ˜ํ•˜์—ฌ $\{segment}์— ํ• ๋‹นํ•œ๋‹ค.
    • ์ด๊ฒƒ์€ URI ๊ฒฝ๋กœ์—์„œ /user-service/ ๋ถ€๋ถ„์„ ์บก์ฒ˜ํ•˜์ง€ ์•Š๊ณ  ๊ทธ ๋’ค์˜ ๋ชจ๋“  ๊ฒƒ์„ ์บก์ฒ˜ํ•œ๋‹ค.
  • /$\{segment}๋Š” ์žฌ์ž‘์„ฑ๋œ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    • ${segment}๋Š” ์•ž์„œ ์บก์ฒ˜ํ•œ ์ƒˆ๋กœ์šด ๊ฒฝ๋กœ ์„ธ๊ทธ๋จผํŠธ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    • ๋”ฐ๋ผ์„œ, ์ด ํ•„ํ„ฐ๋Š” /user-service/ ๋’ค์— ์˜ค๋Š” ๋ชจ๋“  ๊ฒƒ์„ ๋์— ์Šฌ๋ž˜์‹œ๊ฐ€ ์—†๋Š” ๊ฒฝ๋กœ๋กœ ์žฌ์ž‘์„ฑํ•œ๋‹ค.

๋‹ค์‹œ ๋งํ•ด,

RewritePath  ํ•„ํ„ฐ๋Š” ์š”์ฒญ์ด ๊ฒŒ์ดํŠธ์›จ์ด๋ฅผ ํ†ต๊ณผํ•  ๋•Œ /user-service/๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํ•ด๋‹น ๊ฒฝ๋กœ ๋’ค์— ์Šฌ๋ž˜์‹œ๊ฐ€ ์—†๋Š” ํ˜•ํƒœ๋กœ ์žฌ์ž‘์„ฑํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, /user- service/profile์€ /profile๋กœ ์žฌ์ž‘์„ฑ๋œ๋‹ค.

์ด๋Ÿฌํ•œ ํ•„ํ„ฐ๋Š” ๋ผ์šฐํ„ฐ๊ฐ€ ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค์— ์ „๋‹ฌ๋˜๊ธฐ ์ „์— ์š”์ฒญ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜๋‹ค.

 

apigateway-service์˜ application.yml ์ „์ฒด ์ฝ”๋“œ

# 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/**
          filters:
            - RewritePath=/user-service/(?<segment>.*),/$\{segment}
        - id: beverage-service
          uri: http://localhost:8082/
          predicates:
            - Path=/beverage-service/**
          filters:
            - RewritePath=/beverage-service/(?<segment>.*),/$\{segment}
        - id: community-service
          uri: http://localhost:8083/
          predicates:
            - Path=/community-service/**
          filters:
            - RewritePath=/community-service/(?<segment>.*),/$\{segment}

 

user-service์˜ UserController.java

// user-service: UserController.java (8081 ํฌํŠธ ์‚ฌ์šฉ)

package com.caffeinedoctor.userservice.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @GetMapping("/")
    public String getServiceName(){
        return "user-service";
    }
}

 

๊ฐ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์„œ๋ฒ„ ์‹คํ–‰

http://localhost:8081/

user-service

http://localhost:8082/

beverage-service

http://localhost:8083/

community-service

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

http://localhost:8000/user-service/

apigateway-service

 

http://localhost:8000/beverage-service/

apigateway-service

http://localhost:8000/community-service/

apigateway-service

 

 

๐Ÿ”Š ์ •๋ฆฌํ•˜์ž๋ฉด,

์ฒซ ๋ฒˆ์งธ "7. Spring Cloud Gateway๋ฅผ ํ™œ์šฉํ•œ ๊ฒฝ๋กœ ์ˆ˜์ •" ๋ฐฉ๋ฒ•์€ ์š”์ฒญ๋œ ๊ฒฝ๋กœ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•˜๊ณ ,

๋‘ ๋ฒˆ์งธ "8. Spring Cloud Gateway์— ํ•„ํ„ฐ ์ ์šฉํ•˜๊ธฐ" ๋ฐฉ๋ฒ•์€ ์š”์ฒญ๋œ ๊ฒฝ๋กœ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•œ๋‹ค.


 

์ฐธ๊ณ ์ž๋ฃŒ

https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/

728x90
๋ฐ˜์‘ํ˜•