Today we are going to create one Service Registry (Discovery client). before we go forward let's understand about service registry. What is a Service Registry?
A Service Registry is a centralized system or database that stores and manages information about services in a distributed computing environment. Service Registry is also a Service in Microservices Architecture.
Why do we need a Service Registry?
In a microservices architecture or any other distributed system, where software components are deployed independently and communicate over a network, It becomes very crucial to keep track of each service, and also becomes hard to find which service is available. So, we created a new Microservice which keeps track of all available services.
Use of Service Registry:
Service Registry behaves like a directory or catalog that allows to register themselves and discover other services within a system.
Service Registration:
When a service starts up, it registers itself with the Service Registry. This registration typically includes information such as the service's name, network location, endpoint URLs, and possibly metadata about the service.
Service Discovery:
Clients or other services can query the Service Registry to discover the locations and details of other services they need to interact with. This helps in locating and communicating with services dynamically, without hardcoding their addresses.
Health Checking:
Service Registries often include mechanisms for health checking, allowing services to periodically update their health status. This information helps in dynamic load balancing and fault tolerance by directing traffic away from unhealthy services.
Load Balancing:
By maintaining an updated list of available services and their health status, a Service Registry can be used in conjunction with load balancing strategies to distribute incoming requests among multiple instances of a service.
Dynamic Updates:
In dynamic environments, where services may scale up or down based on demand, a Service Registry helps in managing these changes. Clients can be informed of new service instances or retire obsolete ones in real time.
Popular tools for implementing Service Registries in microservices architectures include:
Consul: A tool for service discovery, configuration, and health checking.
Eureka: A service registry for resilient mid-tier load balancing and failover.
etcd: A distributed key-value store that can be used for service registration and discovery.
Zookeeper: A distributed coordination service that can be used for service registration and discovery.
Steps to create a Service Registry(Eureka):
1. Create a Eureka Server Project
go to Spring Initializer https://start.spring.io/ and create a Service Registry Project.
Add Eureka Server Dependency before generating the project.
Now Click on the generate button. It will download the project. Now Import that project to your workspace it will look as below.
Now we are going to add some details to the application.properties file under src/main/resources folder.
server.port=8761
spring.application.name=SERVICE-REGISTERY
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
we defined a port on which the service registry app will run.
we defined the name of the application
we have disabled the Eureka client so it would behave as a server.
Now need to add some properties to Microservices. Here I have created below Microservices 1. Product Service 2. Category Service
Add the below-mentioned details to application.properties for Product Service
spring.application.name=PRODUCT-SERVICE eureka.instance.client.serverUrl.defaultZone=http://localhost:8761/eureka/
Add the below-mentioned details to application.properties for Category Service
spring.application.name=CATEGORY-SERVICE
eureka.instance.client.serverUrl.defaultZone=http://localhost:8761/eureka/
Add the spring cloud version in the properties tab of pom.xml
<spring-cloud.version>2023.0.0</spring-cloud.version>
Add the below-mentioned dependencies to the pom.xml of both services.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
Add Cloud Dependency management for both services in to pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Now Run the Service Registry App as a Spring boot app. It will open in the browser as below.
After running the product service and category service it will show instances as below.
Comments