• Feign Client in Microservices

    Posted by Anil Kumar | Last Updated: 01-May-18


    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.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.web.client.RestTemplate;
    public class BaseApplication extends SpringBootServletInitializer{
        public RestTemplate getRestTemplate() {
            return new RestTemplate();
    	public BCryptPasswordEncoder bCryptPasswordEncoder() {
    		return new BCryptPasswordEncoder();
    	public static void main(String[] args) {, args);
    	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.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;
    public interface AccountsFeignClient {
        @RequestMapping(value = UrlMapping.GET_MARKET_STATUS, method = RequestMethod.GET)
        Boolean getMarketStatus(@RequestHeader("Authorization")String token, @RequestParam MarketDTO marketDTO);