Java MicroServices
Learn Complete Java Microservices Below
If you are reading this page it means you are interested to learn microservices Lets dive in below
Java Microservices
Small autonomous service that work together - Sam NewMan
Micro services are basically services which are exposed by rest in addition to that Expose by rest Small deployable units Cloud enable
Exposed by REST API
Small deployeable UNITS
Cloud Enabled
Cloud enabled means In Below Example, Lets deploy microservice in produciton and scaleup and down.
All the microservices are cloud enabled means
In Below Example microservice1 has two instance
Microservice2 has 4 instance
Microservice3 has 1 instance
Based on the load we can increase or decrease the instance in production
This should not involve lot of configuraiton, we can create instance and pull down instance whithout having much problem.
That is what I mean by cloud enabled
In Microservices Further we will learn How to setup architechture then it would be dynamically adjust and bring new instance up take older instance down.
We will discuss about Microservice different challenges while building microservices
We will start problem no 1 : Bound context, 2. Configuration Management, 3. Dynamic Scale UP and Dynamic scale Down, 4. Visibility, 5. Pack of Cards
Ullam qui culpa officiis quo accusamus et numquam. Non dolor exercitationem expedita et rerum. In corporis delectus et magnam rerum. Et maxime natus sed aut temporibus dolor qui.
what do I mean earlier we have seen in Instead of building one big monolithic applicaiton, we have built 5 small microserices 10 or 20 or 1000 , how do you Identify the boundry of each microservices
How do you Identify what to do each of microservices
How would you decide what you should do each of microservices you should not do
For New Application its much more difficult , probably you don't have business knowledge to stablish right boundry between these microsercices
Deciding boundary of microservices is evaluation process , Its something get you right at first time which you need to play around it.
Try and fallow domain driven design, try to find right boundary for microservices based on knowledge with that in point of time.
As Important thing to keep, as keep gaining knowledge you should keep back knowledge to microservices into deciding what is right boundaries for these microservice
Challenge is configuration management lets look in details.
We said earlier we would have 5 microservices
We said we would have 5 or 50 microservices , these microservices would have multiple instances in each environment and there are multiple environement, like dev, test, stage and prod,
These Microservices would have multiple instances see below
Lets say there 10 microservices in 5 environement
Each Microservices has 50 instances , so we are taking about tons of configuraiton so that lots of work for operation Team to mentain.
third important challenge is dynamic scale up and dynamic scale down.
Stablishing the technology to do that, (ex kubernetes ) Load of Microservices will different on on different instances on different time
At Perticular time might be I need two instances of particular microservices of two, but letter at different point of time might I needing 10 instances of this microservicesso I should be able to bring new instances of microservices UP and bring down older instances of mcirservices when they are realy not needed all with dynamic load balancing beacause when there is one instance of microservices1, and 4 instance of mciroserce2 , I want to dist
I want to distribuite all instance of of microservices1 , If there are 4 instance of microservices2 comming up , I would want to insure that all new one being used full extend , so we need ability to dynamicly bring new instances and also distribute the load among the new instances
4th and most important challenge is visibility lets look in details.
If I say functionality is now distributed among 5 microservices and there is bug
How do you identify where the bug is , you have to centeralise log where you can go and find out what happen on perticular microservices on specific request shich microservices cause the problem
Not Just that we also need monitoring around these microservices because we have 100 of microservices we need to be able to identify which are microservices are down , you would want to autometically identify the the server, which they not have enough disk space all kind of this thing need to be automated.
We need great visibility into what happening to the microservices
Last but not the least Pack of cards see next section
If Not well desing microservices architecture , Its can be like pack of cards what do I mean
Its basically that I microservices architecture one microservices calling another calling another.
There would be the certain microservices fundamental of the whole things , If that microservices goes down then entire application go down, It's like pack of cards , you are building one on top of another and so on. There fore they can collapse very easily
There fore its very important to have fault tolerance into your microservice
We have seen in challenge below section in detail
Configuraiton Management : 100 of microservices has lot of environement , there are tons of configuraiton you would want to manage how would you simplify that
Dynamically Scale UP and Dynamically Down : Dynamically distribute the load among the active instance
Visibility : What happening around the simple microservice, A simple microservice involve multiple microservice , how do I determine which microservice cause for defect , how I would know all my microservice up and running ..
Pack of card : How would I prevent, Once microservices down taking down entire application, How I would Implement fault tolerance into my microservices
Introduction To Spring Cloud
Spring cloud provide various features under umbrella of spring cloud which help us to provide solutions the challenge we discussed
Spring cloud provide multiple project to solve the problem of distributed application.
Integrated various component
1. Eureka
2. Hystrix
3. Zull
Provide Centeralize configuration management
This enable microservices and infrastrucre related component
Challenge Discussed and Solution provide by Spring Cloud
Spring Cloud Config Server
Lets see in detail
We talk about Fact multiple microservices multiple environement for each of this microservices and multiple instance any of those environement, this would mean lot of configuration of these microservices that's operation team need to manage
Spring cloud config server provide an approach where you can store all the configuration for all the different environment of all the microservices in GIT repository , so you can store all the configuration for different environment of different microservices in just one place into centralize location
Spring cloud config server can be used to expose that configuration to all the microservices , this help us to keep all the configuraiton in one one place and make us to easy to maintain all the microservices configuration
Next challenge we talk about Dynamic scale up and scale down the solution in next section
Naming Server
Ribbon (Client side Load balancing)
Feign ( Easier Rest Client)
In Example you are looking at Microservice called CurrencyCalculationService which is talking to the CurrrencyExchangeService1 as you can see in the diagram there are multiple instances CurrencyExchangeService (currency-excahnge-service1, currency-exchange-service2 etc). It's possible that any point of time new instances can be added in or removed out.
We would want to currencyCalculationService to be able to distribute the load between all the instances of CurrencyExchangeService, we would want to able dynamically check what are the available instances of currencyExchangeService and make sure the load distributed among all of them The solution would be discussing , using NamingServer(Eureka) so all the instance of microservices would register with naming server.
So NamingServer(Eureka) has two Important Feature 1. ServiceRegistration(ServiceDisovery) so all microservices can register with microservices. 2. Service Discovery
NamingServer(Eureka)
1. Service REgistration
2. ServiceDiscovery.
In This example CurrencyCalculationService can ask to EurekaNamingServer hey NamingServer give me the current instance of CurrencyExchangeService and the NamingServer would provide provide those url of CurrencyExchangeService , this help to establish dynamic relationship with CurrencyCalculationService and instance of CurrencyExchangeService
Ribon
We will use Ribbon for client side load balancing that means CurrencyCalculationService will host Ribbon, It would make sure load equally distributed among the existing instances that gets from the naming server
Fiegn
We also use Feign in CurrencyCalculationService as a mechanism to write simple Restfull Client
The Next Challenge is Visibility and Monitoring see in next section
Zipking Distributed Tracing
NetFlix API Gateway
The Solution of Visibility and monitoring are Zipking Distributed Tracing Server , We would use spring cloud slauth to asign a id to request across multiple component , we would use zipking Distributed Tracing to trace request across multiple component
Important thing about microservices, Microservices have lots of common features for example logging , security analytics and etc. you would dont want to implement common feature in every microservices
API Gateway
API Gateway provide greate solution to this king of challenge , we will use netflix Zull API Gateway
Fault Tollrence
If Service is down, Hystrix helps us to configure default response
Hystrix
New Technology and Process Adaption
Its enable you to adapt new technology and process very easily
When we build application combination of microservice which can communicate with each other using simple message, each of these microservices can be built in different technology
In Typical monolith applicaiton we would not have that facility,
For Example : Microservice1 might be in java, Microservices2 might be in python , microservices might be in nodejs microservices4 might be written in cotlin tommarow might be language xyz which is realy doing well and which provide lot of benifit to you , you can easily create microservices in that specific languagee and also for the new microservice that we create we can bring new new processes as well
Dynamic Scalling
Other Important feature is dynamic scalling
Consider online shoping like amazon they realy don't have same amount of loads or same amount of traffic or same amount of users throught the year , specially during the holiday season load on the application will be alot and rest of the year there might be not such load . During the black friday might be the load huge amount of load ,
Another Example : Number of the mobile network user will increase during the peak hours (7 to 10 pm) every day rest of the day load will be less.
If your microservices cloud enabled then they can scale dynamically and you can procure hardware and release it dynamically as well
So you can scale up your application and scale them down based on the load because you are developing smaller microservices.
Faster Relase Cycles
Easer to relase microservices campare to monolithic applicaiton
This means that you can bring new feature faster to market, that is good feature in modern world.
Add dependencies
Feign Client
Create Proxy Interface and declare method
user proxy interface object call the method and retrive the desired microservice
Step1 : Add OpenFeign Dependency
API Gateway provide greate solution to this king of challenge , we will use netflix Zull API Gateway
Step2 - Create Proxy interface
If Service is down, Hystrix helps us to configure default response
Hystrix
Step3 - Enable Feign Client : on application class of (main class)
@EnableFeignClients
C
Step2 - Create Proxy interface
If Service is down, Hystrix helps us to configure default response
If Service is down, Hystrix helps us to configure default response
Hystrix
All the microservices would register in service registery(naming server)
Lets say Currency conversion microservices want to talk currency Exchange Microservice , It would ask to the services registry what are the addresses af the currency Exchange Microservices , service registry would return back to the currency conversion microservices then currency conversion microservice would send the request out to the currency exchange microservice
Step1 : Add Eureka server Dependency
you need to add eureka server which provide spring cloud discovery(spring-cloud-netflix-eureka-server)
Step2 - Add @EnableEurekaServer to main class
If Service is down, Hystrix helps us to configure default response
Step3 - configure properties file
step1 - give application
spring.applination.name=naming-server
step2 - give port number
server.port=8761
step3 - Make sure eureka server will not register with itself
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Step1 - Open Pom.xml of Currency Exchange and currency conversion microservice
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
Step1 - Load balance using naming server
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
create one more instance from currency-exchange server configuraiton
-Dserver.port=8001 check on pom.xml search spring-cloud-load balancer will be come up into class path brought by eureka client
Ribbon : earlier version of spring used ribbon for load balancing but now in 2.7.5 version spring use cloud load balancer to balance the load by eureka lcient
Important Point: If you are using feign and eureka client then load balancer coming as free to balance the load to test creat one more instance and instance automatic pic by eureka naming server
When you are developing microservices, microservices provide lot's of common feature,
Authentication
Autherization
Logging
Rate limiting
Where you will implement all common feature, the solution is api gateway
older spring cloud apigateway was - zull provided by netflix, but now new new version zull is no more supported
Now Recommended one api gateway : spring cloud gateway
Add dependencies : We would want api gateway to connect eureka ( there fore will add eureka discovery client), This is rest best service to locating purpose of load balancing and failure of middle tier server
Add dependencies : 1- eureka discovery cleint( for load balancing) we added in currency exchange and currency conversion mciroserice
Add dependencies : 2- gateway dependencies (not zull, its provided by spring cloud)
We would want to connect api gateway connect to eureka discovery client
properties file : enable service locator ture : spring.cloud.gateway.discovery.locator.enabled=true
2023 Evelyn. All Rights Reserved.