Part 3 – Microservices Feign Client

Feign Client

Microservices Feign Client

What have we learned so far,

PART 1 – MICROSERVICES INTRODUCTION – In this tutorial, we discussed what microservices architecture is and how its different from monolithic architecture.

 PART 2 – SCENARIO TO DEVELOP – In this tutorial, we discussed the scenario to develop.

What is Feign Client? How to call microservices?

Feign aims at simplifying the HTTP API clients. The developer needs only to declare and annotate an interface while the actual implementation will be provided at runtime. as we saw in previous tutorial.


1. Add below spring-cloud-starter-feign maven dependency

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

<version>1.4.5.RELEASE</version>

</dependency>

2. Create new feign client interface with the list of methods needs to called from customer service

package com.onlyfullstack.accountservice.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.onlyfullstack.accountservice.bean.Customer;

@FeignClient(url="localhost:8200" , name="customer-service")

public interface CustomerServiceProxy {

    @GetMapping("/customers")
    public List<Customer> getAllCustomers();

    @GetMapping("/customers/{customer_id}")
    public Customer getCustomerDetails(@PathVariable("customer_id") Long customer_id);

}

3. Add below property to the application.properties in order to give the application name which is beign used by the @FeignClient(url=”localhost:8200″ , name=”customer-service”) property.

server.port=8081
spring.application.name=customer-service

4. Now lets modify our Account controller, here we have removed the restTemplate code and added a simple method invocation for the feign interface that we have created earlier. Spring takes the responsibility and creates and injects its concreate class on server startup.

package com.onlyfullstack.accountservice.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.onlyfullstack.accountservice.bean.Account;
import com.onlyfullstack.accountservice.bean.Customer;
import com.onlyfullstack.accountservice.service.CustomerServiceProxy;

@RestController()
public class AccountController {
 
 
 private List<Account> listOfAccounts = new ArrayList<>(5);
 
 @Autowired
 RestTemplate restTemplate;
 
 @Autowired
 CustomerServiceProxy customerServiceProxy;
 
 public AccountController(){
   Account account1 = new Account(new Long(1), null, new Double(1111.50));
   Account account2 = new Account(new Long(2), null, new Double(1112.50));
   Account account3 = new Account(new Long(3), null, new Double(1113.50));
   this.listOfAccounts.add(account1);
   this.listOfAccounts.add(account2);
   this.listOfAccounts.add(account3);
 }
 
 @GetMapping("/accounts")
 public List<Account> getAllCustomers(){
  return this.listOfAccounts;
 }
 
 @GetMapping("/accounts/{account_id}")
 public Account getCustomerDetails(@PathVariable Long account_id) {
  Account account = this.listOfAccounts.stream().filter(acc -> acc.getId().equals(account_id)).findAny().orElse(null);
  /*Map<String, Long> params = new HashMap<>(); // Part 2 Changes
     params.put("customer_id", account.getId());
  Customer customer = restTemplate.getForObject("http://localhost:8081/customers/{customer_id}" , Customer.class, params);*/

  Customer customer = customerServiceProxy.getCustomerDetails(account_id); // Part 3 Changes
  account.setCustomer(customer);
  return account;
 }

}

As you can see, we have called the rest service just like a method invocation. feign client removes the boilerplate code of rest template and implements the associated client at runtime.

Source Code
Download source code of microservice with spring cloud from below git repository :

Lets go to our next tutorial where we will discuss

Part 4 – Ribbon Client

In this tutorial we will understand below topics
– What is Ribbon Client?
– What is client side load balancing?
– How to implement ribbon in microservices?
PART 4 – MICROSERVICES WITH SPRING CLOUD : RIBBON CLIENT

Microservices Tutorial

Microservices Tutorial

Related Posts

Microservices Interview Questions

1 Comment

Comments are closed.