Spring Cloud OpenFeign provides OpenFeign integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms.
Spring Cloud OpenFeign is a library provided by the Spring framework to invoke web services to a client.
Feign is a declarative web service client written in Java. It simplifies the process of calling RESTful web services by handling the details of communication and encoding/decoding request/response payloads. FeignClient is an interface that is automatically implemented by the Feign framework at runtime to provide a client with a specific service. It allows you to define the methods for calling different endpoints of a service, and it uses HTTP requests under the hood to interact with the service.
It also has pluggable annotation support including Feign annotations and JAX-RS annotations.
JAX-RS is Java API that helps to write web services. Developers mark the java class and their methods with JAX-RS annotations to define resources and the actions to be performed on those resources.
Some of the important JAX-RS annotations are
The main advantage of the Spring Cloud OpenFeign is that we don’t need to write any code for calling a web service. We just need to create an interface and annotate it with Feign annotation. The methods declared in this interface are annotated with JAX-RS annotations to call the web service.
We will see how to use Feign annotations in the next stages by building a simple application.
FeignClient and WebClient are both Java libraries for making HTTP requests to remote servers. However, they are designed for different use cases and have some key differences:
FeignClient is a library for creating declarative REST clients. It simplifies the process of writing Java code for making HTTP requests by automatically generating a lot of the boilerplate code for you. FeignClient uses annotations to define the API endpoints, and it can automatically marshal and unmarshal data to and from JSON. FeignClient also allows you to easily switch between different HTTP transport libraries, like OkHttp or Apache HTTP Client. It is built on top of the Retrofit library.
WebClient, on the other hand, is a non-blocking, reactive client for making HTTP requests. It is part of the Spring WebFlux framework, and it is designed to work with the reactive programming model. WebClient can handle high concurrency and is well-suited for streaming large amounts of data. Unlike FeignClient, WebClient does not have a declarative API, and you need to manually build the HTTP requests and handle the responses.
In summary, FeignClient is a good choice if you're working with Spring Cloud and want a simple, declarative API for making HTTP requests, while WebClient is more suitable for high-concurrency and reactive use cases.
While creating a spring boot project, the first step is to add the spring cloud version under the properties tag in the pom.xml file
Next, we have to add openfeign and spring cloud dependencies
Let's build a simple example to demonstrate how OpenFeign works.
In this example, we will build 2 simple microservices for an e-commerce application - order-processing-service and payment-service.
Now we will create a simple microservice for processing payment for the orders that passed from the order-processing-service i.e. a different microservice
Step 1: Create PaymentResponseVO class for returning the response object
Step 2: Create an interface for the constants
Step 3: Create the controller class
NOTE: This is a fairly simple payment service that only demonstrates the working of OpenFeign. The actual payment service will be much more complicated than this.
Now we will create the order processing service that will call the payment service using OpenFeign
Step 1: Create an interface with @FeignClient annotation. The @FeignClient annotation should have properties name and url
@FeignClient(name = “payment-client”, url = “${payment-service-url})”
Here getPaymentStatus method will fetch the payment status from the PaymentService using the API that has been exposed by the service in the Create payment-service section.
Step 2: Add @EnableFeignClients to our main class. @EnableFeignClients annotation enables component scanning for interfaces that declare that they are Feign clients.
Step 3: Create OrderProcessingService.java class that will contain our business logic
Here orderRepository and paymentClient bean have been injected using the constructor injection.
The OrderRepository is an interface that extends the JpaRepository interface for easy access to the MySQL database.
We have added the essential classes, now we need to complete the OrderProcessingService by creating all the remaining classes and interfaces.
Step 3: create all the other remaining classes and interfaces
OrderRepository interface
NOTE: Kindly add the required dependencies for Spring Data JPA and MySQL connector in your pom.xml as we will be using MySQL database for storing our orders table
OrderConstants interface
Order class which acts as an entity
All the model classes i.e. OrderResponseVO, PaymentRepsonseVO and OrderVO
I have used lombok annotations in all the above classes that eliminates the boilerplate code for default constructor, parameterized constructor, getters and setters.
Last but not least let uscreate the controller class that will be the starting point of our application
Now, the most exciting part let's test our mini application.
Run both microservices at different ports. You can set the port in application.properties file
Hit the API exposed by order-processing-service using postman
As you can see we are creating order in our database and we are getting a paymentReferenceNumber if the payment is successful from the payment-service.
Let's check MySQL workbench if the order has been created or not
The order has been successfully stored in our database.