Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Manual Creation of Feign Clients in Spring Cloud

DZone's Guide to

Manual Creation of Feign Clients in Spring Cloud

Learn about the process behind manually building Feign clients to operate in the Spring Cloud framework.

· Cloud Zone
Free Resource

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

If you want something done right, you have to do it yourself

Have you ever heard the saying above? Sometimes it is true even when writing code.

While Spring Cloud makes it extremely easy to create Feign Clients with its use of annotations, sometimes you just have to bite the bullet and create them yourselves. I would say the 90% of the time, the combination of annotations and custom configuration will be good enough for most use cases. Sometimes, however, you run into use cases where that just won’t do. In these cases it would be good to have full control over how you create your Feign clients.

Recently I added some documentation to the Spring Cloud docs that demonstrates exactly how you would do this. Essentially you use the same APIs as you would if you were just using vanilla Feign. Below is the example taken from the Spring Cloud docs.

@Import(FeignClientsConfiguration.class) 
class FooController { 
  private FooClient fooClient; 
  private FooClient adminClient; 

  @Autowired public FooController( ResponseEntityDecoder decoder, SpringEncoder encoder, Client client) { 
    this.fooClient = Feign.builder().client(client) 
      .encoder(encoder) 
      .decoder(decoder) 
      .requestInterceptor(new BasicAuthRequestInterceptor("user", "user")) 
      .target(FooClient.class, "http://PROD-SVC"); 

    this.adminClient = Feign.builder().client(client) 
      .encoder(encoder) 
      .decoder(decoder) 
      .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin")) 
      .target(FooClient.class, "http://PROD-SVC"); 
  } 
} 

In this example, we are creating two Feign Clients from the same interface, FooClient.class, which are using two different request interceptors. Doing this with the @FeignClient annotation plus configuration would not be possible, so in this case, we need to create the clients using the Feign APIs.

For a more in-depth look at the code, check out this GitHub repo.

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

Topics:
spring cloud ,java ,spring boot

Published at DZone with permission of Ryan Baxter, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}