Microservices PDF
Microservices PDF
Microservice Architecture
Microservice Architecture is a Service Oriented Architecture. In the microservice architecture, there are a large number of
microservices.
By combining all the microservices, it constructs a big service. In the microservice architecture, all the services
communicate with each other.
Microservices
Microservice helps in breaking the application and build a logically independent smaller applications. For example, we can
build a cloud application with the help of Amazon AWS with minimum efforts.
each microservice has its own business layer and database. If we change in one microservice, it does not affect the other services.
These services communicate with each other by using lightweight protocols such as HTTP or REST or messaging protocols.
Principles of Microservices
The single responsibility principle states that a class or a module in a program should have only one responsibility. Any
microservice cannot serve more than one responsibility, at a time.
Microservice never restrict itself from accepting appropriate technology stack or database. The stack or database is most
suitable for solving the business purpose
Isolated Failure
The large application can remain mostly unaffected by the failure of a single module. It is possible that a service can fail at any time.
So, it is important to detect failure quickly, if possible, automatically restore failure.
Infrastructure Automation
The infrastructure automation is the process of scripting environments. With the help of scripting environment, we can apply the
same configuration to a single node or thousands of nodes. It is also known as configuration management, scripted infrastructures,
and system configuration management.
Deploy independently
Microservices are platform agnostic. It means we can design and deploy them independently without affecting the other services.
Microservices Monitoring
Monitoring is the control system of the microservices. As the microservices are more complex and harder to understand its
performance and troubleshoot the problems. Given the vivid changes to software delivery, it is required to monitor the
service.
○ Hystrix dashboard
○ Eureka admin dashboard
○ Spring boot admin dashboard
Components of Microservices
Spring Cloud Config Server provides the HTTP resource-based API for external configuration in the distributed system. We
can enable the Spring Cloud Config Server by using the annotation @EnableConfigServer.
Netflix Eureka Naming Server
Netflix Eureka Server is a discovery server. It provides the REST interface to the outside for communicating with it. A
microservice after coming up, register itself as a discovery client. The Eureka server also has another software module
called Eureka Client. Eureka client interacts with the Eureka server for service discovery. The Eureka client also balances the
client requests.
Hystrix Server
Hystrix server acts as a fault-tolerance robust system. It is used to avoid complete failure of an application. It does this by
using the Circuit Breaker mechanism. If the application is running without any issue, the circuit remains closed. If there is
an error encountered in the application, the Hystrix Server opens the circuit. The Hystrix server stops the further request to
calling service. It provides a highly robust system.
Netflix Zuul API Gateway Server
Netflix Zuul Server is a gateway server from where all the client request has passed through. It acts as a unified interface to
a client. It also has an inbuilt load balancer to load the balance of all incoming request from the client.
Zipkin Distributed Server
Zipkin is an open-source project m project. That provides a mechanism for sending, receiving, and visualization traces.
Port numbers
Application
Port
Spring Cloud Config Server
8888
8761
8765
9411
Setting up Spring Cloud Config Server
Step 2: Choose the Spring Boot version 2.2.0 M6 or higher version. Do not choose the snapshot version.
Step 5: Add the Spring Boot DevTools and Config Server dependencies.
Step 6: Click on Generate the project button. A zip file will download, extract it in the hard disk.
Step 7: Now, open the eclipse. Import the downloaded maven project. It will download the required files.
In the next step, we will create a simple Git repository and configure the spring cloud config server to pick up the values
from the particular Git repository. We need to install the local Git.
Step 3: Now move to the spring-cloud-config-server project and add a link to the specific folder.
The command commits any file we have added with the git add command and also commits any files we have changed
since then.
1. git add -A
Now execute the command to commit the changes in the repository. It records or snapshots the file permanently in the
version history.
Example
In this spring boot microservices example, we will be creating Top Sports Brands application which will be having 3
services:-
1. Eureka Service– This Service will register every microservice and then the client microservice will look up the
Eureka server to get a dependent microservice to get the job done.This Eureka Server is owned by Netflix and in
this, Spring Cloud offers a declarative way to register and invoke services by Java annotation.
2. Item Catalog Service – This service will generate the list of Sports brands which are popular in the market.
3. Edge Service – It is similar to the standalone Item service created in Bootiful Development with Spring Boot and
Angular. However, it will have fallback capabilities which prevent the client from receiving an HTTP error when the
service is not available
Spring Boot Microservices: Creating a Eureka Service
To begin with, create a EurekaServer Spring Starter Project in Eclipse IDE. Click on Spring Starter Project and click on
Next.
continuee
Name your Spring Starter Project as EurekaServer and other Information will be filled automatically.
Note:- Make sure your Internet is connected otherwise it will show an error.
continue
Now, select Eureka Server as a dependency and click on Finish.
continue
Now, modify EurekaServer/src/main/resources/application.properties file to add a port number and disable
registration.
server.port=8761
eureka.client.register-with-eureka=false
continue
Open EurekaServer/src/main/java/com/example/EurekaServiceApplication.java and add @EnableEurekaServer
above @SpringBootApplication.
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
This annotation will configure a registry that will allow other applications to communicate.
To start the Application: Right Click on the Project –> Run As –> Click on “Spring Boot App“
After it starts, you should be able to open http://localhost:8761 and see there are no
services available.
Now open http://localhost:8761. Here Spring Eureka Server will open and will show no service will be running.
Spring Boot Microservices: Creating an Item Catalog Service
Again create a new project. Use Item-catalog-service for the artifact name and click on Next.
dependencies
Add the following dependencies:
Click on Finish.
Now, create an Item entity, toItemCatalogServiceApplication.java . The code below assumes you’re putting all classes in the same file.
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
class Item {
this.name = name;
}
@Id
@GeneratedValue
@RepositoryRestResource
@Component
ItemInitializer(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
@Override
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
i
mport org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Component;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.stream.Stream;
Add an application name in item-catalog-service/src/main/resources/application.properties file to display in the Eureka service, and set the port to 8088.
server.port=8088
spring.application.name=item-catalog-service
Click on File –> New –> Other –> File and add the below code in this file and save it.
eureka.instance.hostname=${vcap.application.uris[0]:localhost}
eureka.instance.nonSecurePort=80
eureka.instance.metadataMap.instanceId=${vcap.application.instance_id:${spring.application.name}:${sprin
g.application.instance_id:${server.port}}}
eureka.instance.leaseRenewalIntervalInSeconds = 5
eureka.client.region = default
eureka.client.registryFetchIntervalSeconds = 5
eureka.client.serviceUrl.defaultZone=${vcap.services.pwa-eureka-service.credentials.uri}/eu
Now, to start the Application:
Right Click on Project –> Run As –> Click on “Spring Boot App“
Note: In case of error try this step: Right Click on the Project –> Run As –> Click on “Maven Build“
Now open http://localhost:8761. Here you will see Item Catalog service will be running.
You will see the list of items from the catalog service.
Open http://localhost:8088/items