Friday, 27 September 2019

Spring Boot Microservices

Spring Boot Microservices Demonstration:::

Consider we have 3 services which give info about Movie information and user ratings:

1)   Movie Info Service - i/p: MovieID,    o/p:Movie details
2)   Ratings Data Service - i/p:userId,    o/p:MovieId & ratings & i/p:movieId,    o/p:MovieId & ratings
3)   Movie Catalog service - i/p:userId   o/p:Movie list with details(mname, descr, details)

Steps to create individual APIs and their communication::
  • Using "start.spring.io" create 3 Maven web projects.
  • Extract downloaded projects and import them into workspace.
  • Each project will have:                                                                                                                      >A bean to hold data  (Getters, setters, default constructor, parameterized constructor)              >Resource class which will handle input requests. (@RestController, @RequestMapping with get methods)
  • In each project search for application.properties file and assign separate port number for each service. Foe ex: server.port = 8082 //catalog service
  • 3 services are buil independently with some data and can be accessed as:
http://localhost:8082/catalog/ss
http://localhost:8081/movies/ss
http://localhost:8083/ratingsdata/sam
  •  Now we will have to make these servicescommunicate to each other. This can be done by using RestTemplate / WebClient
  • In the movie catalog service we can do below steps
  1. Using userId get the movieId
  2. Using  movieId get the ratings from ratings service.
  3. Using movieId get movie name from movie info services.
Note: These calls are synchronous
  • Since RestTemplateis called everytime we hot the individual webservice url, why can't we make it singleton> This can be achieved by @Bean anootation. In MovieCatalogServiceApplication clss add below code:
@Bean
public RestTemplate getRestTemplate() {
 return new RestTemplate();
}                                                                                                                                    
  •  In the catalog resource class use it as @Autowired
@Autowired
private RestTemplate restTemplate //inject me the bean created by container

*Use Eureka server for service URL registration and discovery.
1) Create Maven project with Eureka server dependency in "start.spring.io" and add it in workspace
   >change properties so that eureka is not client and trying to find any other server
Update application.properties as:
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

2) Register the client by including eureka client in pom.xml and required update project to get spring cloud jar. This service will be registered as client. Do this for all the 3 services.

3) @LoadBalanced -- does service discovery in load balanced way
@LoadBalanced
    public RestTemplate getRestTemplate() {
                return new RestTemplate();
    }
   
The created jars with the deployed paths are as below::

cf push testSam -p ratings-data-service-0.0.1-SNAPSHOT.jar
https://testsam.cfapps.io/ratingsdata/users/sam

cf push testSam -p movie-info-service-0.0.1-SNAPSHOT.jar
https://testsam.cfapps.io/movies/sam

cf push testSam -p discovery-server-0.0.1-SNAPSHOT.jar

cf push testSam -p movie-catalog-service-0.0.1-SNAPSHOT.jar
https://testsam.cfapps.io/catalog/sam - This is the integrated service which calls
http://ratings-data-service/ratingsdata/users/sam
http://movie-info-service/movies/1111

Friday, 6 September 2019

Spring Boot course API

::Welcome to Course API::

Actions to be performed

1) Get All the available topics

2) Get a particular topic from the list
    Java course details
    C++ course details

3) POST, DELETE, PUT operations are included in the below postman project



The relevant screens:


GET

POST

PUT

DELETE