Including Feign in the cloud application

First we will include the Netflix Feign dependency in the pom.xml file:

<parent> 
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-starter-parent</artifactId> 
   <version>2.0.2.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository --> 
</parent> 
 
<properties> 
   ... 
   <spring-cloud.version>Finchley.M8</spring-cloud.version> 
</properties> 
 
<dependencies> 
   ... 
   <dependency> 
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-starter-web</artifactId> 
   </dependency> 
   <dependency> 
         <groupId>org.springframework.cloud</groupId> 
         <artifactId>spring-cloud-starter-netflix-eureka-
client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ... </dependencies>

To include Feign in your client application project based on the Spring Cloud, use the Starter with the org.springframework.cloud group and the spring-cloud-starter-openfeign artifact ID.

Now, let's define a Feign client by creating an interface with the @FeignClient annotation. This interface is working as a client to access microservices registered on the discovery server. But how do we access these services? We have to specify the name value as account-service on the @FeignClient annotation (it is the logical service name of the account microservice using Eureka for discovery). Let's see the following code for this interface:

package com.dineshonjava.customerservice.service; 
 
import java.util.List; 
 
import org.springframework.cloud.openfeign.FeignClient; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PathVariable; 
 
import com.dineshonjava.customerservice.domain.Account; 
 
@FeignClient("account-service") 
public interface AccountService { 
    
   @GetMapping(value = "/account/customer/{customer}") 
   List<Account> findByCutomer (@PathVariable("customer") Integer 
customer); @PutMapping(value = "/account/{accountId}", consumes =
"application/json") Account update(@PathVariable("storeId") Integer
accountId, Account account); @DeleteMapping(value = "/account/{accountId}") void delete(@PathVariable("accountId") Integer accountId); @PostMapping(value = "/account/customer/", consumes =
"application/json") Account update(@RequestBody Account account); }

We have used the @FeignClient("account-service") annotation with the account-service logical service name. And we have defined the method call to be made to consume this account REST microservice exposed by the account-service module with the /account/customer/{customer} endpoint.

To make sure this @FeignClient annotation is working, we have to enable the Feign client cloud behavior in your application. Finally, we annotate the Spring Boot main class with @EnableFeignClients. Let's see the following main class of the application:

package com.dineshonjava.customerservice; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.openfeign.EnableFeignClients; 
 
@SpringBootApplication 
@EnableFeignClients 
public class CustomerServiceApplication { 
 
   public static void main(String[] args) { 
         SpringApplication.run(CustomerServiceApplication.class, args); 
   } 
} 

Next create another microservice, CUSTOMER-SERVICE, and it will access the account microservice to fetch all accounts associated with a customer in the banking application. Let's see the following class that will use the accountService by using Feign:

package com.dineshonjava.customerservice.controller; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.DeleteMapping; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.PostMapping; 
import org.springframework.web.bind.annotation.PutMapping; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RestController; 
 
import com.dineshonjava.customerservice.domain.Customer; 
import com.dineshonjava.customerservice.repository.CustomerRepository; 
import com.dineshonjava.customerservice.service.AccountService; 
 
@RestController 
public class CustomerController { 
    
   @Autowired 
   CustomerRepository customerRepository; 
    
   @Autowired 
   AccountService accountService;   
    
   @PostMapping(value = "/customer") 
   public Customer save (@RequestBody Customer customer){ 
         return customerRepository.save(customer); 
   } 
    
   @GetMapping(value = "/customer") 
   public Iterable<Customer> all (){ 
         return customerRepository.findAll(); 
   } 
    
   @GetMapping(value = "/customer/{customerId}") 
   public Customer findByAccountId (@PathVariable Integer customerId){ 
         Customer customer = 
customerRepository.findByCustomerId(customerId); customer.setAccount(accountService.findByCutomer(customerId)); return customer; } @PutMapping(value = "/customer") public Customer update (@RequestBody Customer customer){ return customerRepository.save(customer); } @DeleteMapping(value = "/customer") public void delete (@RequestBody Customer customer){ customerRepository.delete(customer); accountService.delete(customer); } }

An interface AccountService annotated with @FeignClient has autowired with this controller class of the customer microservice. The complete code of these microservices (AccountService,Customer Service, and, web application service) is available on GitHub:
https://github.com/PacktPublishing/Mastering-Spring-Boot-2.0.

Feign clients can be used to consume text-based HTTP APIs only, which means that they cannot handle binary data, for example, file uploads or downloads.

Let's run these microservices along with the Eureka Server, following the console output with the registered services with Eureka:

Now access the following URL endpoint of the customer microservice:

http://192.168.225.208:6161/customer/1001

This will fetch information about the customer whose customer ID is 1001 and also fetch the account associated with this customer from the account microservice. Let's see the following output:

In this example, there are two accounts associated with the 1001 customer ID. In the next section, let's look at how to override the default configurations of the Feign client.

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

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