yuhang2685 / Demo-SpringBoot-Microservices

In this demo, we explore how to implement microservices with Spring-Boot and Spring Cloud Eureka through building a Movie Rating App.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Demo-SpringBoot-Microservices

Author: Dr. YUHANG ZHAO

Introduction: In this demo we illustrate how to build microservices with Spring Boot and Spring cloud.

Reference: "Spring Boot Microservices Level 1" by Java Brains

Overview:

  1. In general, microservice changes what happens in the runtime, not only the architecture? but also the thinking of programming.

    We'll be creating a few microservices and then have them communicated with each other. In the process, we'll understand how inter-service communication work, and we'll implement "service discovery" with Eureka to facilitate services discoverying each other to communicate.

    Spring Cloud is one way to do microservice.

  2. Advantage is independent, scalability; disadvantage is the problems in general such as "load balancing".

    In other words, monolith: "complexity hidden with"; microservice: "complexity between microservices".

  3. Monolith -> pieces (microservices); service discovery; how to communicate each other.

  4. Spring Cloud is one way to do microservice.

    Prerequisites for this demo: Java and Spring Boot, since microservice is a SpringBoot app.

    Demo: a movie catalog API application.

  5. Each microservice is a spring boot project.

  6. So far we have constructed 3 applications independently with hard-coded data.

    Discuss we are going to call each other programatically through REST APIs using REST client library. Spring Boot has one client (to do REST API calls) already in your classpath - RestTemplate, we use it to call an external microservice API.

  7. Implement using RestTemplate to call an external microservice API - movie info service.

  8. Use @Bean for RestTemplate.

  9. Use webClient (asynchronous) to make API calls instead of RestTemplate (synchronous).

  10. Make an API accepting user ID and returns a list of movie ID.

  11. Why should avoid returning list in APIs? and how?: use object to wrap list of objects.

  12. Dynamic url in cloud; load balancing needed; Then how to? the discovery server, which is like a phone book. Spring Boot and Spring Cloud use Client side discovery, which requests the discovery server and jump to the supplied url.

    Eureka, as Ribbon, Hysterix, Zuul, belongs to Netflix OSS.

  13. Discuss we will set up Eureka server, register the 3 microservices in it as Eureka clients. Also, who needs Eureka services is also an Eureka client.

  14. Start up a Eureka server: Spring Boot Starter Project using Eureka Server; @EnableEurekaServer; check port 8761 for Dashboard; Error in console solved by adding in application properties that stop acting as a client.

  15. Have microservices registered (publish) using Eureka client: Add dependency; Add application name in application.properties.

  16. Have microservices located (consume) using Eureka client: @LoadBalanced which does everything including locating and load balancing; Replace the hard coded URL by the registered microservice name; Fixed error caused by application name diff to the registered name.

  17. Load balancing: How to duplicate a microservice? through java -Dserver.port=? -jar X.jar

Level 2:

  1. User themovieDB.org to get movie info by making external RESTful API calls.

About

In this demo, we explore how to implement microservices with Spring-Boot and Spring Cloud Eureka through building a Movie Rating App.


Languages

Language:Java 100.0%