Feign Client in Microservices

Posted By Anil Kumar | 21-Apr-2018

Introduction:

In microservice architecture, all services are running independently with respect to other microservices.

All services are registered with Eureka server and Gateway is responsible to redirect the all incoming requests.

In microservices sometimes there is a requirement to get data from one microservice to complete an Incoming request on other microservice.

So we can simply use rest Api call for performing such tasks or we can use feignClient provided by Netflix.

Here is the example of same

1. This is the main class where we are required to use @EnableEurekaClient annotation

 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@ComponentScan
@EnableScheduling
@EnableAsync
@EnableZuulProxy
@EnableFeignClients
@EnableEurekaClient
public class BaseApplication extends SpringBootServletInitializer{
	
	@Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

	@Bean
	public BCryptPasswordEncoder bCryptPasswordEncoder() {
		return new BCryptPasswordEncoder();
	}

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

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(BaseApplication.class);
	}
}

 

2. This is the interface where Feign client integrate the Rest API call

(i) @FeignClient("api-accounts") 

Here api-accounts is application name which rest API is called by using the feign client in this application 

(ii) UrlMapping.GET_MARKET_STATUS is the URL of api-accounts application

Here is the code of UrlMapping

public static final String GET_MARKET_STATUS = "/api-accounts/v1/market/status";

 

Use Netflix Feign client to call a rest API of a microservice in another microservice 

 

import com.base.DTO.MarketDTO;
import com.base.mapping.UrlMapping;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("api-accounts")
public interface AccountsFeignClient {

    @RequestMapping(value = UrlMapping.GET_MARKET_STATUS, method = RequestMethod.GET)
    Boolean getMarketStatus(@RequestHeader("Authorization")String token, @RequestParam MarketDTO marketDTO);

}

 

 

 

 

 

Request for Proposal

Recaptcha is required.

Sending message..