Real code example: Spring boot with OpenFeign client, Hystrix and Spring Service Locator pattern — Part 2

The first part is here

In this part, we are gonna go through OpenFeign client.

OpenFeign Client

I have referred to these articles as below:

OpenFeign Github repository:

Long story short, OpenFeign is a open source project, based on Spring Cloud Netflix Feign. Ultimately, Netflix decided to stop using Feign internally and ceased its development. As a result of this decision, Netflix fully transferred Feign to the open-source community under a new project named OpenFeign.

A declarative REST client for Spring Boot apps. Feign makes writing web service clients easier with pluggable annotation support, which includes Feign annotations and JAX-RS annotations.

In this demo, I only use a simple interface to build a simple client to connect to JSON PLACEHOLDER fake API server

Feign client interface

We only need to define an interface which is annotated with annotation @FeignClient.

@FeignClient(name = "postClient", url = "", configuration = PostClientConfig.class)
public interface PostClient {
@GetMapping(value = "/posts", produces = MediaType.APPLICATION_JSON_VALUE)
public List<PostModel> listAllPosts();
@PostMapping(value = "posts", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public PostModel createPost(PostModel postRequest);

The name argument passed in the @FeignClient annotation is a mandatory, arbitrary client name, while with the url argument, we specify the API base URL.

Furthermore, since this interface is a Feign client, we can use the Spring Web annotations to declare the APIs that we want to reach out to.

Enable Feign client interface scanning

Next, we need to enable component scanning for interfaces that declare they are Feign clients.

public class SpringBootOpenfeignApplication {
public static void main(String[] args) {, args);

We can either declare @EnableFeignClients in Spring application main class or any configuration classes which are annotated with @Configuration.

Now, we are good to go for using this client class. In order to use, we only need to inject the Feign client interface in any Spring component classes.

public clas
s PostAsynchronousService implements AsynchronousService<PostModel, PostModel> {
private PostClient postClient;

In example code above, I use Lombok to automatically generate constructor with one argument as PostClient type.

- @AllArgsConstructor: to generate constructor with argument(s) for all class's properties

Since we only have one class property as PostClient type, so the auto generated constructor will only have one argument.

After that, since we only have one constructor, so @Autowired annotation can be omitted, Spring will automatically initialize a PostClient bean (which was generated by OpenFeign library during startup application), then inject into the PostAsynchronousService class constructor.

This constructor injection will make sure we will have a PostClient bean ready before initializing PostAsynchronousService bean.

OpenFeign logging

Each Feign client will have its independent logger to application’s logger and the default logging level is NONE.

To enable logging level, we need to override logging level by configuration:

public class PostClientConfig {
feignLoggerLevel() {
return Logger.Level.FULL;

The Logger.Level object that you may configure per client, tells Feign how much to log. Choices are:

- NONE, No logging (DEFAULT).
- BASIC, Log only the request method and URL and the response status code and execution time.
- HEADERS, Log the basic information along with request and response headers.
- FULL, Log the headers, body, and metadata for both requests and responses.

NOTE: Feign client logging levels are only worked when our application log level is DEBUG or the Feign interface package at least.

To run the demo, we can use the Unit Test class TestPostService.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store