Microservice Architecture with Spring Cloud in Code
1. Overview
Microservice is a service-oriented architecture where an application is deployed as a collection of loosely-couple services. The goal is to make each service independent, fine-grained, scalable and flexible, allowing faster testing and release.
2. Microservices
2.1 Business Services
2.1.1 applicant-services
Dummy service that returns a list of applicant names.
@GetMapping("/applicants-by-job") public List<String> getApplicantsByJob() { log.debug("port={} get applicants by job", port); return Arrays.asList("Steve", "Bill", "Linus"); }
2.1.2 job-services
Dummy service that returns a job title with a list of applicant names.
@GetMapping("/job-with-applicant-profiles") public ResponseEntity listJobsWithApplicantProfiles() { log.debug("get job details with applicants"); JobWithApplicantsDto result = new JobWithApplicantsDto(); result.setJob("Java Developer"); result.setApplicants(applicantProxy.getApplicantsByJob()); return ResponseEntity.ok().body(result); }
2.2 Spring Cloud Services
2.2.1 naming-server / discovery-server
This server holds all the information about our microservices, such as name, IP, and port. This information is used for exchanging a service name for IP address and port.
A simple naming server typically has the following:
// pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> // in application.yml eureka: client: register-with-eureka: false fetch-registry: false // in SpringBootApplication annotated class @EnableEurekaServer
2.2.2 api-gateway
It's a server that provides criteria-driven request routing. It also offers other features such as security, load balancing, logging, monitoring, etc.
API Gateway is not at all different from the naming-server. Thanks to Spring for doing the heavy lifting:
// pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> // @Configuration annotated class @Bean public RouteLocator gatewayRouter(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p .path("/get") .uri("http://httpbin.org")) .route(p -> p.path("/applicants/**") .uri("lb://applicant-services")) .route(p -> p.path("/jobs/**") .uri("lb://job-services")) .build(); }
3. Spring Cloud Libraries
To enable this feature, @EnableFeignClients must be annotated to a configuration class.
To call an endpoint from another service, an interface must be created with the same method signature as the method from the other service. In this example, we are importing 2 endpoints from the applicant-service.
@FeignClient(name = "applicant-services") public interface ApplicantProxy { @GetMapping("/applicants/applicants-by-job") List<String> getApplicantsByJob(); @GetMapping("/applicants/top-applicants-by-job") public List<String> getTopApplicantsByJob(); }
To register to a naming-server, we need to add this configuration in each of the services:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka // make sure that this address is correct
4. URLs
5. Git Repository
- https://github.com/czetsuya/lab-microservice-spring
Post a Comment