Http Call

Open Feign

Introduction

  • The Feign client is a declarative REST client that makes writing web clients easier. When using Feign, the developer has only to define the interfaces and annotate them accordingly. The actual web client implementation is then provided by Spring at runtime.

  • For each request, the assigned thread blocks until it receives the response. The disadvantage of keeping multiple threads alive is that each open thread occupies memory and CPU cycles.

Implementation

  • Add the dependency, Example: Maven

pom.xml
<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  • Setting up environment variable

application.yaml
chat:
  api:
    url: http://localhost:8000    
    key: your-super-secret-api-key
  • Setting up Http header configuration and error handling

  • Define the class of output and error class of http call response

  • Define the client(interface) of HTTP call

Web Client

Introduction

  • The WebClient is part of the Spring WebFluxarrow-up-right library. It’s a non-blocking solution provided by the Spring Reactive Framework

  • WebClient executes the HTTP request and adds a “waiting for response” task into a queue. Later, the “waiting for response” task is executed from the queue after the response is received, finally delivering the response to the subscriber function.

Implementation

  • Add the dependencies

  • Setting up web client with http call detail

  • Define the class of streaming response and controller level for response type

Comparison

  • Open Feign will be more developer-friendly, easy to implement through interface layer. Most are likely suitable for CRUD operation

  • For handling streaming, Feign’s design is request → response → close connection, streaming is not supported out of the box. In that case, Web Client will be better.

  • Although the Feign client is a great choice in many cases and the resulting code has lower cognitive complexity, the non-blocking style of WebClient uses much fewer system resources during high-traffic spikes

Last updated