Microservices Final
Microservices Final
1. INTRODUCTION
Microservice is an architecture style which says decompose big application into smaller
services later communicate these services together to form larger business application.
Microservices are autonomous, self- contained, and independently deployable.
Software
1. JDK 1.8
2. Spring Tool Suite 3.7.2 (STS)
3. Maven 3.x/ Gradle
4. Spring Framework 4.2.6. RELEASE or above
5. Spring Boot
6. RabbitMQ / Kafka / Active MQ
7. Git /SVN
8. Spring cloud
9. MySQL/Mongo-DB
= -o
Microservices are not invented; rather It was evaluated from the previous
architecture styles.
Offer Engine
Personalized
Engine
Personalized
Engine
Core Legacy Core Legacy
System System
Offer
Engine
Microservices are not invented rather many organizations such as Netflix, Amazon, and
eBay successfully used the divide-and–conquer technique to functionally partition their
monolithic application into smaller atomic units, each performing a single function.
In below diagram, each layer holds all three business capabilities pertaining to that layer.
The presentation layer has web components of all the three modules, the business layer has
business components of all the three modules, and the database hosts tables of all the three
modules. In most cases, layers are physically spreadable, whereas modules within a
layer are hardwired.
What is Microservices ?
Ans; Microservice is an architectural style which says decompose big project into smaller
microservices which are autonomous, self- contained, loosely coupled, independently
deployable and can contains its own presentation layer, business layer and DAO layer later
they communicate with each other.
Principles of microservices
The below principles are a “must have” when designing and developing microservices:
1. Single microservice per single business responsibility.
2. Microservices are independently deployable. Hence they bundle all dependencies,
including library dependencies, and execution environments such as web servers and
containers and databases.
One of the major differences between Microservices and SOA is in their level of decomposition.
While most SOA implementations provide service-level decomposition, microservices go further
and decompose till execution environment.
In monolithic developments, we build a WAR or an EAR, then deploy it into a JEE application
server such as JBoss, WebLogic, WebSphere, and so on. We may deploy multiple applications
into the same JEE server. In microservices approach, each microservice will be built as a Fat Jar
by using boot, Which contains all dependencies jincluding weh servers and run as a standalone
Java process.
Characteristics of microservices
1) Services are first class citizens
Each Micro service has its own database, http listener such as tomcat or jetty.
The development phase is automated by using version control tools such as Git together
with continuous Integration (CI) tools such as Jenkins.
The testing phase will be automated by using testing tools such as selenium.
Automated deployments are handled by using DevOps.
Infrastructure provisioning is done through Cloud.
5) Microservices ecosystem
Microservices implementations have a supporting ecosystem including DevOps,
Centralized log management, Service registry, API gateways, Service routing, Flow
control mechanism.
Microservices benefits
3) Selective scaling
A monolithic application, packaged as a single WAR or an EAR, can only be scaled
as a whole. In Microservices, each service could be independently scaled up or down
depending on scalability requirement. As scalability can be selectively applied at each
service, the cost of scaling is comparatively less with the microservices approach.
4) Allowing substitution
Microservices are self-contained, independent deployment modules enabling the
substitution jof one microservice with another similar microservice. Many large
enterprises follow buy-versus-build policies to implement software systems. A
common scenario is to build most of the functions in house and buy certain niche
capabilities from specialists outside.
5) Supporting Cloud
6) Enabling Devops
Microservices are one of the key enablers of DevOps. DevOps is widely adopted as a
practice in many enterprises, primarily to increase the speed of delivery and agility.
DevOps advocates having agile development, high-velocity release cycles, automatic
testing, and automated deployment
We observed similar aspects in microservices as well. Then how microservices differ from SOA?
One of the major differences between Microservices and SOA is in their level of abstraction.
While most SOA implementations provide service-level abstraction, Microservices go further
and abstract the realization and execution environment i.e., in SOA development, we may deploy
multiple services into the same JEE container. In the microservices approach, each microservice
will be built as a fat jar, embedding all dependencies including web containers and run as a
standalone Java process.
In case of Legacy modernization, the services are built and deployed in the ESB layer connecting
to backend systems using ESB adapters. In these cases, microservices are different from SOA.
3) Twitter
When Twitter experienced growth in its user base, they went through an architecture-
refactoring cycle. with this refactoring, Twitter moved away from a typical web
application to an API-based event driven code. Twitter uses Scale and Java to develop
microservices with polyglot persistence.
4) Uber
When Uber expanded their business from one city to multiple cities, the challenges
started. Uber then moved to microservice based architecture by breaking the system into
smaller independent units. Each module was given to different teams and empowered
them to choose their language, framework, and database. Uber has many microservices
deployed in their ecosystem using REST.
Traditionally a war was explicitly created and deployed on a Tomcat server. But
microservices need to develop services as executables, self-contained JAR files with an
embedded HTTP listener (such as tomcat of jetty). Spring boot is a tool to develop such
kinds of services i.e., Spring Boot enables microservices development by packaging all the
required runtime dependencies in a executable fat Jar file.
2. DESIGNING MICROSERVICES
Microservices have gained enormous popularity in recent years. They have evolved as the
preferred choice of architects, putting SOA into the backyards. While acknowledging the fact
that microservices are a vehicle for developing scalable cloud native systems, successful
microservices need to be carefully designed to avoid catastrophes. Hence number of factors are
to be considered when designing microservices, as detailed in the following sections.
The Sensor data service has two logical end points: read and write
Communication styles
Communication between microservices can be designed either in synchronous or asynchronous
styles.
Synchronous style
The following diagram shows an example of synchronous (request/response) style service:
In synchronous communication, the http listener such as tomcat or jetty of jboss, etc is needed
but not messaging listener. When a caller requests a service, it passes the required information
and waits for a response.
Advantages:
1) No messaging server overhead.
2) The error will be propagated back to the caller immediately.
Dis advantages:
1) The caller has to wait until the request has been processed.
2) Adds hard dependencies (tight coupling) between Microservices i.e., if one service
in the chain fails, then the entire service chain will fail.
Asynchronous style
The following diagram is a service is a service designed to accept an asynchronous message as
input, and send the response asynchronously for others to consume:
The asynchronous style is based on reactive event loop semantics which decouple microservices.
Advantages:
1. Decouple Microservices
2. Higher level of scalability because of services are independent. Hence if there is a
slowdown in one of the services, it will not impact the entire chain.
Dis advantages:
1. It has a dependency to an external messaging listener.
2. It is complex to handle the fault tolerance of a messaging listener.
This is perfect scenario for synchronous communication. This can also be modeled in an
asynchronous style by pushing a message to an input queue, wait and read response from the
output queue. However, though we use asynchronous messaging, the user is still blocked for the
entire duration of the query. Hence no advantage of using asynchronous style.
Another use case is user clicking on a UI to search hotels, Which is depicted in the following
diagram:
When the system receives this request, it calculates the customer ranking, gets offers based on
the destination, gets recommendations based on customer preferences, and optimizes the prices
based on customer values and revenue factors, and so on. In this case, we have an opportunity to
do many of these activities in parallel so that we can aggregate all these results before presenting
them to the customer. As shown in the preceding diagram, virtually any computational logic
could be plugged in to the search pipeline listening to the IN queue. An effective approach in this
case is to start with a synchronous request response, and refactor later to introduce an
asynchronous style when there is value in doing that.
The following example shows a fully asynchronous style of service interactions:
When booking is successful, it sends a message to the customer’s e-mail address, sends a
message to the hotel’s booking system, updates the cached inventory, updates the loyalty points
system, prepares an invoice, and perhaps more. Instead of pushing the user into a long wait state,
a better approach is to break the service into pieces. Let the user wait till a booking record is
created by the Booking service. On successful completion, a booking event will be published,
and return a confirmation message back to the user Subsequently, all other activities will happen
in parallel, asynchronously.
Conclusion:
In general, an asynchronous style is always better in the microservices world, but identifying the
right pattern should be purely based on merits. If there are no merits in modeling a
communication in an asynchronous style, then use the synchronous style till we find an
appealing case.
Orchestration of Microservices
Composability (means controlling) is one of the service design principles. In the SOA world,
ESBs are responsible for composing a set of fine-grained services i.e., In the SOA world, ESBs
play the role of orchestration.
Microservices are autonomous. This means that all required components to complete their
function should be within the service. This includes the database, orchestration of its internal
services, state management, and so on. But in reality, microservices may need to talk with other
microservices to fulfill their function.
The following approach is preferred to connect multiple microservices together:
In case of cloud infrastructure, the developers need to worry about where the services are
running. Developers may not even think about capacity planning. Services will be deployed in a
compute cloud. Based on the infrastructure availability and the nature of the service, the
infrastructure self- manages deployments.
Shared Libraries
Sometimes code and libraries may be duplicated in order to adhere to autonomous and self-
contained principle.
The eligibility for a flight upgrade will be checked at the time of check-in as well as when
boarding. This was the trade-off between overheads in communication versus duplicating
libraries in multiple services:
1. It may be easy to duplicate code or shared library but downside of this approach is that in
case of a bug or an enhancement on the shared library, it has to be upgraded in more than
one place.
2. An alternative option of developing the shared library as another microservice itself
needs careful analysis. If it is not qualified as a microservice from the business capability
point of view, then it may add more complexity than its usefulness.
3. MICROSERVICES CHALLENGES
In this chapter, we will review some of the challenges with microservices, and how to address
them for a successful microservice development.
Infrastructure provisioning
With many Microservices running, manual development could lead to significant operationa
overheads and the chances of errors are high.
To address this challenge, Microservices should use elastic cloud-like infrastructure which can
automatically provision VMs or containers, automatically deploy applications, adjust traffic
flows, replicate new version to all instances, and gracefully phase out older versions. The
automation also takes care of scaling up elastically by adding containers or VMs on demand,
and scaling down when the load falls below threshold.
Data Islands
Microservices use their own local transactional store, which is used for their own transactional
purposes.
In the preceding diagram, Hotel search is expected to have high transaction volume hence
preferred to use Elastic search. The Hotel booking needs more ACID transactions hence
preferred to use MySQL. That means different Microservices may use different types of
databases which leads data islands.
Organization culture
One of the biggest challenges in microservices implementation is the organization culture.
Organization following a waterfall development or heavyweight release management processes
with infrequent release cycles are a challenge for microservices development. Insufficient
automation is also a challenge for microservices deployment.
To harness the speed of delivery of microservices, the organization should adopt Agile
development processes, continuous integration, automated QA checks, automated delivery
pipelines, automated deployments, and automatic infrastructure provisioning.
Core capabilities
The core capabilities are explained as follows:
Service listeners (HTTP/Message): If microservices are enabled for a HTTP- based
service endpoint, then the HTTP listener is embedded within the microservices, thereby
eliminating the need to have any external application server requirement.
If the micro services is based on asynchronous communication, then instead of an HTTP
listener, a message listener is started. Spring Boot and Spring Cloud Streams provide this
capability.
Storage capability: The microservices have some kind of storage mechanisms to store
state or transactional data pertaining to the business capability. The storage Could be
either a physical storage (RDBMS such as MySQL: NoSQL such as Hadoop, Cassandra,
Neo 4j, Elasticsearch, and so on), or it could be an in- memory store (cache like Ehcache,
Redis, data grids like HaZelcast, Infinispan, and so on)
Business capability definition: This is the core of microservices, where the business
logic is implemented. This could be implemented in any applicable language such as
java, Scala, Conjure, Erlang, and so on. All required business logic to fulfill the function
will be embedded within the microservices themselves.
Event Sourcing: Microservices send out state changes to the external world without
really worrying about the targeted consumers of these events. These events could be
consumed by other micro services, audit services, replication services, or external
applications, and the like. This allows other microservices and applications to respond to
state changes.
API gateway: The API gateway provides a level of indirection by either proxying service
endpoints or composing multiple service endpoints. There are many API gateways
available in the market. Spring Cloud Zuul, Mashery, Apigee, and 3scale are some
examples of the API gateway providers.
User interfaces: Generally, use interfaces are also part of microservices for users to
interact with the business capabilities realized by the microservices. These could be
implemented in any technology.
Infrastructure capabilities
Certain infrastructure capabilities are required for a successful deployment, and managing
large scale microservices. When deploying microservices at scale, not having proper
infrastructure capabilities can challenging, and can lead to failures:
Supporting capabilities
Supporting capabilities are not directly linked to microservices, but they are essential
for large scale microservices development:
Software defined load balancer: The load balancer should be smart enough to
understand the changes in the deployment topology, and respond accordingly. This
moves away from the traditional approach of configuring static IP addresses, domain
aliases, or cluster addresses in the load balancer. When new servers are added to the
environment, it should automatically detect this, and include them in the logical cluster
by avoiding any manual interactions. Similarly, if a service instance is unavailable, it
should take it out from the load balancer. A combination of ribbon, Eureka, and Zuul
provide this capability in spring cloud Netflix.
Central log management: A capability is required to centralize all logs emitted by
service instances with the correlation IDs. This helps in debugging, identifying
performance bottlenecks, and predictive analysis. The result of this is fed back into the
life cycle manager to the corrective actions.
Service registry: A service registry provides a runtime environment for services to
automatically publish their availability at runtime. A registry will be a good source of
information to understand the services topology at any point. Eureka from spring Cloud,
Zookeeper, and Etcd are some of the service registry tools available.
Security service: A distributed microservices ecosystem requires a central server for
managing service security. This includes service authentication and token services.
OAuth2- based services are widely used for microservices security. Spring Security and
Spring Security OAuth are good candidates for building this capability.
Service configuration: All service configurations should be externalized as discussed in
the Twelve- Factor application principles. A central service for all configurations is a
good choice. Spring cloud config server, and Archaius are out-of-the- box configuration
servers.
Testing tools (anti-fragile, RUM, and so on): Netflix uses simian Army for anti-fragile
testing. Matured services need consistent challenges to see the reliability of the services,
and how good fallback mechanisms are. Simian Army components create various error
scenarios to explore the behavior of the system under failure scenarios.
Monitoring and dashboards: Microservices also require a strong monitoring
mechanism. This is not just at the infrastructure. level monitoring but also at the service
level. Spring cloud Netflix Turbine, Hysterix Dashboard, and the like provide service
level information. End-to-end monitoring tools like AppDynamic, New Relic, dynatrace,
and other tools like statd, sensu, and spigot could add value to microservices monitoring.
Dependency and management: We also need tools to discover runtime topologies,
service dependencies, and to manage configurable items. A graph-based CMOB is the
most obvious tool to manage these scenarios.
Data lake: We need a mechanism to combine data stored in different microservices, and
perform near real-time analytics. A data lake is a good choice for achieving this. Data
ingestion tools like Spring Cloud Data flow, flume, and kafka are used to consume data.
HDFS, Cassandra, and the like are used for storing data.
Reliable messaging: If the communication is asynchronous, we may need a reliable
messaging infrastructure service such as RabbitMQ or any other reliable messaging
service. Cloud messaging of messaging as a service is a popular choice in Internet scale
message-based service endpoints.
Process and governance capabilities
The last piece in the puzzle is the process and governance capabilities that are required
for microservices:
DevOps: The key to successful implementation of microservices is to adopt DevOps.
DevOps compliment microservices development by supporting Agile development, high
velocity delivery, automation, and better change management.
DevOps tools: DevOps tools for Agile development, continuous integration, continuous
delivery, and continuous deployment are essential for successful delivery of
microservices. A lot of emphasis is required on automated functioning, real user testing,
synthetic testing, integration, release, and performance testing.
Microservices repository: A micro services repository is where the versioned binaries
of microservices are placed. These could be a simple Nexus repository or a container
repository such as a Docker registry.
Microservice documentation: It is important to have all microservices properly
documented. Swagger or API Blueprint are helpful in achieving good microservices
documentation.
Reference architecture and libraries: The reference architecture provides a blueprint at
the organization level to ensure that the services are developed according to certain
standards and guidelines in a consistent manner. Many of these could then be translated
to a number of reusable libraries that enforce service development philosophies.
We are able to implement for microservices such as fair, search, booking and check-in. In order
to test the application, there is a website application developed using spring MVC with
Thymeleaf templates. (needed for HTML pages). The asynchronous messaging is implemented
with the help of Rabbit MQ. In this implementation, the Oracle database will be used with
separate schema for each microservice. The code is section demonstrates all the capabilities
highlighted in green colour above.
The following steps are used to set up PSS micro services project