Spring Cloud Gateway

2024. 10. 25. 16:17Spring Boot

우선 Spring Cloud Gateway는 Spinrg을 이용한 Api Gateway이다.

 

API Gateway란?

 

Client가 Service에 직접 요청을 보내는 것이 아니라 API GateWay에 요청을 보내고 API Gateway가 해당 요청을 지정된 Service에 보내고 답을 받아 Client에 보내주는 서비스이다.

즉 일종의 Proxy역할을 수행하는 서비스이다.

 

https://learn.microsoft.com/ko-kr/dotnet/architecture/microservices/architect-microservice-container-applications/direct-client-to-microservice-communication-versus-the-api-gateway-pattern

 

Client와 Service간의 직접 통신의 경우

 

 

https://learn.microsoft.com/ko-kr/dotnet/architecture/microservices/architect-microservice-container-applications/direct-client-to-microservice-communication-versus-the-api-gateway-pattern

 

API Gateway를 사용 할 경우

MSA같이 수 많은 작은 서비스로 이루어진 경우 엔드포인트를 한 번에 관리를 하고 또한 공통으로 수행하는 인증/인가, 로깅 등의 작업을 대신 처리해서 중복 개발을 줄일 수 있습니다.

 

 

간단한 구현 예시)

 

우선 API Gateway에 등록할 간단한 API를 준비합니다.

 

 

그후 새로운 Spring Boot 프로젝트에 spring cloud gateway를 추가합니다.

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

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

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

 

 

그 후에는 application.properties에 등록할 api의 정보를 입력합니다

server.port=8000

spring.cloud.gateway.routes[0].id = microService
spring.cloud.gateway.routes[0].uri=http://localhost:8081
spring.cloud.gateway.routes[0].predicates[0]=Path=/microService/**

 

이제 http://localhost:8000/microService/hello나 port에 들어가면

 

 

이렇게 localhost:8081에서 실행 중인 서버에 요청을 보내고 답을 받아올 수 있다.

'Spring Boot' 카테고리의 다른 글

Spring Cloud Eureka  (0) 2024.10.29
Spring Cloud Gateway Filter  (0) 2024.10.29
Redis Cache  (0) 2024.10.11
WebMVC와 WebFlux를 동시 사용 할 경우 WebSocket  (0) 2024.08.14
OSIV  (0) 2024.08.03