How to Build Microservices in Java using Spring, Eureka (Source Codes)

How to Build Microservices Step by Step Source Codes

Find the Step by Step Procedure here

Final AdmissionsResource File
@RestController
@RequestMapping("/admissions")
public class AdmissionsResource {
	
	@Autowired
	private RestTemplate restTemplate;
	
	//A hardcoded list of patients
	List<Patient> patients = Arrays.asList(				
		new Patient("P1", "Joseph", "Nigerian"),
		new Patient("P2", "Gabor", "Hungarian")
		);
	
	//getPatients() returns a list of patients
	@RequestMapping("/patients")
	public List<Patient> getPatients() {
		return patients;
	}
	
	//getPatientById() returns a patient with a given Id
	@RequestMapping("/patients/{Id}")
	public Patient getPatientById(@PathVariable("Id") String Id) {
		Patient p = patients.stream()
				.filter(patient -> Id.equals(patient.getId()))
				.findAny()
				.orElse(null);
		return p;
	}
	
	//getPhysicians calls the hr-service microservice to get list of physicians
	@RequestMapping("/physicians")
	public EmployeesList getPhysicians() {
		EmployeesList physicians = 
				restTemplate.getForObject("http://hr-service/hr/employees", EmployeesList.class);
		return physicians;
	}
	
	//getDiseases calls the pathology-service to get list of diseases
	@RequestMapping("/diseases")
	public DiseasesList getDiseases() {
		DiseasesList diseases = 
				restTemplate.getForObject("http://pathology-service/pathology/diseases", DiseasesList.class);
		return diseases;
	}	
}

 

Final HrResource File
@RestController
@RequestMapping("/hr")
public class HrResource {
	
	//Hardcoded list of employees
	List<Employee> employees = Arrays.asList(
		new Employee("E1", "Kindson", "Munonye", "MedTech"),
		new Employee("E2", "Kate", "Winters", "Surgery")
	);
	
	//getEmployees returns list of employees
	@RequestMapping("/employees")
	public EmployeesList getEmployees() {
		
		EmployeesList employeesList = new EmployeesList();
		employeesList.setEmployees(employees);		
		return employeesList;
	}
	
	//getEmployeeById returns an employee with the given Id
	@RequestMapping("/employees/{Id}")
	public Employee getEmployeeById(@PathVariable("Id") String Id) {
		Employee e = employees.stream()
				.filter(employee ->Id.equals(employee.getId()))
				.findAny()
				.orElse(null);
		return e;
	}		
}

 

 

Final PathologyResource File
@RestController
@RequestMapping("/pathology")
public class PathologyResource {
	
	//Hardcoded list of diseases
	private List<Disease> diseases = Arrays.asList(
		new Disease("D1", "Ashma", "Warm water bath"),
		new Disease("D2", "Headache", "Panadol capsule")
	);
	
	//getDiseases returns a list of diseases
	@RequestMapping("/diseases")
	public DiseasesList getDiseases() {
		DiseasesList diseasesList = new DiseasesList();
		diseasesList.setDiseases(diseases);
		return diseasesList;
	}
	
	//getDiseaseById returns the disease with the given Id
	@RequestMapping("/diseases/{Id}")
	public Disease getDiseaseById(@PathVariable("Id") String Id) {
		
		Disease d = diseases.stream()
			.filter(disease -> Id.equals(disease.getId()))
			.findAny()
			.orElse(null);	
		
		return d;
	}	
}

 

pom.xml for the admission-service
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.kindsonthegenius</groupId>
	<artifactId>admissions-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>admissions-service</name>
	<description>Admissions Service</description>

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

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	
	<repositories>
	    <repository> 
	        <id>spring-milestones</id> 
	        <name>Spring Milestone</name> 
	        <url>http://repo.spring.io/milestone</url> 
	    </repository>
	</repositories>
</project>

 

application.properties file of the Discovery Server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

 

EmployeesList class

public class EmployeesList {
	
	private List<Employee> employees;

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}		
}

 

More Tutorials on Microservices