100% found this document useful (2 votes)
234 views

Microservices PDF

1. Create a EurekaServer project and add the @EnableEurekaServer annotation to register it as a Eureka service registry. 2. Create an ItemCatalogService project that adds entity and repository classes for items. It also registers with Eureka. 3. Create an EdgeService project that calls the ItemCatalogService, handles routing and fallback scenarios. It also registers with Eureka.

Uploaded by

Tejasri 001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
234 views

Microservices PDF

1. Create a EurekaServer project and add the @EnableEurekaServer annotation to register it as a Eureka service registry. 2. Create an ItemCatalogService project that adds entity and repository classes for items. It also registers with Eureka. 3. Create an EdgeService project that calls the ItemCatalogService, handles routing and fallback scenarios. It also registers with Eureka.

Uploaded by

Tejasri 001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Microservices

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

There are the following principles of Microservices:

○ Single Responsibility principle


○ Modelled around business domain
○ Isolate Failure
○ Infrastructure automation
○ Deploy independently
Single Responsibility Principle

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.

Modeled around business domain

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.

There are three monitoring tools are as follows:

○ Hystrix dashboard
○ Eureka admin dashboard
○ Spring boot admin dashboard
Components of Microservices

There are the following components of microservices:

○ Spring Cloud Config Server


○ Netflix Eureka Naming Server
○ Hystrix Server
○ Netflix ZuulAPI Gateway Server
○ Netflix Ribbon
○ Zipkin Distributed Tracing Server
Spring Cloud Config Server

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

Netflix Eureka Naming Server

8761

Netflix Zuul API gateway Server

8765

Zipkin distributed Tracing Server

9411
Setting up Spring Cloud Config Server

Step 1: Create a Maven project using Spring Initializr https://start.spring.io/

Step 2: Choose the Spring Boot version 2.2.0 M6 or higher version. Do not choose the snapshot version.

Step 3: Provide the Group name. In our case, com.javatpoint.microservices.

Step 4: Provide the Artifact id. We have provided spring-cloud-config-server

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.

1. Right-click on the spring-cloud-config-server project.


2. Click on Build Path->Configure Build Path…
3. Select the Source tab.
4. Click on Link Source and browse the folder git-localconfig-repo.
5. Right click on the folder-> New -> Other -> File -> Next -> Provide the file name: limits-service-properties-> Finish.
6. Now write the following code in the properties file:
1. limits-service.minimum=8
2. limits-service.maximum=888
Step 4: Configure the user name and user email:

1. git config -global user.email [email protected]


2. git config -global user.name "abc"

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:

● Actuator: features to help you monitor and manage your application


● Eureka Discovery: for service registration
● JPA: to save/retrieve data
● H2: an in-memory database
● Rest Repositories: to expose JPA repositories as REST endpoints
● Web: Spring MVC and embedded Tomcat
● DevTools: to auto-reload the application when files change
● Lombok: to reduce boilerplate code

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 {

public Item(String name) {

this.name = name;

}
@Id

@GeneratedValue

private Long id;

private String name;

@RepositoryRestResource

interface ItemRepository extends JpaRepository<Item, Long> {}

@Component

class ItemInitializer implements CommandLineRunner {

private final ItemRepository ItemRepository;

ItemInitializer(ItemRepository itemRepository) {

this.itemRepository = itemRepository;

@Override

public void run(String... args) throws Exception {

Stream.of(""Lining", "PUMA", "Bad Boy", "Air Jordan", "Nike", "Adidas", "Reebok")

.forEach(item -> itemRepository.save(new Item(item)));


If you’re using an editor that doesn’t auto-import classes, here’s the list of imports needed at the top of
ItemCatalogServiceApplication.java.

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

Now, Create the Cloud Properties file

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

You might also like