M3 _ P1 - Introduction to Microservices Using Spring Boot
M3 _ P1 - Introduction to Microservices Using Spring Boot
especially microservices.
● When building microservices or any other application, developers need
settings, or even defining how the application will interact with different
services.
you don’t have to worry about many details right from the start.
that can run independently without relying on an external web server like
Apache Tomcat. You can package your application as a Java archive file (JAR)
applications in production.
password).
● Spring Boot makes configuration simple and flexible by allowing you to define
it.
Example: application.properties Example: application.yml
server.port=8080 server:
spring.datasource.username=root spring:
spring.datasource.password=secret datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: secret
2. Environment Variables
● Example:
export SERVER_PORT=8081
3. Command-Line Arguments:
● Example:
auto-configuration.
○ When you create a new Spring Boot application, you don’t need to manually
settings) yourself.
to your project, Spring Boot will automatically configure a DataSource for you.
● Pre-set Defaults: Spring Boot provides default configurations that
you.
specific configuration in those files, Spring Boot will use that instead of
the defaults.
● Example of Auto-Configuration:
○ Let’s say you add Spring Data JPA (for database interaction) and H2
Spring Boot.
● It represents the container that holds and manages all the Spring Beans and
configurations.
○ When your application runs, Spring Boot automatically creates these beans and
○ For example, a Controller or Service in a Spring Boot application is a bean that Spring
manages.
What is ApplicationContext?
● The ApplicationContext is the "heart" of the Spring Framework, and it manages all
● When your application starts, the Spring ApplicationContext is created, and it:
○ Application Lifecycle: The context manages the lifecycle of the beans, ensuring
● This means that Spring Boot will automatically provide the required beans
● These are pre-built configurations that allow you to get started quickly with
various components like Spring Data JPA, Spring Security, or Spring Web.
Embedded Servers:
● Spring Boot comes with embedded servers like Tomcat or Jetty, so you don’t
● This makes running and testing your application locally very easy.
Spring Boot DevTools:
● Spring Boot offers a special module called DevTools, which helps developers with hot
Actuator:
● Spring Boot Actuator provides a set of built-in endpoints to monitor and manage
your application, such as /health for checking if your service is running properly
etc, etc.
● You have many spring boot applications and all the spring boot
application has been deployed into many different servers and you
Address Service, Student Service wants to connect to the Course Service, and
wants to get some course-related data then how will these servers communicate
● We will simply make a REST call and all these servers will communicate with
● But the real challenging part is when a server wants to connect to another server
then before this server connects to this server it needs to know the IP address,
and it needs to know the port number where this particular application is running
in the server.
● Don’t you think managing the IP and the server URL will be critical?
we used to remember the server IP and the port but now your
handling that?
● And for that Spring Cloud is providing us with Service Discovery and
● Now let’s introduce our Discovery Service. Now what this discovery service will
do now whenever Service-A and Service-B want to communicate with each other
then whenever we are starting our Microservices we’ll be registering them with
Discovery Service.
● And this discovery service right now will know what is the IP and port number of
Discovery Service.
● So it is one central location where we’ll be managing our host and the port
● This is basically called registration because all the services whenever they are
starting off they are registering themselves with the discovery service and now
the discovery service is maintaining all their information inside a particular map
and they are running in some port number and some IP address.
● Now the load balancer once get the request, it is gonna do a query with the
discovery service that, hey, can you tell me what instances are there for Service-
B?
● Now the load balancer finds out that there are this many instances available
● Now Load Balancer is going to dispatch to one of the servers by looking into
Service Registry.
● It can take all four instances of Service-B and whoever has less load then to
● Because every server, when you want some data from another server, it needs to
connect to them, and in order to connect to them it needs to know the IP and the
server address of that and it will be a really critical job to handle all the IP and the
server ports where you have thousands of servers where you have split your one
servers
Types of Service Discovery
● Point to Remember:
○ Eureka Server is a key component of the Netflix OSS stack and is widely used
each other, and Eureka Server helps manage this communication efficiently.
○ Eureka Server is a Service Registry that holds information about all the
Eureka Server by sending its metadata (like IP address, port, and service name).
Eureka Server to inform it that they are still available. If the heartbeat stops, the
Eureka Server assumes that the service is down and removes it from the registry.
● How Eureka Server Works:
acts as the Service Registry. This server will hold all the information about
the services.
Client. When the microservice starts, it registers itself with the Eureka
Server. This allows the Eureka Server to know the address of each service.
● Service Discovery: When one microservice (say Service A) needs to
communicate with another (Service B), it will ask the Eureka Server for
● Fault Tolerance: If a service goes down, Eureka detects this through the
heartbeat mechanism and removes the service from its registry, ensuring
it.
● Why Use Eureka?
○ When the Order Service needs to check item availability in the Inventory
Service, it asks Eureka Server for the location of the Inventory Service,
○ Key-Value Store: Consul includes a key-value store that can be used for
○ DNS and HTTP Interfaces: Consul supports both DNS and HTTP APIs for
coordination.
layer that sits between clients (users) and the various microservices.
● Its primary job is to handle requests from the clients and route them to the
appropriate microservices.
● It serves as the single entry point for all client requests, playing an essential
● How it works:
○ When a client sends a request to the API Gateway, the gateway examines
the Customer Service, while a request for orders (/orders) would be routed
for all services, which simplifies routing logic and makes it easier to
manage.
requirements.
○ Suppose you have three microservices: User Service, Product Service, and
Order Service.
○ The API Gateway routes the /users path to the User Service, /products to
○ Clients only need to know the single API Gateway endpoint instead of
● How it works:
○ When a client requests data that spans several microservices, the API
○ Instead of the client making multiple calls to different services, the API
Gateway makes the necessary service calls, gathers the results, and returns
interact with individual microservices. The API Gateway handles all the
○ The Order Service may store only the order data, so the API Gateway
needs to fetch customer details from the Customer Service and product
● Protocol translation is the process by which an API Gateway translates one protocol
into another, enabling services to interact seamlessly even if they use different
communication methods.
● How it works:
○ The API Gateway receives a request using one protocol (e.g., HTTP) and
translates it into the protocol used by the target microservice (e.g., gRPC).
○ After receiving the response from the service, the API Gateway converts it
HTTP to interact with services using more optimized protocols like gRPC.
interface.
protocols.
● Example:
● The API Gateway will handle the conversion from HTTP to gRPC and back,
allowing both the client and microservice to communicate without being aware
○ They can enforce rate limits to prevent clients from overwhelming the
● Caching:
○ API Gateways can cache responses from services to improve performance and