Learnitweb

Invoke other microservice using Feign client

1. Introduction

In this tutorial, we’ll create use microservices – product and inventory. These microservices will be registered with a Eureka server. We’ll then call an endpoint of product microservice and this will call the inventory microservice.

2. Inventory microservice

In our previous tutorials, we used product microservice. The inventory microservice is similar to that service. This microservice has a controller, InventoryController.

package com.learnitweb.inventoryservice.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.learnitweb.inventoryservice.dto.InventoryDto;

@RestController
@RequestMapping("/api")
public class InventoryController {

	@GetMapping("/inventory/{name}")
	public ResponseEntity<?> getProductInventory(@PathVariable String name) {
		System.out.println("getProductInventory");
		if (name.equals("car")) {
			InventoryDto inventory = new InventoryDto();
			inventory.setProductName("some car");
			inventory.setQuantityRemaining(100);

			return new ResponseEntity<InventoryDto>(inventory, HttpStatus.OK);
		}
		return new ResponseEntity(null, HttpStatus.OK);

	}

}

This controller has one endpoint /api/inventory/{name}. This endpoint is a dummy endpoint which just returns an inventory object with information.

3. Making a Feign Client – product service

Let’s use the product service to call the inventory service. To do that, add the following dependency:

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

Then you need to add the @EnableFeignClients annotation on the application main class.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class ProductserviceApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProductserviceApplication.class, args);
	}

}

Next, we need to create an interface which is the declarative way to tell which methods to call. Here is the interface:

package com.learnitweb.productservice.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.learnitweb.productservice.dto.InventoryDto;

@FeignClient(name = "InventoryService")
public interface InventoryFeignClient {

	@GetMapping("/api/inventory/{name}")
	public ResponseEntity<InventoryDto> getProductInventory(@PathVariable("name") String name);

}

Here, InventoryService is the is the logical name of the service registered with the Eureka server. The signature of the method is same as that of the method in Inventory microservice controller. Notice that the signature of the getProductInventory method in InventoryController is same as the one defined in the interface. The method in the interface has the annotation @GetMapping("/api/inventory/{name}") which is the endpoint of the inventory service to be called.

4. Test our changes

To test our changes, we’ll make a controller in product service. This controller will invoke the method of interface which calls the inventory microservice endpoint. When you invoke the http://localhost:8071/api/inventory/car endpoint of product microservice, you should get the following result.

{
  "productName": "some car",
  "quantityRemaining": 100
}

As you can see, the product microservice has not called the inventory microservice directly. We have declared an interface in the product microservice and the method of the interface is invoked, rest of the things are managed by the Feign client.