Software Architecture/MSA

[MSA ๊ตฌ์ถ•ํ•˜๊ธฐ] 9. API Gateway์™€ Eureka ์„œ๋ฒ„ ์—ฐ๋™ํ•˜๊ธฐ (API Gateway)

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

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

๐Ÿ”Š ์ด๋ฒˆ์—๋Š”, API Gateway๋ฅผ  Eureka ํด๋ผ์ด์–ธํŠธ๋กœ ๋“ฑ๋กํ•˜์—ฌ API Gateway์™€ Eureka ์„œ๋ฒ„๋ฅผ ์—ฐ๋™ํ•  ๊ฒƒ์ด๋‹ค.

 

API Gateway์™€ Eureka ์„œ๋ฒ„ ์—ฐ๋™ ํ”„๋กœ์„ธ์Šค

ํด๋ผ์ด์–ธํŠธ๊ฐ€ API Gateway์— ์š”์ฒญ์„ ํ•˜๋ฉด  API Gateway ๊ฐ€ Eureka server์— ๋“ฑ๋ก๋œ ์„œ๋น„์Šค๋“ค์„ ์กฐํšŒํ•˜๊ณ  ํ•ด๋‹น ์„œ๋น„์Šค๋กœ ๋ผ์šฐํŒ…์„ ํ•ด์ค€๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, ๋กœ๊ทธ์ธ์„ ํ•œ๋‹ค๋Š” ๊ฐ€์ƒ์˜ ์‹œ๋‚˜๋ฆฌ์˜ค์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž.

  • โ‘ : ๊ฐ ์ธ์Šคํ„ด์Šค๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์œ ๋ ˆ์นด ์„œ๋ฒ„์— ๋“ฑ๋กํ•œ๋‹ค.
    • ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋Š” ์‹œ์ž‘ํ•  ๋•Œ ์œ ๋ ˆ์นด ์„œ๋ฒ„์— IP, ํ˜ธ์ŠคํŠธ ์ฃผ์†Œ, ํฌํŠธ ์ •๋ณด ๋“ฑ์„ ์ „๋‹ฌํ•œ๋‹ค.
    • ์œ ๋ ˆ์นด ์„œ๋ฒ„๋Š” ๋“ฑ๋ก๋œ ์ธ์Šคํ„ด์Šค๋“ค์„ ์ฃผ๊ธฐ์ ์œผ๋กœ ์ƒํƒœ๋ฅผ ์ฒดํฌํ•˜๋ฉด์„œ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.
  • โ‘ก: ํด๋ผ์ด์–ธํŠธ์—์„œ ๋กœ๊ทธ์ธ ์š”์ฒญ์ด API ๊ฒŒ์ดํŠธ๋กœ ์˜จ๋‹ค.
  • โ‘ขโ‘ฃ: API ๊ฒŒ์ดํŠธ์›จ์ด์—์„œ ๋ฐ›์€ ๋กœ๊ทธ์ธ ์š”์ฒญ์„ ์–ด๋–ค ์ธ์Šคํ„ด์Šค์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„์ง€ ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ์—์„œ ์ฐพ๋Š”๋‹ค.
    • ๋งค๋ฒˆ ์งˆ์˜ํ•˜์ง€ ์•Š๊ณ  ์ธ์Šคํ„ด์Šค ์ •๋ณด๋ฅผ ์บ์‹ฑํ•œ๋‹ค.
  • โ‘ค: API ๊ฒŒ์ดํŠธ์›จ์ด๋Š” ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ ์š”์ฒญ์„ Auth Service์— ํ•œ๋‹ค.
  • โ‘ฅ: Auth Service์˜ ์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•œ๋‹ค.

 

 

๊ฒŒ์ดํŠธ์›จ์ด์™€ Eureka ์„œ๋ฒ„ ์—ฐ๋™

Eureka ์„œ๋ฒ„๋Š” ๊ฐ๊ฐ์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ชฉ๋ก๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

