티스토리 뷰

OpenFeign

Netflix에서 개발된 Java 의 Http Client Binder. 선언적으로 HTTP 요청을 생성 해주는 라이브러리다. 
즉, 인터페이스에 어노테이션을 추가하는 정도의 코드를 통해 통신 로직을 작성할 수 있다.

 

적용하기

전제

Server

Setting

 

1. 의존성 추가

2.  @EnableFeignClients  어노테이션을 추가.

 

마이크로 서비스간 요청받을 API 작성

 

 

Client

Setting

1. 의존성 추가

  • Sprinb Boot Stater Web, Eureka-Client, Openfeign 의존성을 추가한다.

 

2.  @EnableFeignClients  어노테이션을 추가.

 

Controller

 

 

Feign 을 활용한 선언적 HTTP Client Binder

  1.  @FeignClient  를 통해 연결할 MICRO SERVICE의 Application Name을 지정해준다.
  2. Cotroller에서 클라이언트에서의 요청을 매핑하는 방식과 동일한 형태로 내 요청을 바인딩 해준다.
  3. 연결된 Micro Service에 바인딩 해준 요청을 전송해줄 수 있다.

 

결과

 

과정
  1. Order Micro Service 에  GET: /order/1  에 매핑되는 API를 요청한다.
  2. Order Service는 User name 확인을 위해 User MicroService에  GET: /user/2  를 호출한다.
  3. OpenFeign 라이브러리를 통해 런타임 이후 생성된 통신 로직을 통해 User Service로 부터 응답을 받는다..
  4. Order Service는 클라이언트에게 응답한다.

 

느낀점

최근 회사 에서 안드로이드 개발자분과 페어코딩을 진행하면서 Retrofit2 를 통해 API 통신을 하는 내용을 본 적이 있는데, 그 내용과 비슷해 보인다. 추가로 마치 마이크로 서비스가 분리된 것이 아닌 모놀리틱한 코드를 작성한 것 처럼 통신을 구성할 수 있다는 점이 굉장히 섬세하고 신경써서 만들었구나 라는 생각이 들게한다.

 

 

전체 코드

 

참조

 

GitHub - OpenFeign/feign: Feign makes writing java http clients easier

Feign makes writing java http clients easier. Contribute to OpenFeign/feign development by creating an account on GitHub.

github.com

 

Spring Cloud OpenFeign

We welcome contributions. You can read more on how to contribute to the project here.

spring.io

 

Introduction to Spring Cloud OpenFeign | Baeldung

Feign makes writing web service clients easier with pluggable annotation support, which includes Feign annotations and JAX-RS annotations.

www.baeldung.com

 

Spring Cloud - Synchronous Communication with Feign

Spring Cloud - Synchronous Communication with Feign Introduction In a distributed environment, services need to communicate with each other. The communication can either happen synchronously or asynchronously. In this section, we will look at how services

www.tutorialspoint.com

 

댓글