Build Microservices from scratch

  1. Write all multiple spring boot applications, say 3 spring boot applications.
  2. Synchronous: Make RestTemplate as Bean, and rt.getForObject(url, ClassName.class) . Json response to object of ClassName.
  3. 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)

Annotation: @EnableEurekaServer

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. Ex:http://service-name/endpoint

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store