Declarative REST Client - Feign

Feign helps us create REST clients for REST services with minimum configuration and code. All you need to define is a simple interface and use proper annotations.

RestTemplate is typically used to make REST service calls. Feign helps us write REST clients without the need for RestTemplate and the logic around it.

Feign integrates well with Ribbon (client-side load balancing) and Eureka (Name server). We will look at this integration later in the chapter.

To use Feign, let's add the Feign starter to the pom.xml file of service consumer microservice:

    <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

We need to add dependencyManagement for Spring Cloud to the pom.xml file as this is the first Cloud dependency that service consumer microservice is using:

    <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

The next step is to add the annotation in order to enable scanning for Feign clients to ServiceConsumerApplication. The following snippet shows the usage of the @EnableFeignClients annotation:

    @EnableFeignClients("com.mastering.spring.consumer")
public class ServiceConsumerApplication {

We need to define a simple interface to create a Feign client for a random service. The following snippet shows the details:

    @FeignClient(name ="microservice-a", url="localhost:8080")
public interface RandomServiceProxy {
@RequestMapping(value = "/random", method = RequestMethod.GET)
public List<Integer> getRandomNumbers();
}

Some important things to note are as follows:

  • @FeignClient(name ="microservice-a", url="localhost:8080"): The FeignClient annotation is used to declare that a REST client with the given interface needs to be created. We are hardcoding the URL of Microservice A for now. Later, we will look at how we can connect this to a Name server and eliminate the need for hardcoding.
  • @RequestMapping(value = "/random", method = RequestMethod.GET): This specific GET service method is exposed at the URI /random.
  • public List<Integer> getRandomNumbers(): This defines the interface of the service method.

Let's update NumberAdderController to use RandomServiceProxy in order to call the service. The following snippet shows the important details:

    @RestController
public class NumberAdderController {
@Autowired
private RandomServiceProxy randomServiceProxy;
@RequestMapping("/add")
public Long add() {
long sum = 0;
List<Integer> numbers = randomServiceProxy.getRandomNumbers();
for (int number : numbers) {
sum += number;
}
return sum;
}
}

A couple of important things to note are as follows:

  • @Autowired private RandomServiceProxy randomServiceProxy: RandomServiceProxy is autowired in.
  • List<Integer> numbers = randomServiceProxy.getRandomNumbers(): Look at how simple it is to use the Feign client. No playing around with RestTemplate anymore.

When we invoke the add service in service consumer microservice at http://localhost:8100/add, you will get the following response:

    2103

GZIP compression can be enabled on Feign requests by configuring it, as shown in the following snippet:

    feign.compression.request.enabled=true
feign.compression.response.enabled=true
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset