In this tutorial we will learn how to create registry/ discovery server using Eureka and Spring boot. We will register our Rest microservice with this registry server which can be discovered by client by connecting to registry server.
If we don't user registry server then we need host name and endpoint details to access a service. In today's era most of the services are deployed in cloud and using cloud features like auto-scaling where network addresses are updated dynamically and we can't use a simple network address for that. Now when we use registry server, then such microservice are registering themselves with registry server. And since clients are access these services using registry server they don't need to bother of network address. They just need to connect to registry server and invoke the desired operations using registered service ID.
Enable Eureka server on main class
Required configuration
Enable discovery client
Required configuration
URL: http://localhost:8089/
Now start the microservice and refresh the discovery service URL. In below screen you can see now it is showing the service instance as registered applications.
https://github.com/thetechnojournals/microservices/tree/master/discovery-server
What is registry/ discovery server
We can assume registry or discovery server as a central place where we can find all our microservices using their registered IDs with discovery server. Microservices register themselves in registry server and client applications access them using a single registry or discovery server. It is similar to a phone directory but provides many other useful features.If we don't user registry server then we need host name and endpoint details to access a service. In today's era most of the services are deployed in cloud and using cloud features like auto-scaling where network addresses are updated dynamically and we can't use a simple network address for that. Now when we use registry server, then such microservice are registering themselves with registry server. And since clients are access these services using registry server they don't need to bother of network address. They just need to connect to registry server and invoke the desired operations using registered service ID.
Creating registry/ discovery server
In below steps we will see the implementation to create our registry server.- Maven dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
@EnableEurekaServer @SpringBootApplication public class DiscoveryServerApplication { public static void main(String[] args) { SpringApplication.run(DiscoveryServerApplication.class, args); } }
spring.application.name: discovery-server server.port: 8089 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false
Service registration
Here I am using spring boot microservice to register with our discovery server. Below are the steps to register a service.- Maven dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
@EnableDiscoveryClient @SpringBootApplication public class UserMgmtServiceApplication { public static void main(String[] args) { SpringApplication.run(UserMgmtServiceApplication.class, args); } }
eureka: client: serviceUrl: defaultZone: http://localhost:8089/eureka
Microservice (User management)
This microservice is created for demonstration purpose. This service uses spring cloud config and mongo DB which will be required to run this microservice but you can create your own code without these dependencies. Below is the POST link for complete tutorial and source code.
Accessing Eureka server dashboard
Now start registry server by executing below command.mvn spring-boot:runAccess below URL in browser and you will see the similar screen as given below. Currently there no services registered.
URL: http://localhost:8089/
Now start the microservice and refresh the discovery service URL. In below screen you can see now it is showing the service instance as registered applications.
Source code
Source code is available at below Git location.https://github.com/thetechnojournals/microservices/tree/master/discovery-server
Comments
Post a Comment