Reader small image

You're reading from  Mastering Spring Cloud

Product typeBook
Published inApr 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788475433
Edition1st Edition
Languages
Right arrow
Author (1)
Piotr Mińkowski
Piotr Mińkowski
author image
Piotr Mińkowski

Piotr works as a Solution Architect at Red Hat. He has several years of experience in software architecture and development. During this time, he was working in large organizations, where he was responsible for IT transformation to the modern cloud-native development approach. He is interested in technologies related to programming, containerization, and microservices. He writes about it in his blog https://piotrminkowski.com.
Read more about Piotr Mińkowski

Right arrow

Chapter 4. Service Discovery

Before we got to this point, we had discussed service discovery many times in previous chapters. In fact, it is one of the most popular technical aspects of microservice architecture. Such a subject could not have been omitted from the Netflix OSS implementation. They did not decide to use any existing tool with similar features, but designed and developed a discovery server especially for their own needs. Then, it had been open sourced along with several other tools. The Netflix OSS discovery server is known as Eureka.

The Spring Cloud library for integration with Eureka consists of two parts, the client side and the server side. The server is launched as a separate Spring Boot application and exposes an API that allows for the collection of a list of registered services and adding a new service with a location address. The server can be configured and deployed to be highly available, with each server replicating its state with the others. The client is included...

Running Eureka on the server side


Running the Eureka Server within a Spring Boot application is not a difficult task. Let's take a look at how this can be done:

  1. First, the right dependency has to be included to our project. Obviously, we will use a starter for that:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
  1. Eureka Server should also be enabled on the main application class:
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(DiscoveryApplication.class).web(true).run(args);
    }

}
  1. It is interesting that together with the server starter, client's dependencies are also included. They can be useful for us, but only when launching Eureka in high availability mode with peer-to-peer communication between discovery instances. When running a standalone instance...

Enabling Eureka on the client side


As on the server side, there is only one dependency that has to be included to enable a Eureka Client for the application. So, first include the following starter to your project's dependencies:

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

The example application does nothing more than communicate with the Eureka Server. It has to register itself and send metadata information such as host, port, health indicator URL, and home page. Eureka receives heartbeat messages from each instance belonging to a service. If the heartbeat isn't received after a configured period of time, the instance is removed from the registry. The second responsibility of discovery client is fetching data from the server, then caching it and periodically asking for changes. It can be enabled by annotating the main class with @EnableDiscoveryClient. Surprisingly...

Advanced configuration settings


Eureka's configuration settings may be divided into three parts:

Enabling secure communication between client and server


Until now, none of the client's connections were being authenticated by the Eureka Server. While in the development mode, security doesn't really matter as much as in the production mode. The lack of it may be a problem. We would like to have, as a bare minimum, the discovery server secured with basic authentication to prevent unauthorized access to any service that knows its network address. Although Spring Cloud reference material claims that HTTP basic authentication will be automatically added to your Eureka Client, I had to include a starter with security to the project dependencies:

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-security</artifactId>
 </dependency>

Then, we should enable security and set the default credentials by changing the configuration settings in the application.yml file:

security:
 basic:
   enabled: true
 user:
   name...

Eureka API


Spring Cloud Netflix provides a client written in Java that hides the Eureka HTTP API from the developer. In case we use other frameworks than Spring, Netflix OSS provides a vanilla Eureka client that can be included as a dependency. However, we may imagine a need to call the Eureka API directly, for example, if the application is written in another language than Java, or we need such information as a list of registered services in the Continuous Delivery process. Here's a table for quick reference:

Replication and high availability


We have already discussed some useful Eureka settings, but until now we have analyzed only a system with a single service discovery server. Such a configuration is valid, but only in development mode. For production mode, we would like to have at least two discovery servers running in case one of them fails or a network problem occurs. Eureka is by definition built for availability and resiliency, two primary pillars of development at Netflix. But it does not provide standard clustering mechanisms such as leadership election or automatically joining to the cluster. It is based on the peer-to-peer replication model. It means that all of the servers replicate data and send heartbeats to all of the peers, which are set in configuration for the current server node. Such an algorithm is simple and effective for containing data, but it also has some drawbacks. It limits scalability, because every node has to withstand the entire write load on the server.

Architecture...

Zones


A cluster-based on a peer-to-peer replication model is a good way to go in most cases, but not always enough. Eureka has one more interesting feature that can be very useful in a clustered environment. A zone mechanism is, in fact, the default behavior. Even if we have a single standalone service discovery instance, every client's property has to be set to eureka.client.serviceUrl.defaultZone in the configuration settings. When will this be useful to us? To analyze it, we go back to the example from the previous section. Let's imagine that now we have our environment divided into three different physical networks, or we just have three different machines processing the incoming requests. Of course, discovery services are still grouped logically in the cluster, but each instance is placed in a separated zone. Every client application would be registered in the same zone as its main discovery server. Instead of one instance of the Zuul gateway, we are going to launch three instances...

Summary


In this chapter, we had the opportunity to develop applications using Spring Cloud for the first time in this book. In my opinion, the best way to start an adventure with a framework for microservices is with trying to figure out how to implement service discovery properly. Starting with the simplest use cases and examples, we have been going through advanced and production-ready features provided by the Netflix OSS Eureka project. I have shown you how to create and run a basic client and a standalone discovery server in five minutes. Based on that implementation, I have introduced how to customize the Eureka client and server to meet our specific needs, placing the emphasis on negative scenarios such as network or application failure. Such features as the REST API or UI dashboard have been discussed in detail. Finally, I have shown you how to create a production-ready environment using Eureka's mechanisms such as replication, zones, and high availability. With that knowledge, you...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Spring Cloud
Published in: Apr 2018Publisher: PacktISBN-13: 9781788475433
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Piotr Mińkowski

Piotr works as a Solution Architect at Red Hat. He has several years of experience in software architecture and development. During this time, he was working in large organizations, where he was responsible for IT transformation to the modern cloud-native development approach. He is interested in technologies related to programming, containerization, and microservices. He writes about it in his blog https://piotrminkowski.com.
Read more about Piotr Mińkowski

HTTP endpoint

Description

POST /eureka/apps/appID

Add a new instance of the service to the registry

DELETE /eureka/apps/appID/instanceID

Remove the instance of the service from the registry

PUT /eureka/apps/appID/instanceID

Send a heartbeat to the server

GET /eureka/apps

Get details about the list of all registered instances of services

GET /eureka/apps/appID 

Get details about the list of all registered instances of a specific service

GET /eureka/apps/appID/instanceID

Get details about a single...