์ด๋ฒ ํฌ์คํ ์์๋ Spring Cloud๋ฅผ ์ด์ฉํด MSA ๋ฐฉ์์ ์๋ฒ๋ฅผ ๊ตฌ์ถํ ๋, ๊ฒ์ดํธ์จ์ด์์ ์ฌ์ฉ์๊ฐ ์ง์ ํ ๋น์จ๋ก ํน์ ์๋น์ค๋ฅผ ๋ผ์ฐํ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ ๋ฆฌํ๋ ค๊ณ ํฉ๋๋ค.
๊ฐ์ค์น ๋ผ์ฐํ (Weighted Routing)์ด๋?
MSA์์ ๊ฐ์ค์น ๋ผ์ฐํ ์ ํธ๋ํฝ์ ์ฌ๋ฌ ๋ง์ดํฌ๋ก์๋น์ค ์ธ์คํด์ค์ ์ง์ ํ ๊ฐ์ค์น์ ๋ฐ๋ผ ๋ถ์ฐ์ํค๋ ๊ธฐ๋ฒ์ ๋๋ค. ์นด๋๋ฆฌ ๋ฐฐํฌ, ๋ธ๋ฃจ-๊ทธ๋ฆฐ ๋ฐฐํฌ์ ๊ฐ์ด ์๋ฒ์ ๋ฒ์ ์ ๋๋์ด์ผ ํ ์ํฉ์์๋ ์ ์ฉ๋ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด, ์๋น์ค A์ 70% ์๋น์ค B์ 30%์ ํธ๋ํฝ์ ๋ณด๋ด๋๋ก ์ค์ ํ ์ ์์ต๋๋ค.
Spring Cloud Gateway
Spring Cloud Gateway๋ Spring Framework ๊ธฐ๋ฐ์ API Gateway ์๋ฃจ์ ์ ๋๋ค. ์ฃผ์ ๊ธฐ๋ฅ์ผ๋ก ๋ผ์ฐํ , ํํฐ, ๋ก๋ ๋ฐธ๋ฐ์ฑ, ๋ณด์, ๋ชจ๋ํฐ๋ง ๋ฐ ๋ก๊น ์ด ์์ต๋๋ค.
์ด๋ฒ์ ๋ค๋ค๋ณผ ๊ธฐ๋ฅ์ ๋ผ์ฐํ ์ด๋ฉฐ, ๊ทธ ์ค์์๋ Weight๋ฅผ ์ ์ฉํ์ฌ ํธ๋ํฝ์ ๋ถ์ฐํ๋ ๊ฐ์ค์น ๋ผ์ฐํ ์ ์ ์ฉํด๋ณด๊ฒ ์ต๋๋ค.
์๋ฒ ์ค์
์ธํ ๋ฆฌ์ ์ด์์ [Edit Configurations]์ ๋ค์ด๊ฐ๋๋ค.
Product Service Instance๋ฅผ ํฌํธ๋ฅผ ๋ค๋ฅด๊ฒ ์ด์ด์ ๋๊ฐ ์คํํ๋ ค๊ณ ํฉ๋๋ค. [Modify options] ์ ๋ค์ด๊ฐ [Add VM options] ์ฒดํฌํด์ค๋๋ค.
java ๋ฒ์ ๊ณผ ํจํค์ง๋ช ์ฌ์ด์ ์นธ์ด ์๊ฒผ์ ํ ๋ฐ ์ ๊ธฐ์ [-Dserver.port={ํฌํธ๋ฒํธ}] ๋ฅผ ์ ์ด์ค๋๋ค. ๊ทธ๋ฆฌ๊ณ ํท๊ฐ๋ฆฌ์ง ์๊ฒ Application Name์ ํฌํธ ๋ฒํธ๋ฅผ ์ถ๊ฐํด์ค์๋ค.
application.yml (gateway service)
server:
port: 19091
spring:
main:
web-application-type: reactive
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: order
uri: lb://order
predicates:
- Path=/orders/**
- id: product-70 # ๋ผ์ฐํธ์ ๊ณ ์ ID
uri: http://localhost:19093 # ์์ฒญ์ด ์ ๋ฌ๋ ๋์ URI
predicates: # ์์ฒญ์ด ์ด ๋ผ์ฐํธ์ ๋งคํ๋ ์กฐ๊ฑด
- Path=/products/** # ์์ฒญ ๊ฒฝ๋ก๊ฐ /products/** ์ธ ๊ฒฝ์ฐ
- Weight=product-group, 7 # ๊ฐ์ค์น 70%
- id: product-30
uri: http://localhost:19094
predicates:
- Path=/products/**
- Weight=product-group, 3 # ๊ฐ์ค์น 30%
- id: auth
uri: lb://auth
predicates:
- Path=/auth/**
service:
jwt:
secret-key: ""
eureka:
client:
service-url:
defaultZone: http://localhost:19090/eureka/
๋น๋๊ธฐ๋ก ํจ์จ์ ์ธ ์๋ฒ ์คํ์ ์ํด [spring.main.web-application-type: reactive] ์ผ๋ก ์ค์ ํด์ค๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ๊ฐ ์๋น์ค์ ๋ง๊ฒ ๋ผ์ฐํ ์ค์ ์ ํด์ค๋๋ค.
product๋ ๊ฐ์ค์น ๋ผ์ฐํ ์ ์ํด predicates์ weight๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์ด๋ ๊ฒ ์ค์ ํ๋ฉด /products/** ๋ก ๋ค์ด์ค๋ ์์ฒญ์ product-group์ ์ํด 70%๋ 19093ํฌํธ, 30%๋ 19094 ํฌํธ๋ก ๋ถ์ฐ๋ฉ๋๋ค.
ProductController
@RequiredArgsConstructor
@RestController
@RequestMapping("/products")
public class ProductController {
@Value("${server.port}")
private String port;
@GetMapping("/health-check")
public String getHealthCheck() {
return "Server port: " + port;
}
}
๋ช๋ฒ ํฌํธ๋ก ์์ฒญ์ด ์ ์ก๋๊ณ ์๋ ์ง ํ์ธํ๊ธฐ ์ํ ํฌํธ ์กฐํ ๋ฉ์๋๋ฅผ ์ถ๊ฐํฉ๋๋ค.
Test
10๋ฒ ์์ฒญ ๋ณด๋์ ๋ 19093์ด 6๋ฒ 19094๊ฐ 4๋ฒ ์๋ต์ผ๋ก ์์ต๋๋ค.
์ด ํ ๊ณ์ ์์ฒญ์ ๋ณด๋์ ๋ ์ผ์ถ 7:3 ๋น์จ๋ก ๋ผ์ฐํ ๋๋ ๋ชจ์ต์ ํ์ธํ ์ ์์์ต๋๋ค.
์ด๋ ๊ฒ ํด์ Spring Cloud Gateway์์ ์ํ๋ ๋น์จ๋ก ์์ฒญ์ด ์ ๋ฌ๋๊ฒ ๊ตฌ์ฑํด๋ณด์์ต๋๋ค.
์ถ์ฒ
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
'๐ Web > Back-end' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Servlet] Forward์ Redirect์ ์ฐจ์ด์ (feat. ๋ณด์) (0) | 2023.10.10 |
---|---|
[MySQL] Mac M1 Workbench ์ค์น ๋ฐ ์ด๊ธฐ ์ค์ (0) | 2023.09.17 |