DEV Community

rajubora
rajubora

Posted on

Service Discovery :

In a microservices architecture, a discovery Server is a component that helps microservices locate and communicate with each other within the distributed system. Since microservices contains
number of services so ,for storing all the services together at a location we have to use service registry at that point . Commonly used service registries include tools like Eureka, Consul, and ZooKeeper.
By using Service discovery and service registry, microservices can achieve better scalability, flexibiliy.

1) In Springboot based application we can use Service Discovery(Service Registry) as Server by adding a dependency in pom.xml file :

<dependency>
   <groupId>org.springframework.cloud</groupId> 
   <artifactId>spring-cloud-starter-netflix-eureka- 
    server</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

After adding dependency the next step is to add the scripts in application.yml or property file :

                  eureka:
                     instance:
                           hostname: localhost
                      client:
                            register-with-eureka: false
                            fetch-registry: false
                  sever:
                      port: 8761
Enter fullscreen mode Exit fullscreen mode

by adding above scripts the service will acts as a Eureka server where we can store all the client services .

2) For Using Service Discovery as client we have to add another dependency in pom.xml file:

  <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka- 
       client</artifactId>
  </dependency>
Enter fullscreen mode Exit fullscreen mode

Next step is to add scripts in Application.yml or property file :

      eureka:
            instance:
              prefer-ip-address: true
         client:
             register-with-eureka: true
             fetch-registry: true
             serviceUrl:
                defaultZone: http://localhost:8761/eureka/
Enter fullscreen mode Exit fullscreen mode

by adding these scripts the service will act as a eureka client and automatically store into Service Registry

Note that : netflix-eureka-server are the servers of Netflix itself which is configured by spring developers for using it in springboot application .

Top comments (0)