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 System shown below:
I’m currently working on the Step by Step video tutorial which would be available on my channel. The Tech Pro
-
Create 3 Spring Applications in start.spring.io (add the Web dependency)
- Admissions (list of patients)
- HR (list of employee)
- Pathology (list of diseases)
-
Open the three applications in Spring Tool Suite
-
Set the ports and application name in the application.properties file
-
Build the addmissions-service
- Create the AdmissionsResource class(controller) in a resources package
- Annotate the class with @RestController
- Create a method getPatients() that return list of patients
- Create a method getPatientById that takes an Id and returns a single Patient
- Annotate the Id parameter with @PathVariable annotation
- Create a class called Patient in the models package
- Add some hardcoded patients to the getPatients method
- Annotate the AdmissionsResource with RequestMapping of /admissions
- Annotate the getPatients method with the RequestMapping of /patients
- Annotate the getPatientById method with RequestMapping of /patients/{id}
- Move the hardcoded patients outside the method
- Test the addmissions-service
-
Build the hr-service
- Create a HrResource class in the resources package
- Annotate the class with the @RestController
- Annotate the class with @RequestMapping of /hr
- Add some hardcoded list of patients
- Create the Employee class in the models folder
- Create the getEmployees method to return list of employees
- Annotate the getEmployees method with RequestMapping of /employees
- Create the getEmployeeById method that take an Id to return a single employee
- Annotate the Id parameter with @PathVariable annotation
- Annotate the getEmployeeById method with @RequestMapping of /employees/{id}
- Test the hr-service(remember the port!)
-
Build the pathology service
- Create the PathologyResource class in the resources model
- Annotate this class with the @RestController annotation
- Annotate this class with the @RequestMapping annotation of /pathology
- Create a hardcoded list of diseases
- Create the Disease class in the models package
- Create the getDiseases method to return list of diseases
- Annotate the getDiseases method with RequestMapping of /diseases
- Create the getDiseaseById method to return a single disease
- Annotate the getDiseaseById method with RequestMapping of /diseases/{Id}
- Annotate the Id parameter with the @PathVariable annotation
- Test the pathology-service
-
Call the hr-service from the admissions-service using RestTemplate
- Add a getRestTemplate method to the AdmissionsServiceApplication file. This method returns a new RestTemplate (this is a bean)
- Annotate this method with the @Bean annotation
- Create a RestTemplate private variable in the AdmissionsResource class
- Annotate this with the @Autowired annotation
- 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
- Modify the getEmployees method in the HrResource class in the hr-service to return EmployeesList object instead of List<Employees>
- Create the EmployeesList class in the hr-service model package
- Copy the EmployeesList and the Employee class to the admissions-service
- Test the services
-
Call the pathology-service from the admissions-services using RestTemplate
- 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
- Annotate the getDiseases method with RequestMapping of /diseases
- Modify the getDiseases method in the pathology-service to return DiseasesList object instead of List<Diseases> object
- Create a DiseasesList class
- Copy the DiseasesList class and the Disease class to the admissions-service
- Test the services
-
Create the Discovery Server (Eureka) and make the service Eureka Client
- Create a spring boot application using spring initializr (add the Eureka Server dependency)
- Add the @EnableEurekaServer annotation to the DiscoveryServerApplication
- Ensure that the spring cloud dependency is in the pom.xml
- Update the application.properties file with register-with-eureka and fetch-registry to false
- Test the Eureka Server
-
Publish the three services to the Eureka Server
- For the admissions-service, Open the pom.xml and add the spring cloud client dependency (check the pom of the discovery server)
- Provide the spring cloud version as a property
- Copy over the dependencyManagement section from the discovery-server pom.xml to the admissions-service pom.xml
- Copy over the repositories section from the discovery-server pom.xml to the admissions-service.xml (spring-milestones repository)
-
Repeat the procedure in 10 for the hr-service and the pathology-service
-
Start all the services
-
Test the Eureka server and make sure all the services are registered
-
Eureka Server with all the services published If you followed the steps correctly, then you will have the window below
-
Make the admissions-service consume the hr-service and the pathology service
- Add @LoadBalanced Annotation to the RestTemplate bean in the admissions-service class
- Open the AdmissionsResource class
- Replace the urls with the name of the services (replace only the http://localhost:port section of the url)
2 thoughts on “How to Build Microservices in Java using Spring, Eureka (step by step Procedure)”