RestTemplate
RestTemplate restTemplate = new RestTemplate();
restTemplate.getForObject("http://localhost:8080", User.class);
FeignClient
@FeignClient("stores')
public interface StoreClient {
@RequestMapping(method = RequestMethod.GET, value = "/stores")
List<Store> getStores();
}
Zuul은 Netflix의 애플리케이션 게이트웨이로, 동적 라우팅, 모니터링, 내구성 및 보안 기능을 제공합니다. 이 프로젝트는 L7 애플리케이션 게이트웨이로서 다음과 같은 특징을 가지고 있습니다:
Netflix Ribbon은 2.0 버전 이전에 동기적으로 작동했으나 그 한계로 인하여 2 버전 이후에는 비동기적으로 작동될 수 있도록 변경되었습니다.
하지만, 여전히 다른 라이브러리와의 호환성 문제로 인하여 결국 Zuul과 함께 Deprecated 되게 되었습니다.
현재는 Ribbon을 대체하기 위해서 Spring Cloud Gateway
가 대신해서 사용되고 있습니다.
Spring Cloud Gateway는 스프링 생태계에서 제공하는 API 게이트웨이 솔루션입니다. API 게이트웨이는 클라이언트 요청을 받아 적절한 서비스로 라우팅하고, 로드 밸런싱, 인증, 필터링, 모니터링 등의 기능을 제공합니다. Spring Cloud Gateway는 네티(reactor-netty) 기반으로 만들어졌으며, 비동기식 논블로킹 방식으로 높은 성능을 제공합니다.
실습을 위하여 2개의 Service Project와 하나의 Gateway Project를 생성
Dependency 추가
Web → Spring Web
Spring Cloud Discovery → Eureka Discovery Client
Controller 생성
@RestController
@RequestMapping("/first-service")
public class FirstServiceController {
@GetMapping("/welcome")
public String welcome() {
return "Welcome to the First Service";
}
}
application.yml 수정
server:
port: 8081 # Service2 -> 8082
spring:
application:
name: first-service
eureka:
client:
fetch-registry: false
register-with-eureka: false
fetch-registry
, register-with-eureka
옵션을 false
로 설정두번째 Service도 First
→ Second
로 변경하여 동일하게 생성
Dependency 추가
Spring Cloud Discovery → Eureka Discovery Client
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
Spring Cloud Routing → Gateway
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
application.yml 수정
server:
port: 8000
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: api-gateway-service
cloud:
gateway:
routes:
- id: first-service
uri: http://localhost:8081/
predicates:
- Path=/first-service/**
- id: second-service
uri: http://localhost:8082/
predicates:
- Path=/second-service/**
cloud.gateway.mvc.routes
에 기존에 생성한 Service Project를 등록id
에는 Service Project의 Name을 등록uri
는 Service Project의 Base-uri를 등록predicates
에는 Routing
을 위한 Path
를 등록해 준다.8000
포트를 사용하는 Gateway Project가 Service Project로 Routing