{"id":1926,"date":"2019-06-07T12:00:00","date_gmt":"2019-06-07T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-build-microservices-in-java-using-spring-eureka-step-by-step-procedure\/"},"modified":"2026-07-05T03:23:38","modified_gmt":"2026-07-05T01:23:38","slug":"how-to-build-microservices-in-java-using-spring-eureka-step-by-step-procedure","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-build-microservices-in-java-using-spring-eureka-step-by-step-procedure\/","title":{"rendered":"How to Build Microservices in Java using Spring, Eureka (step by step Procedure)"},"content":{"rendered":"<p>We would build a Microservices in Java Step by step. <a href=\"https:\/\/kindsonthegenius.com\/tempsite\/how-to-build-microservices-in-java-using-spring-eureka-source-codes\/\">Find the Source Codes here<\/a><\/p>\n<p>This microservice is based on a simple hypothetical Hospital Information System shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-952 \" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Hospital-Information-Service-Microservice-Architecture.jpg\" alt=\"\" width=\"577\" height=\"317\" \/><\/p>\n<p>I&#8217;m currently working on the Step by Step video tutorial which would be available on my channel.\u00a0<a href=\"https:\/\/www.youtube.com\/channel\/UCppSe7mf1kdxA3Ga49rPWBg\" target=\"_blank\" rel=\"noopener\">The Tech Pro<\/a><\/p>\n<ol>\n<li>\n<h5><strong>Create 3 Spring Applications in start.spring.io (add the Web dependency)<\/strong><\/h5>\n<ul>\n<li>Admissions (list of patients)<\/li>\n<li>HR (list of employee)<\/li>\n<li>Pathology (list of diseases)<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5>Open the three applications in Spring Tool Suite<\/h5>\n<\/li>\n<li>\n<h5>Set the ports and application name in the application.properties file<\/h5>\n<\/li>\n<li>\n<h5><strong>Build the\u00a0 addmissions-service<\/strong><\/h5>\n<ul>\n<li>Create the AdmissionsResource class(controller) in a resources package<\/li>\n<li>Annotate the class with @RestController<\/li>\n<li>Create a method getPatients() that return list of patients<\/li>\n<li>Create a method getPatientById that takes an Id and returns a single Patient<\/li>\n<li>Annotate the Id parameter with @PathVariable annotation<\/li>\n<li>Create a class called Patient in the models package<\/li>\n<li>Add some hardcoded patients to the getPatients method<\/li>\n<li>Annotate the AdmissionsResource with RequestMapping of \/admissions<\/li>\n<li>Annotate the getPatients method with the RequestMapping of \/patients<\/li>\n<li>Annotate the getPatientById method with RequestMapping of \/patients\/{id}<\/li>\n<li>Move the hardcoded patients outside the method<\/li>\n<li>Test the addmissions-service<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5><strong>Build the hr-service<\/strong><\/h5>\n<ul>\n<li>Create a HrResource class in the resources package<\/li>\n<li>Annotate the class with the @RestController<\/li>\n<li>Annotate the class with @RequestMapping of \/hr<\/li>\n<li>Add some hardcoded list of patients<\/li>\n<li>Create the Employee class in the models folder<\/li>\n<li>Create the getEmployees method to return list of employees<\/li>\n<li>Annotate the getEmployees method with RequestMapping of \/employees<\/li>\n<li>Create the getEmployeeById method that take an Id to return a single employee<\/li>\n<li>Annotate the Id parameter with @PathVariable annotation<\/li>\n<li>Annotate the getEmployeeById method with @RequestMapping of \/employees\/{id}<\/li>\n<li>Test the hr-service(remember the port!)<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5><strong>Build\u00a0 the pathology service<\/strong><\/h5>\n<ul>\n<li>Create the PathologyResource class in the resources model<\/li>\n<li>Annotate this class with the @RestController annotation<\/li>\n<li>Annotate this class with the @RequestMapping annotation of \/pathology<\/li>\n<li>Create a hardcoded list of diseases<\/li>\n<li>Create the Disease class in the models package<\/li>\n<li>Create the getDiseases method to return list of diseases<\/li>\n<li>Annotate the getDiseases method with RequestMapping of \/diseases<\/li>\n<li>Create the getDiseaseById method to return a single disease<\/li>\n<li>Annotate the getDiseaseById method with RequestMapping of \/diseases\/{Id}<\/li>\n<li>Annotate the Id parameter with the @PathVariable annotation<\/li>\n<li>Test the pathology-service<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5><strong>Call the hr-service from the admissions-service using RestTemplate<\/strong><\/h5>\n<ul>\n<li>Add a getRestTemplate method to the AdmissionsServiceApplication file. This method returns a new RestTemplate (this is a bean)<\/li>\n<li>Annotate this method with the @Bean annotation<\/li>\n<li>Create a RestTemplate private variable in the AdmissionsResource class<\/li>\n<li>Annotate this with the @Autowired annotation<\/li>\n<li>Create a getPhysicians method in the AdmissionsResource class. This method uses restTemplate to get list of employees from the hr-service. It returns this list of employees as EmployeesList object<\/li>\n<li>Modify the getEmployees method in the HrResource class in the hr-service to return EmployeesList object instead of List&lt;Employees&gt;<\/li>\n<li>Create the EmployeesList class in the hr-service model package<\/li>\n<li>Copy the EmployeesList and the Employee class to the admissions-service<\/li>\n<li>Test the services<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5><strong>Call the pathology-service from the admissions-services using RestTemplate<\/strong><\/h5>\n<ul>\n<li>Create a method getDiseases in the AdminssionsResource class. This method uses restTemplate to get the list of diseases. This method returns a list of diseases as DiseasesList object<\/li>\n<li>Annotate the getDiseases method with RequestMapping of \/diseases<\/li>\n<li>Modify the getDiseases method in the pathology-service to return DiseasesList object instead of List&lt;Diseases&gt; object<\/li>\n<li>Create a DiseasesList class<\/li>\n<li>Copy the DiseasesList class and the Disease class to the admissions-service<\/li>\n<li>Test the services<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5><strong>Create the Discovery Server (Eureka) and make the service Eureka Client<\/strong><\/h5>\n<ul>\n<li>Create a spring boot application using spring initializr (add the Eureka Server dependency)<\/li>\n<li>Add the @EnableEurekaServer annotation to the DiscoveryServerApplication<\/li>\n<li>Ensure that the spring cloud dependency is in the pom.xml<\/li>\n<li>Update the application.properties file with register-with-eureka and fetch-registry to false<\/li>\n<li>Test the Eureka Server<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5><strong>Publish the three services to the Eureka Server<\/strong><\/h5>\n<ul>\n<li>For the admissions-service, Open the pom.xml and add the spring cloud client dependency (check the pom of the discovery server)<\/li>\n<li>Provide the spring cloud version as a property<\/li>\n<li>Copy over the dependencyManagement section from the discovery-server pom.xml to the admissions-service pom.xml<\/li>\n<li>Copy over the repositories section from the discovery-server pom.xml to the admissions-service.xml (spring-milestones repository)<\/li>\n<\/ul>\n<\/li>\n<li>\n<h5>Repeat the procedure in 10 for the hr-service and the pathology-service<\/h5>\n<\/li>\n<li>\n<h5>Start all the services<\/h5>\n<\/li>\n<li>\n<h5>Test the Eureka server and make sure all the services are registered<\/h5>\n<\/li>\n<li>\n<figure id=\"attachment_950\" aria-describedby=\"caption-attachment-950\" style=\"width: 735px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-950 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Eureka-Server-Window-1024x808.jpg\" alt=\"Eureka Server Window\" width=\"735\" height=\"580\" \/><figcaption id=\"caption-attachment-950\" class=\"wp-caption-text\">Eureka Server with all the services published<\/figcaption><\/figure>\n<p>If you followed the steps correctly, then you will have the window below<\/li>\n<li>\n<h5><strong>Make the admissions-service consume the hr-service and the pathology service<\/strong><\/h5>\n<ul>\n<li>Add @LoadBalanced Annotation to the RestTemplate bean in the admissions-service class<\/li>\n<li>Open the AdmissionsResource class<\/li>\n<li>Replace the urls with the name of the services (replace only the http:\/\/localhost:port section of the url)<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h5><\/h5>\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>We would build a Microservices in Java Step by step. Find the Source Codes here This microservice is based on a simple hypothetical Hospital Information &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":[85],"tags":[],"class_list":["post-1926","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1926","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=1926"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1926\/revisions"}],"predecessor-version":[{"id":2094,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1926\/revisions\/2094"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}