How to Make two Microservices Communicate

How to Make Two Microservices Communicate

I would teach you how to make two microservices communicate. This would be step by step. You can watch the video as well.

We assume we already have two applications(not microservices). So we would:

  • first convert them to microservices
  • add a Eureka Server
  • register them with Eureka server
  • finally, make them communicate

 

Set Names and Port of the Services

For and application to serve as a microservice, it should run on a different port. It should also have a name. The two services are named ms-teacher and ms-student. This we’ll set in the application.properties file.

Follow the steps below:

Step 1:  Give the application names in the application.properties file

Step 2:  Set the port number of the applications in the application.properties file

Step 3: Do the same for the second application

The application.properties file would contain the following

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=root
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://localhost:3301/teacherdb?serverTimezone=UTC
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always

spring.application.name=ms-teacher
server.port=8081

 

Note: the first 6 lines contains settings for connection to MySQL databases. So we are focusing on the last two lines: the application name and the port

 

Add a Eureka Server

A Eureka Server is called a Discovery Server. It contains a register of all the microservices. Also, all microservices must register with the server.

Follow the steps below to add a Eureka Server:

Step 1:  Create a new spring application

Step 2:  Add the Eureka Server dependency. You can add this from the New Spring Starter Project wizard.

Step 3:  Add @EnableEurekaServer to the main Application class

Step 4:  Set the port of the server in the application.properties file. (same way you did for the other applications. But remember to use a different port number)

Step 5:  Set the eureka.client.register-with-eureka to false

Step 6:  Set the eureka.client.fetch-registry to false.

Steps 5 and 6 is used to inform the server that there is not other server.

The application.properties file for the Eureka Server should now be as shown below:

 

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

 

 

Add Necessary Dependencies to the Clients

Step 1:   Add spring-cloud dependency to the ms-student and ms-teacher pom.xml (include the version number)

 

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

 

Step 2: Add Copy the version number from the properties section of the server pom.xml to the clients pom.xml

This is shown below:

 

<properties>
	<java.version>1.8</java.version>
	<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>

 

Step 3.  Copy the repositories section to the pom.xml. This is Spring Milestone and allows dependencies to be pulled from other sources if they are not yet in Maven Repository.

This is shown below

<repositories>
    <repository> 
	<id>spring-milestones</id>
	<name>Spring Milestones</name>
   <url>https://repo.spring.io/milestones</url>
    </repository>
</repositories>

 

Step 4:  Add the @EnableEurekaClient annotation to the ms-teacher and ms-student services

Step 5:  Test the services to make sure they registered with the Eureka Server. To to this, just run all the three applications, then visit http://localhost:8761.

You will see the Eureka gui with the three services registered as shown below(though the names would be ms-student and ms-teacher:

 

Eureka Server Window 2

 

Make HTTP Calls Across Microservice Using RestTemplate

Normally, microservices interact over HTTP. To achieve that, we need to create  RestTemplate bean and then use it to communicate across the microservices.

To do that, follow the steps below:

Step 1:  Create a RestTemplate bean in the ms-teacher and ms-student services. This is simply a method that returns an instance of a RestTemplate.

Step 2: Annotate this method with @Bean annotation

Step 3: Also annotate this method with the @LoadBalanced Annotation

This is what it looks like:

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
	return new RestTemplate();
	
}

 

Step 4:  Autowire the RestTemplate into the restControllers for both the ms-teacher and ms-student services (You already know how to do this)

Step 5:  In the StudentController, create a method to return a list of teachers. Annotate with @GetMapping of /teachers

Step 6:  Copy the teacher class from the ms-teacher service to the ms-student service.

Step 7: Copy the Student class from the ms-student service to the ms-teacher service.

Step 8:   In the TeacherController, create a method the calls the ms-student microservice to retrieve list of students.

Now, the code for microservices communication is shown below:

@GetMapping("/students") 
public List<Object> getStudents() {
	Object[] objects = restTemplate.getForObject("http://student-microservice/students", Object[].class);
	return Arrays.asList(objects);
}

 

Step 9: Test the microservices: try to access student list from the teachers microservice and vice versa.

Watch the Video below:

 

More Tutorials on Microservices

4 Comments on “How to Make two Microservices Communicate”

Leave a Reply

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