Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Cache HTTP Requests made with Netflix's Feign library in a Java Spring app

In a spring boot application using Netflix's Feign to make HTTP requests to a service, is there an easy way to cache and return these cached values automatically? The cache should be based on the parameters passed to the request (similar to memoizing a function for X minutes).

I'm looking for something like the @Cache annotation below:

@Component
@FeignClient(value = "facebook", url = "${auth.facebook.url}")
public interface FacebookClient {
    @Cache(600) // Something like this.
    @RequestMapping(method = RequestMethod.GET, value = "/debug_token?input_token={input_token}&access_token={access_token}")
    Map debugToken(@PathVariable("input_token") String inputToken, @PathVariable("access_token") String appToken);
}

Of course I could cache it myself using a decorator around the FacebookClient, I was wondering if there was a quicker/less code way.

like image 322
Alejandro Avatar asked Apr 30 '16 16:04

Alejandro


People also ask

How to manually create feign client in Spring Cloud Netflix?

It is also possible to manually create Feign client if there is a need for some particular configuration. Let us create such a client along with a next controller class: annotation is just a default configuration provided by Spring Cloud Netflix. In the constructor of the controller, we are creating Feing client using FeignBuilder API.

What is the Netflix feign client?

What is the Netflix Feign Client? Need for it? Feign is a java to http client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to http apis regardless of restfulness.

What is annotation in Spring Cloud Netflix?

annotation is just a default configuration provided by Spring Cloud Netflix. In the constructor of the controller, we are creating Feing client using FeignBuilder API. It is also possible to configure Feing clients using application properties. Feign object defines what annotations and values are valid on interfaces.

How to load balance a service with Netflix feign?

For Load balancing using Ribbon. Getting the Service instance and then the Base URL. Make use of the REST Template for consuming service. The previous code, there are chances of exceptions like NullPointer and is not optimal. We will see how the call is made much easier and cleaner using Netflix Feign.


2 Answers

Feign does not support caching. I would prefer the JCache (JSR-107) and maybe use it via the spring-boot-starter-cache described in the spring guide

JCache is an abstraction for the proprietary implementation of EhCache, Hazelcast, ... so it is possible to change the implementation with very less impact on the application. At first I would prefer EhCache 3.

like image 200
Tobsch Avatar answered Sep 18 '22 14:09

Tobsch


Springs @Cacheable does what you need.

Check: Caching Data with Spring

like image 26
Stefan Isele - prefabware.com Avatar answered Sep 21 '22 14:09

Stefan Isele - prefabware.com