Rad
Rad

Reputation: 5002

How to Convert Blocking Feign Clients to Reactive Feign Clients

Introduction
1. We have quite a bunch of Spring microservices, some totally reactive (spring-webflux) and some old style (spring-web).
2. We use Feign to define API (and the client) in our microservices as interface and implement them in our controllers.
3. Each microservice might have dependencies on both types.

Objective
To prevent code duplication, generate Reactive Feign clients based on normal Feign clients (and vice versa).

Questions
I already developed a (PoC) Maven plugin which reads Feign interfaces and generates Reactive ones with the same signature but a reactive return type.
1. Is this a stupid idea? If so, what should I do to have both technology supported in our clients without code duplication?
2. Is there any tools/solutions in the market I can use? (Or should I continue with my maven plugin?)

Upvotes: 3

Views: 5222

Answers (1)

Kevin Davis
Kevin Davis

Reputation: 1293

Feign includes simple Reactive streams support that allows for the use of Reactive return types. This library wraps the method execution in a Reactive wrapper. It is not "reactive all the way down", the method execution is still blocking, but it can be integrated into an existing Reactive execution chain.

To use the provided library, include it as a dependency:

<dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-reactive-wrappers</artifactId>
   <version>10.4.0</version>
</dependency>

And use the appropriate builder for the reactive streams implementation you want, Reactor and RxJava2+ are supported.

public class ExampleReactor {
  public static void main(String args[]) {
    GitHubReactor gitHub = ReactorFeign.builder()      
      .target(GitHubReactor.class, "https://api.github.com");

    List<Contributor> contributors = gitHub.contributors("OpenFeign", "feign")
      .collect(Collectors.toList())
      .block();
  }
}

You can use this library to get you started. Our roadmap includes full reactive support in the near future.

Upvotes: 2

Related Questions