{"id":1933,"date":"2019-07-02T12:00:00","date_gmt":"2019-07-02T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-make-two-microservices-communicate\/"},"modified":"2026-07-05T03:23:56","modified_gmt":"2026-07-05T01:23:56","slug":"how-to-make-two-microservices-communicate","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-make-two-microservices-communicate\/","title":{"rendered":"How to Make two Microservices Communicate"},"content":{"rendered":"<p>I would teach you how to make two microservices communicate. This would be step by step. You can <a href=\"#video\">watch the video<\/a> as well.<\/p>\n<p>We assume we already have two applications(not microservices). So we would:<\/p>\n<ul>\n<li>first convert them to microservices<\/li>\n<li>add a Eureka Server<\/li>\n<li>register them with Eureka server<\/li>\n<li>finally, make them communicate<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5><strong>Set Names and Port of the Services<\/strong><\/h5>\n<p>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<strong> ms-teacher<\/strong> and <strong>ms-student<\/strong>. This we&#8217;ll set in the application.properties file.<\/p>\n<p><strong>Follow the steps below:<\/strong><\/p>\n<p><strong>Step 1:<\/strong>\u00a0 Give the application names in the application.properties file<\/p>\n<p><strong>Step 2:<\/strong>\u00a0 Set the port number of the applications in the application.properties file<\/p>\n<p><strong>Step 3:<\/strong> Do the same for the second application<\/p>\n<p>The application.properties file would contain the following<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">spring.datasource.driver<span style=\"color: #333333;\">-<\/span>class<span style=\"color: #333333;\">-<\/span>name<span style=\"color: #333333;\">=<\/span>com.mysql.cj.jdbc.Driver\nspring.datasource.password<span style=\"color: #333333;\">=<\/span>root\nspring.datasource.username<span style=\"color: #333333;\">=<\/span>root\nspring.datasource.url<span style=\"color: #333333;\">=<\/span>jdbc<span style=\"color: #333333;\">:<\/span>mysql<span style=\"color: #333333;\">:<\/span><span style=\"color: #888888;\">\/\/localhost:3301\/teacherdb?serverTimezone=UTC<\/span>\nspring.jpa.hibernate.ddl<span style=\"color: #333333;\">-<\/span><span style=\"color: #008800; font-weight: bold;\">auto<\/span><span style=\"color: #333333;\">=<\/span>create\nspring.datasource.initialization<span style=\"color: #333333;\">-<\/span>mode<span style=\"color: #333333;\">=<\/span>always\n\nspring.application.name<span style=\"color: #333333;\">=<\/span>ms<span style=\"color: #333333;\">-<\/span>teacher\nserver.port<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">8081<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #ff0000;\">Note:<\/span><\/strong> 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<\/p>\n<p>&nbsp;<\/p>\n<h5><strong>Add a Eureka Server<\/strong><\/h5>\n<p>A Eureka Server is called a Discovery Server. It contains a register of all the microservices. Also, all microservices must register with the server.<\/p>\n<p><strong>Follow the steps below to add a Eureka Server:<\/strong><\/p>\n<p><strong>Step 1:\u00a0<\/strong> Create a new spring application<\/p>\n<p><strong>Step 2:\u00a0<\/strong> Add the Eureka Server dependency. You can add this from the <strong><em>New Spring Starter Project<\/em><\/strong> wizard.<\/p>\n<p><strong>Step 3:<\/strong>\u00a0 Add <strong>@EnableEurekaServer<\/strong> to the main Application class<\/p>\n<p><strong>Step 4:<\/strong>\u00a0 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)<\/p>\n<p><strong>Step 5:\u00a0<\/strong> Set the <em><strong>eureka.client.register-with-eureka<\/strong><\/em> to false<\/p>\n<p><strong>Step 6:\u00a0\u00a0<\/strong>Set the<strong><em> eureka.client.fetch-registry<\/em><\/strong> to false.<\/p>\n<p>Steps 5 and 6 is used to inform the server that there is not other server.<\/p>\n<p>The application.properties file for the Eureka Server should now be as shown below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">server.port<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">8761<\/span>\neureka.client.<span style=\"color: #008800; font-weight: bold;\">register<\/span><span style=\"color: #333333;\">-<\/span>with<span style=\"color: #333333;\">-<\/span>eureka<span style=\"color: #333333;\">=<\/span><span style=\"color: #007020;\">false<\/span>\neureka.client.fetch<span style=\"color: #333333;\">-<\/span>registry<span style=\"color: #333333;\">=<\/span><span style=\"color: #007020;\">false<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h5><strong>Add Necessary Dependencies to the Clients<\/strong><\/h5>\n<p><strong>Step 1:\u00a0<\/strong> \u00a0Add spring-cloud dependency to the <strong>ms-student<\/strong> and <strong>ms-teacher<\/strong> pom.xml (include the version number)<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;dependency&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.springframework.cloud<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>spring-cloud-starter-netflix-eureka-client<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span>\n<span style=\"color: #007700;\">&lt;\/dependency&gt;<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:<\/strong> Add Copy the version number from the properties section of the server pom.xml to the clients pom.xml<\/p>\n<p>This is shown below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;properties&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;java.version&gt;<\/span>1.8<span style=\"color: #007700;\">&lt;\/java.version&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;spring-cloud.version&gt;<\/span>Greenwich.RELEASE<span style=\"color: #007700;\">&lt;\/spring-cloud.version&gt;<\/span>\n<span style=\"color: #007700;\">&lt;\/properties&gt;<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong>.\u00a0 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 <a href=\"https:\/\/mvnrepository.com\/\" target=\"_blank\" rel=\"noopener\">Maven Repository<\/a>.<\/p>\n<p>This is shown below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;repositories&gt;<\/span>\n    <span style=\"color: #007700;\">&lt;repository&gt;<\/span> \n\t<span style=\"color: #007700;\">&lt;id&gt;<\/span>spring-milestones<span style=\"color: #007700;\">&lt;\/id&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;name&gt;<\/span>Spring Milestones<span style=\"color: #007700;\">&lt;\/name&gt;<\/span>\n   <span style=\"color: #007700;\">&lt;url&gt;<\/span>https:\/\/repo.spring.io\/milestones<span style=\"color: #007700;\">&lt;\/url&gt;<\/span>\n    <span style=\"color: #007700;\">&lt;\/repository&gt;<\/span>\n<span style=\"color: #007700;\">&lt;\/repositories&gt;<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4:\u00a0<\/strong> Add the <strong>@EnableEurekaClient<\/strong> annotation to the <strong>ms-teacher<\/strong> and <strong>ms-student<\/strong> services<\/p>\n<p><strong>Step 5:\u00a0<\/strong> 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.<\/p>\n<p>You will see the Eureka gui with the three services registered as shown below(though the names would be ms-student and ms-teacher:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-997 size-large aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Eureka-Server-Window-2-1024x735.jpg\" alt=\"Eureka Server Window 2\" width=\"735\" height=\"528\" \/><\/p>\n<p>&nbsp;<\/p>\n<h5><strong>Make HTTP Calls Across Microservice Using RestTemplate<\/strong><\/h5>\n<p>Normally, microservices interact over HTTP. To achieve that, we need to create\u00a0 <em><strong>RestTemplate<\/strong> <\/em>bean and then use it to communicate across the microservices.<\/p>\n<p>To do that, follow the steps below:<\/p>\n<p><strong>Step 1:\u00a0<\/strong> Create a RestTemplate bean in the\u00a0<strong>ms-teacher<\/strong> and <strong>ms-student<\/strong> services. This is simply a method that returns an instance of a RestTemplate.<\/p>\n<p><strong>Step 2:<\/strong> Annotate this method with @Bean annotation<\/p>\n<p><strong>Step 3:<\/strong> Also annotate this method with the @LoadBalanced Annotation<\/p>\n<p>This is what it looks like:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Bean<\/span>\n<span style=\"color: #555555; font-weight: bold;\">@LoadBalanced<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> RestTemplate <span style=\"color: #0066bb; font-weight: bold;\">restTemplate<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">RestTemplate<\/span><span style=\"color: #333333;\">();<\/span>\n\t\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4:\u00a0<\/strong> Autowire the RestTemplate into the restControllers for both the ms-teacher and ms-student services (You already know how to do this)<\/p>\n<p><strong>Step 5:<\/strong>\u00a0 In the StudentController, create a method to return a list of teachers. Annotate with @GetMapping of \/teachers<\/p>\n<p><strong>Step 6:<\/strong>\u00a0 Copy the teacher class from the ms-teacher service to the ms-student service.<\/p>\n<p><strong>Step 7:<\/strong> Copy the Student class from the ms-student service to the ms-teacher service.<\/p>\n<p><strong>Step 8:<\/strong>\u00a0 \u00a0In the TeacherController, create a method the calls the ms-student microservice to retrieve list of students.<\/p>\n<p>Now, the code for microservices communication is shown below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/students\"<\/span><span style=\"color: #333333;\">)<\/span> \n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Object<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getStudents<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\tObject<span style=\"color: #333333;\">[]<\/span> objects <span style=\"color: #333333;\">=<\/span> restTemplate<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getForObject<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"http:\/\/student-microservice\/students\"<\/span><span style=\"color: #333333;\">,<\/span> Object<span style=\"color: #333333;\">[].<\/span><span style=\"color: #0000cc;\">class<\/span><span style=\"color: #333333;\">);<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> Arrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">asList<\/span><span style=\"color: #333333;\">(<\/span>objects<span style=\"color: #333333;\">);<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 9:<\/strong> Test the microservices: try to access student list from the teachers microservice and vice versa.<\/p>\n<h5 id=\"video\">Watch the Video below:<\/h5>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/BdXHrUacTLc\" width=\"100%\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<h5><strong>More Tutorials on Microservices<\/strong><\/h5>\n<ul>\n<li>\n<h6><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/how-to-make-two-microservices-communicate\/\">How to Make two Microservices Communicate<\/a><\/strong><\/h6>\n<\/li>\n<li>\n<h6><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/how-to-build-microservices-in-java-using-spring-eureka-source-codes\/\">How to Build Microservices in Java using Spring, Eureka (Source Codes)<\/a><\/strong><\/h6>\n<\/li>\n<li>\n<h6><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/how-to-build-microservices-in-java-using-spring-eureka-step-by-step-procedure\/\">How to Build Microservices in Java using Spring, Eureka (step by step Procedure)<\/a><\/strong><\/h6>\n<\/li>\n<li>\n<h6><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/dockers-containers-and-kubernetes-a-simple-explanation\/\">Dockers, Containers and Kubernetes \u2013 A Simple Explanation<\/a><\/strong><\/h6>\n<\/li>\n<li>\n<h6><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/what-are-microservices-a-simple-explanation\/\">What are Microservices? \u2013 A Simple Explanation<\/a><\/strong><\/h6>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1933","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1933"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1933\/revisions"}],"predecessor-version":[{"id":2101,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1933\/revisions\/2101"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}