MSA๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์š”์†Œ๋Š” ์ž๋™์œผ๋กœ ์˜คํ†  ์Šค์ผ€์ผ๋ง๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ ์ƒ๊ธด ์„œ๋ฒ„์˜ IP๋ฅผ ๊ฒŒ์ดํŠธ์›จ์ด๊ฐ€ ์•Œ์ง€ ๋ชปํ•œ๋‹ค.

๋”ฐ๋ผ์„œ Eureka ์„œ๋ฒ„๊ฐ€ ํ•ด๋‹น ๋ชฉ๋ก๋“ค์„ ๊ด€๋ฆฌํ•˜๋ฉฐ Gateway์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

์Šคํ”„๋ง ํด๋ผ์šฐ๋“œ ๊ฒŒ์ดํŠธ์›จ์ด Eureka ํด๋ผ์ด์–ธํŠธ ์„ค์ •

1. Eureka ํด๋ผ์ด์–ธํŠธ ์„ค์ •์„ ์œ„ํ•œ ์˜์กด์„ฑ ์ถ”๊ฐ€

(1) ๊ธฐ์กด apigateway-service์˜ build.gradle์— ์˜์กด์„ฑ ์ถ”๊ฐ€ํ•˜๊ธฐ

// build.gradle

ext {
  set('springCloudVersion', "2023.0.0")
}

dependencies {
  implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}

dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}

 

(2) ์ฒ˜์Œ ๋ถ€ํ„ฐ Spring Initializr์—์„œ apigateway-service๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์˜์กด์„ฑ ๊ฐ™์ด ๋“ฑ๋กํ•˜๊ธฐ

 

3. Eureka ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ

apigateway-service์˜ application.yml ํŒŒ์ผ์—์„œ ๋ณ€์ˆ˜ ์„ค์ •์„ ํ†ตํ•ด Eureka ์„œ๋ฒ„์— ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค.

# application.yml

# eureka ํด๋ผ์ด์–ธํŠธ ์„ค์ • (eureka ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ)
eureka:
  client:
    # eureka ์„œ๋ฒ„์— ๋“ฑ๋กํ• ์ง€ ์—ฌ๋ถ€
    register-with-eureka: true
    # eureka ์„œ๋ฒ„๋กœ ๋ถ€ํ„ฐ ์ธ์Šคํ„ด์Šค๋“ค์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ธ์ง€ ์„ค์ •ํ•˜๋Š” ์†์„ฑ
    fetch-registry: true
    # eureka ์„œ๋ฒ„ ์ฃผ์†Œ์— ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์ •๋ณด ๋“ฑ๋ก
    service-url:
      defaultZone: http://admin:1234mst@localhost:8761/eureka

 

apigateway-service์˜ application.yml ํŒŒ์ผ ์ „์ฒด

# application.yml

server:
  port: 8000

# eureka ํด๋ผ์ด์–ธํŠธ ์„ค์ • (eureka ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ)
eureka:
  client:
    # eureka ์„œ๋ฒ„์— ๋“ฑ๋กํ• ์ง€ ์—ฌ๋ถ€
    register-with-eureka: true
    # eureka ์„œ๋ฒ„๋กœ ๋ถ€ํ„ฐ ์ธ์Šคํ„ด์Šค๋“ค์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ธ์ง€ ์„ค์ •ํ•˜๋Š” ์†์„ฑ
    fetch-registry: true
    # eureka ์„œ๋ฒ„ ์ฃผ์†Œ์— ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์ •๋ณด ๋“ฑ๋ก
    service-url:
      defaultZone: http://admin:1234mst@localhost:8761/eureka

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}

 


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


 

์ฐธ๊ณ ์ž๋ฃŒ

https://covenant.tistory.com/251

https://covenant.tistory.com/252

https://wonit.tistory.com/497

https://velog.io/@boo105/MSA-EUREKA-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0

728x90
๋ฐ˜์‘ํ˜•