OpenFeign is an open-source project initially developed by Netflix and now maintained by the open-source community. It simplifies the process of writing web services by providing a declarative approach to REST API consumption using the FeignClient
interface.
- Declarative REST Client: Write cleaner and more maintainable code by declaring interfaces.
- Integration Friendly: Ideal for consuming third-party APIs and microservice endpoints.
- Dynamic Implementation: Automatically creates dynamic implementations for declared
FeignClient
interfaces.
To use OpenFeign in your project, add the following dependency to your pom.xml
file:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
This project demonstrates the use of OpenFeign in a microservice architecture with the following components:
- Employee Service
- Manages employee-related data and operations.
- Address Service
- Handles address-related information for employees.
- Java Development Kit (JDK 8 or above)
- Maven
- Spring Boot
- Clone this repository.
- Navigate to each service directory and build the project using:
mvn clean install
- Run each service using:
mvn spring-boot:run
- Test the endpoints using tools like Postman or cURL.
- Declare a
@FeignClient
interface with the desired REST API endpoints. - OpenFeign dynamically generates the implementation at runtime.
- Use the client as a Spring Bean in your services to make API calls effortlessly.
@FeignClient(name = "address-service", url = "http://localhost:8081")
public interface AddressClient {
@GetMapping("/addresses/{id}")
Address getAddressById(@PathVariable("id") Long id);
}
- Reduces boilerplate code for REST API calls.
- Simplifies integration between microservices.
This project is licensed under the MIT License. See the LICENSE file for details.
Developed with ❤️ for simplifying microservice communication.