Feign as a declarative REST client

In the Booking microservice, there is a synchronous call to Fare. RestTemplate is used for making the synchronous call. When using RestTemplate, the URL parameter is constructed programmatically, and data is sent across to the other service. In more complex scenarios, we will have to get to the details of the HTTP APIs provided by RestTemplate or even to APIs at a much lower level.

Feign is a Spring Cloud Netflix library for providing a higher level of abstraction over REST-based service calls. Spring Cloud Feign works on a declarative principle. When using Feign, we write declarative REST service interfaces at the client, and use those interfaces to program the client. The developer need not worry about the implementation of this interface. This will be dynamically provisioned by Spring at runtime. With this declarative approach, developers need not get into the details of the HTTP level APIs provided by RestTemplate.

The following code snippet is the existing code in the Booking microservice for calling the Fare service:

Fare fare = restTemplate.getForObject(FareURL +"/get?flightNumber="+record.getFlightNumber()+"&flightDate="+record.getFlightDate(),Fare.class);

In order to use Feign, first we need to change the pom.xml file to include the Feign dependency as follows:

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

For a new Spring Starter project, Feign can be selected from the starter library selection screen, or from http://start.spring.io/. This is available under Cloud Routing as shown in the following screenshot:

Feign as a declarative REST client

The next step is to create a new FareServiceProxy interface. This will act as a proxy interface of the actual Fare service:

@FeignClient(name="fares-proxy", url="localhost:8080/fares")
public interface FareServiceProxy {
  @RequestMapping(value = "/get", method=RequestMethod.GET)
  Fare getFare(@RequestParam(value="flightNumber") String flightNumber, @RequestParam(value="flightDate") String flightDate);
}

The FareServiceProxy interface has a @FeignClient annotation. This annotation tells Spring to create a REST client based on the interface provided. The value could be a service ID or a logical name. The url indicates the actual URL where the target service is running. Either name or value is mandatory. In this case, since we have url, the name attribute is irrelevant.

Use this service proxy to call the Fare service. In the Booking microservice, we have to tell Spring that Feign clients exist in the Spring Boot application, which are to be scanned and discovered. This will be done by adding @EnableFeignClients at the class level of BookingComponent. Optionally, we can also give the package names to scan.

Change BookingComponent, and make changes to the calling part. This is as simple as calling another Java interface:

Fare = fareServiceProxy.getFare(record.getFlightNumber(), record.getFlightDate());

Rerun the Booking microservice to see the effect.

The URL of the Fare service in the FareServiceProxy interface is hardcoded: url="localhost:8080/fares".

For the time being, we will keep it like this, but we are going to change this later in this chapter.

..................Content has been hidden....................

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