Creating a discovery service

Let's create a discovery service. As we know, a discovery service can solve the following problems of the cloud-native application:

  • How do services find each other?
  • What happens if we run multiple instances for a service?

Let's see the following diagram with the cloud-native problems:

We have already discussed the preceding diagram in detail in Chapter 5, Spring Cloud Netflix and Service Discovery.

Let's see the following Maven dependencies required for the discovery service:

<parent> 
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-starter-parent</artifactId> 
   <version>2.0.2.RELEASE</version> 
   <relativePath/> <!-- lookup parent from repository --> 
</parent> 
 
<properties> 
   ... 
   <spring-cloud.version>Finchley.M8</spring-cloud.version> 
</properties> 
 
<dependencies> 
   <dependency> 
         <groupId>org.springframework.cloud</groupId> 
         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 
   </dependency> 
</dependencies> 
 
<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> 

The preceding Maven configuration is included in Spring Cloud with the Finchley.M8 version and accordingly, in this version, it manages all transitive dependencies required for Spring Cloud.

So now spring-cloud-starter-eureka-server dependency is present in the Maven build configuration (pom.xml), and because of this dependency, the application will be started as a Eureka Server. As you can see, the spring-cloud-starter-eureka-server dependency starter is part of the Spring Cloud project and it uses the latest Spring Cloud release train (currently Finchley.M8) to manage your Maven dependency versions for other cloud-related dependencies.

After adding the Starter dependency for the Eureka Server, we now need to add a Eureka Service registry. It is a very simple and regular Spring Boot application with one additional annotation apart from @SpringBootApplication. This annotation is added to enable the service registry. So, you can use Spring Cloud's @EnableEurekaServer annotation to create and enable a Eureka registry server. The application can talk to this registry server and register itself.

Let's see the following code to create the discovery registry service and enable it to register the microservices:

package com.dineshonjava.eurekaserver; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 
 
@SpringBootApplication 
@EnableEurekaServer 
public class EurekaServerApplication { 
 
   public static void main(String[] args) { 
         SpringApplication.run(EurekaServerApplication.class, args); 
   } 
} 
 

As you can see, the preceding code is a very simple Spring Boot application class. Spring Boot's opinionated design makes it easy to create a Eureka Server just by annotating the entry point class with @EnableEurekaServer. This class is only required to implement the registry service.

You can see I have used two annotations:

  • @SpringBootApplication
  • @EnableEurekaServer

As you know, the @SpringBootApplication annotation enables your application to load the auto-configurations related to the Spring Cloud, and the @EnableEurekaServer annotation enables and starts this application as a Eureka Server.

Let's see the required basic configuration for this server application in the application.properties or application.yml file:

server: 
  port: 8761 
 
eureka: 
  instance: 
    hostname: localhost 
  client: 
    registerWithEureka: false 
    fetchRegistry: false 
    serviceUrl: 
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 

As per the preceding configuration, when this Eureka Server application starts, it will listen for registrations on server port 8761. All our microservices, at the time of start, will register themselves with this Eureka Server by making a call to this Eureka Server application running at server port 8761. Other services or web applications can query this Eureka Server to find other registered services. You can access the Eureka dashboard by using the http://localhost:8761 URI, because Eureka also provides a simple status dashboard:

The dashboard shows that the Eureka Server is running smoothly, but currently there are no instances registered with Eureka.

Now that we have created and started up a service registry, let's move on to create a client that registers itself with the Eureka registry server and also uses either Spring Cloud DiscoveryClient or EurekaClient to expose its own registry with the host and port.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.32