Microservice Spring Boot ?
Simple Microservices using spring boot
Benefits
- small
- single function
- isolated
Steps
- navigate to start.spring.io
- select maven project, 3, select spring version --> 2.6.4
- Change the group to --> com.hashnodehub
- Artifact id --> project name --> movie-catalog-service
- Set packaging and java version
- Add dependency --> spring --> web
- Generate Zip and import to spring and start using spring boot app.
Lets understand what is inside the project.
Once we have created and imported the maven project in the eclipse, we can run the maven install to install all the dependencies.
lets check pom.xml
since we have selected spring boot version to be 2.6.4 it is showing that in the starter-parent dependency.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
artifact --> project name
dependencies --> spring-boot-starter-web and test is present
- We have maven wrapper
- src/main/java
- src/main/resources -src/main/test -src -target --> this will contains the JAR
What is inside src/main/java
Package --> com.hashcodehub.moviecatalogservice
Class --> MovieCatalogServiceApplication
- main --> SpringApplication.run(MovieCatalogServiceApplication.class,args);
Simple example of microservice
- First Service --> rating-data-service --> this will give rating to each movie id
- Second Sercice --> movie-info-service --> from here we will get all the movie information such as name, descrition ,size etc
-Third Service --> catalog service --> this wil combine all the information into one-->
Example :
we want all the movies catalog with rating 4
we will make call to rating service --> to get list of movies with rating 4 now we will traverse throught the list and get all the movie information from movie-info-service --> and from there , we will create object of catalog.
by combining moviename , description , rating etc
form a list and return that list.
@GetMapping("/{userId}")
public List<CatalogItem> getCatalog(@PathVariable("userId") String userId) {
//Steps
//get list of rated movies --> movie id and rating
//for each movie id --> call the movie-info-service --> movieId and movieName
//take the movie name and the rating form the catalog item form the list of catalog
// and return
RestTemplate restTemplate= new RestTemplate();
List<Rating> ratings=Arrays.asList(
new Rating("1",5),
new Rating("2",4),
new Rating("3",5),
new Rating("4",5)
);
return ratings.stream().map(rating -> {
Movie movie= restTemplate.getForObject("http://localhost:8082/movies/" +rating.getMovieId(), Movie.class);
return new CatalogItem(movie.getMovieName(),"desc",rating.getRating());
}).collect(Collectors.toList());
}
How to create beans in springboot application ?
since beans are managed by spring container and it is a singleton container.\ this helps us in not creating a object everytime a call is being made.
Autowired --> is the consumer --> which is asking for a object Bean --> producer which is telling spring boot to create a new object and now who ever wants to use this object, spring will inject the same object everytime. Study --> Bean Annotation
@SpringBootApplication
public class MovieCatalogServiceApplication {
@Bean
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MovieCatalogServiceApplication.class, args);
}
}
how to use the bean which is created in MovieCatalogServiceApplication
do this in any of the classes
@Autowired
private RestTemplate restTemplate;
Synchronous Calls
usually normal calls that we made using RestTemplate we are making Synchronous calls.if we have to wait for the response in each case, if we wants to make Asynchronous calls we need to make the calls using WebClient.
Thread Issue
- each calls in springboot is unique thread and rest template is thread safe.
Security between microservices ?
Why Hardcoding URL's are bad ?
- Change require code updates.
- Dynamic url's in cloud --> when we deploy some service in the cloud,the url will not be same everytime and that is the issue since if we hardcoded it we have to make the code changes, everytime services are up.