Feign Client in Spring Boot MicroService

Home / Feign Client in Spring Boot MicroService

We know that Spring boot micro services application is divided into modules or separate application. And in many cases it is necessary that we have to call the one application end point from another application. So to Call one application end point from another application Spring boot provided two choices.

  1. RestTemplae
  2. Feign Client

In this example we are using Feign Client to communicate between two applications.

So to working with Feign Client we have to create two applications with some end Point. You can follow my Create Application in Spring boot microservice Blog to see how to create application in Spring boot Microservices.

Suppose we have two differnet application hosted on Eureka Server with name CLIENT1 , CLIENT2.

Note: CLIENT1 and CLIENT2 are the name that you give to your application inside application.properties using.

spring.application.name=client1

Now Suppose we are Calling end point of CLIENT2 application from CLIENT1

So we will perform some steps in CLIENT1 application

1- Add below dependency in your pom.xml file

<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>

2- Enable Feign Client using @EnableFeignClients annotation in your main class.

@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class EurekaClient1Application {

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

3- Create an Interface with @FeignClient and @RibbonClient annotation and give the name of application that display on your Eureka Server.

@FeignClient(name = "CLIENT2")
@RibbonClient(name = "CLIENT2")
public interface Client2proxy {

	@GetMapping("client2")
	public String proxyResponse();
}

In above Interface we are given name of CLIENT2 application and create a end point with same that we created in CLIENT2 application.

Note: In CLIENT2 application you must have an end point /client2 that return some string.

4- Now create a Controller class in CLIENT1 application and Auto-wired your proxy interface in that application

@RestController
public class ClientController1 {
	
	@Autowired
	private Client2proxy proxy;

	@GetMapping("/client1")
	public String client1() {
		return "I am client 1";
	}
	
	@GetMapping("/testProxy")
	public String testP() {
		String proxyResponse = proxy.proxyResponse();
		return proxyResponse;
	}
}

Now Hit below URL in your browser you will get the response from CLIENT2 application end point

http://localhost:8081/testProxy

Help Others, Please Share

About Author

3 Comments
  1. blog3002.xyz

    Hi, I log on to your new stuff regularly. Your humoristic style is awesome, keep it
    up!

  2. Leeann Crenshaw

    hi there, your site is so good.Following your articles.

  3. WWW.XMC.PL

    This is a attention-grabbing article by the way. I am going to go ahead and save this article for my brother to read later on tomorrow. Keep up the high-quality work.

Leave a Reply

Your email address will not be published. Required fields are marked *

x