Steps To Access API across Microservices

Posted By : Vijay Kumar | 29-Nov-2018
Introduction
This article is related to the steps related to access of one Microservice's API  from another microservice. Here the Producer microservice has been developed in grails and the consumer microservice has been developed in Spring boot. The API to be accessed is a REST API.
 
Following microservices are involved in the current set up:
  1.  Eureka (discovery service) 
  2.  Zuul (API gateway)
  3.  Producer grails 
  4.  Consumer spring boot 
 
In the above list, each microservice is a eureka client. ie they must all be registered with eureka discovery service. 
 
So the steps are as follows:
 
1 Eureka discovery service must be started
2 Zuul microservice should be started. Here Zuul is acting as API Gateway for all the APIs. The meaning of API gateway is that all the requests go to this microservice and the consumer microservice does not need to know the exact IP address of the producer microservice 
3 Grails microservice which is API producer  should be started
4 Spring boot microservice which is API consumer should be started
 
 
The client can be of two types:
1 RestTemplate
2 feign client. 
 
In the case of RestTemplate, the client knows the exact location of the API and the request is made directly to the endpoint. whereas in case of feigning client the request is indirect that it is routed by the API gateway. 
 
@FeignClient(name = "proucerApplicationName", url = "http://"+"${PRODUCER_GRAILS_HOST_ADDRESS}"+":"+"${PRODUCER_GRAILS_PORT_NUMBER}")
public interface FeignGrailsInterface {

	@GetMapping("/xyz/getAllProjects")
	public List getAllProjects(@RequestHeader("Authorization") String headerValue);

}
In the above example, "/xyz/getAllProjects" is the actual endpoint which belongs to Producer application. Also, the values of the following variables are being fetched from .bashrc in case of Ubuntu
PRODUCER_GRAILS_PORT_NUMBER
PRODUCER_GRAILS_HOST_ADDRESS
 
One more thing to be noted is that the applications should be run from terminals because the values for the properties from .bashrc file are not fetched when we run the application from IDE. 
 
There is another advantage of running the application from terminal is that it runs fast in comparison to IDE as well as we can terminate it easily. The amount of RAM is an important factor in case of microservices.

Conclusion:
In this article, We got an overview of interaction of microservices, which included the producer and consumer microservices. And also got the how feign client is different from the rest template. We also learned what is the basic role of API gateway.

Hope it helps!

About Author

Author Image
Vijay Kumar

Vijay has consulting experience in Java. His current interests include Spring Boot and he is acting as a mentor to the trainees for the same.

Request for Proposal

Name is required

Comment is required

Sending message..