Build Microservices from scratch
- Write all multiple spring boot applications, say 3 spring boot applications.
- Synchronous: Make RestTemplate as Bean, and
rt.getForObject(url, ClassName.class). Json response to object of ClassName.
- Asynchronous: Build webClient, (comment the above line mentioned in pt 2). Dependency: Reactive Web (web-flux).
.get() //or post method
.bodyToMono(ClassName.class) // response to instance of Class
4. Returning list is a problem, so always return objects
5. Issues: Code updates; dynamic updates; Load balancing
6. Service discovery (2 types): Above issues (pt 5) get us to here;
Discovery server: It registers the (3) services.
Client side service discovery — Client asks discovery server for the service it needs and discovery server responds with service location. Client now knows the address of service and communicate with it. Eg: Spring Cloud
Server side service discovery — Client says “send this message to service-x” to discovery server and discovery server delivers it to the service-x.
Eureka: Spring Cloud uses eureka to implement service discovery (Netflix OSS — Eureka, Ribbon, Zuul, Hysterix).
eureka clients(client; consume) — eureka server(discovery server)— eureka clients(services themselves; publish)
Dependencies: Eureka server(discovery server) and Eureka discovery(microservices)
Modify pom.xml to eureka client and start it. Clients will search for default port and register themselves to eureka server. COOL!
Name the service in application.properities
Annotation: @LoadBalance to the restTemplate in eureka client.
Now in getForObject, instead of url localhost, put the name of the service.
7. Spring handles caching (default cache) and… it also handles load balancing!!
8. Send heart beat (polling) for fault tolerance. Picks from cache if one of the service is down.