Example of service discovery pattern

There are many implementations available for this pattern, some of which are mentioned in the previous section, but for this particular example, we will use Netflix's Eureka. Spring has an in-built support for Netflix Eureka with Spring Boot (1.4.0+). With use of some annotation, we can create registry service and clients very easily. In this example, we will create one registry service and one client who will register itself while coming up. We will generate a sample code for Eureka server first from the Spring initializer or the Maven archetype. For this particular service, we will need some dependency to define spring-cloud-starter-eureka-server. Pom dependencies of eureka will be look like the following:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

Spring also has an implementation for zookeeper and consul. This can also be easily embedded as a spring-cloud-starter-consul-discovery dependency or a spring-cloud-starter-zookeeper-discovery dependency.

In your main application class, the user has to add an @EnableEurekaServer annotation. There's nothing fancy going on in the following piece of code:

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

The @EnableEurekaServer annotation enables the Eureka service discovery server. In application.properties, which will be located at src/main/resources/application.property, mention the following property:

server.port=8761 

Run the application with mvn spring-boot:run; users are supposed to get a warning level like this:

WARN 24432 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator  

This happens because, by default, the replica mode is on in the Eureka server, looking for another replica node, and it can't find any. Also, one sees the attempt of service to self-register. So, by adding the following two properties, these problems should be resolved:

eureka.client.register-with-eureka=false 
eureka.client.fetch-registry=false 

After adding these properties, the server should be up and running on port 8761. A dashboard can also be seen in the browser if you type http://locahost:8761 in the address bar you will see the following screenshot:

As you can see, there is no entry under the heading instances currently registered with Eureka; this is because we don't have any service instances registered with this server. Other than this, some general information regarding servers is available under the General Info heading, such as CPU, memory, IP address, and so on. So, by adding single annotation and some properties, we have our service registry server up.

To create a client again, generate the template code for Spring Boot with application/project name EurekaClientApplication as follows:

package com.practicalMicroservcies; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.client.discovery.DiscoveryClient; 
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
@EnableDiscoveryClient 
@SpringBootApplication 
public class EurekaClientApplication { 
 
    public static void main(String[] args) { 
        SpringApplication.run(EurekaClientApplication.class, args); 
    } 
     
    @RestController 
    class ServiceInstanceRestController { 
 
        @Autowired 
        private DiscoveryClient discoveryClient; 
 
        @RequestMapping("/Demo/{UserName}") 
        public  String serviceInstancesByApplicationName( 
                @PathVariable String UserName) { 
            return "Hello "+UserName; 
        } 
    } 
} 

First to test the URL you can hit http://localhost:8080/Demo/Alice it will show Hello Alice as response.

Using the @EnableDiscoveryClient annotation, as soon as the client comes up, it looks for service registry and registers itself with the name mentioned in the application.properties file with the spring.application.name key. In our example, we have used the PracticalMicroservice-Demo name. In client logs, you should be able to see the following log line:

c.n.e.registry.AbstractInstanceRegistry  : Registered instance PRACTICALMICROSERVICE-DEMO/192.168.1.4:PracticalMicroService-Demo with status UP (replication=false) 

We can see the same name as the registered service in the server console, as shown in the next screenshot. You can get any service instance with the following code:

this.discoveryClient.getInstances(serviceName); 

By default, this implementation of the service client looks for the local host for service registry. Additionally, we can override this property by adding the service URL in the properties file. Registry of service with server can be slow because the server creates an instance of service and fills it with all the metadata that it gets from the service client. Even after registry, there will be a heart between server and client at the interval of 30 seconds; although, it is also configurable.

Spring has many excellent features to support microservice architecture and their pattern. With spring-cloud, it brings a numerous number of features to handle different cases that arise in microservice, such as Ribbon for a client-side load balancing (Eureka aware client-side load balancing), Hystrix (developed by NetFlix) for circuit breaker pattern, and so on. Discussing the implementation and the use of these technologies is not the scope of this book, so I would like to leave it to the reader to explore spring-cloud and its offering for microservice architecture.

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

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