[MSA] API Gateway 구축하기 - Spring Boot + Spring Cloud Gateway
API Gateway를 구현하기 전에 Service Discovery Server가 필요한데
Netflix OSS Eureka를 사용한 구현하는 방법을 소개한 글을 읽고오면 된다.
[MSA] Spring Boot로 Service Discovery 구축하기 - Netflix OSS Eureka Server
이전 글에서 이야기했듯이 MSA에서는 기본적으로 여러 서비스를 등록하고 등록한 서비스의 검색을 수행하는 서비스 디스커버리 서버(Service Discovery Server)가 필요하다. 디스커버리 서버 Client-Side
diary-developer.tistory.com
바로 시작해보자!
1. API Gateway, Service 프로젝트 생성
1-1. Sping Cloud Gateway 프로젝트 생성
SpringCloudApiGateway 프로젝트 생성하고
dependency 선택창에서 gateway를 검색해서
Gateway를 추가하고 Eureka Discovery Client도 추가한다.


1-2. Service 프로젝트 생성
SpringCloudService 프로젝트를 생성하고
dependency 선택창에서 eureka를 검색해서
Eureka Discovery Client를 추가하고 Spring Web도 추가한다.
(이전 글에서 만든 EurekaService를 그대로 사용해도 무방하다.)


1-3. application.yml 설정
API Gateway 프로젝트와 Service 프로젝트 총 2개의 프로젝트를 생성했다.
각 프로젝트 src/main/resources 폴더 하위에
application.properties 파일명을 application.yml로 변경하고
각 프로젝트의 해당 파일을 열어서 아래 내용을 추가한다.
● SpringCloudApiGateway
server: port: 9000 spring: application: name: spring-cloud-gateway cloud: gateway: routes: - id: spring-cloud-service uri: lb://SPRING-CLOUD-SERVICE predicates: - Path=/spring-cloud-service/** eureka: instance: prefer-ip-address: true client: register-with-eureka: true fetch-registry: true service-url: defalutZone: http://localhost:8761/eureka
● SpringCloudService
server: port: 8100 servlet: context-path: /spring-cloud-service spring: application: name: spring-cloud-service eureka: instance: prefer-ip-address: true client: registryFetchIntervalSeconds: 5 disable-delta: true register-with-eureka: true fetch-registry: true service-url: defalutZone: http://localhost:8761/eureka
1-4. Eureka Client 어노테이션 추가
아래의 코드와 같이 API Gateway와 Service 각각 @EnableDiscoveryClient 어노테이션을 추가한다.
● SpringCloudApiGatewayApplication
package com.gateway.exam; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class SpringCloudApiGatewayApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudApiGatewayApplication.class, args); } }
● SpringCloudServiceApplication
package com.service.exam; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class SpringCloudServiceApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudServiceApplication.class, args); } }
1-5. Service API 추가
SpringCloudService에 문자열을 반환하는 API 하나를 추가한다.
package com.service.exam.api.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SpringCloudServiceController { @GetMapping("/service") public String springCloudService() { return "spring-cloud-service 호출!"; } }
2. API Gateway, Service 서버 실행하기
2-1. Service Discovery, API Gateway, Service 서버 실행
ServiceDiscovery, SpringCloudApiGateway, SpringCloudService 서버를 실행하고
http://localhost:8761로 접속하면 API Gateway와 Service가 잘 등록되어 있는 것을 확인할 수 있다.

2-2. API Gateway 호출
postman을 사용해서 localhost:9000/spring-cloud-service/service 호출하면
"spring-cloud-service 호출!" 문구가 잘 노출되는 것을 확인할 수 있다.

정말 간단한 설정부터 서버 실행 및 호출까지 구현을 해봤는데
세부적으로 들어가면 더 많고 복잡한 설정을 할 수 있다.
'개발 이야기 > MSA' 카테고리의 다른 글
[MSA] 모니터링 (Monitoring, Logging, Tracing) (0) | 2022.12.12 |
---|---|
[MSA] Service Discovery 구축하기 - Spring Boot + Netflix OSS Eureka Server (0) | 2022.12.02 |
[MSA] Config Server 이해하기 - Spring Cloud Config (4) (0) | 2022.11.22 |
[MSA] Service Discovery Server 이해하기 (3) (0) | 2022.11.22 |
[MSA] API Gateway 이해하기 - Spring Cloud Gateway (2) (0) | 2022.11.21 |
댓글
이 글 공유하기
다른 글
-
[MSA] 모니터링 (Monitoring, Logging, Tracing)
[MSA] 모니터링 (Monitoring, Logging, Tracing)
2022.12.12 -
[MSA] Service Discovery 구축하기 - Spring Boot + Netflix OSS Eureka Server
[MSA] Service Discovery 구축하기 - Spring Boot + Netflix OSS Eureka Server
2022.12.02 -
[MSA] Config Server 이해하기 - Spring Cloud Config (4)
[MSA] Config Server 이해하기 - Spring Cloud Config (4)
2022.11.22 -
[MSA] Service Discovery Server 이해하기 (3)
[MSA] Service Discovery Server 이해하기 (3)
2022.11.22
댓글을 사용할 수 없습니다.