About Microservices.io

Microservices.io is brought to you by Chris Richardson. Experienced software architect, author of POJOs in Action and the creator of the original CloudFoundry.com. His latest startup is eventuate.io, a microservices application platform.


Learn more about microservices

Chris offers a comprehensive set of resources for learning about microservices including articles, an O'Reilly training video, and example code. Learn more


Microservices consulting and training

Chris offers a comprehensive consulting services, workshops and hands on training classes to help you use microservices effectively. Get advice


Example microservices applications

Want to see an example? Check out Chris Richardson's example applications. See code


Get the book: Microservice patterns


Signup for the newsletter

A new microservices application platform that solves distributed data management problems.

Join the microservices google group

Pattern: Self Registration

Context

You have applied either the Client-side Service Discovery pattern or the Server-side Service Discovery pattern. Service instances must be registered with the service registry on startup so that they can be discovered and unregistered on shutdown.

Problem

How are service instances registered with and unregistered from the service registry?

Forces

  • Service instances must be registered with the service registry on startup and unregistered on shutdown
  • Service instances that crash must be unregistered from the service registry
  • Service instances that are running but incapable of handling requests must be unregistered from the service registry

Solution

A service instance is responsible for registering itself with the service registry. On startup the service instance registers itself (host and IP address) with the service registry and makes itself available for discovery. The client must typically periodically renew its registration so that the registry knows it is still alive. On shutdown, the service instance unregisters itself from the service registry.

This is typically handled by a Microservice chassis framework

Examples

The Microservices Example application is an example of an application that uses self-registration. It is written in Scala and uses Spring Boot and Spring Cloud as the Microservice chassis. The application uses the Eureka Service Registry, which is a Netflix OSS component.

Service registration is configured using the @EnableEurekaClient on a Java config class:

@Configuration
@EnableEurekaClient
class EurekaClientConfiguration {

This annotation causes the service instance to be registered with Eureka.

Resulting context

The benefits of the Self Registration pattern include the following:

  • A service instance knows its own state so can implement a state model that’s more complex than UP/DOWN, e.g. STARTING, AVAILABLE, …

There are also some drawbacks:

  • Couples the service to the Service Registry
  • You must implement service registration logic in each programming language/framework that you use to write your services, e.g. NodeJS/JavaScript, Java/Scala, etc.
  • A service instance that is running yet unable to handle requests will often lack the self-awareness to unregister itself from the service registry

Copyright © 2017 Chris Richardson • All rights reserved • Supported by Kong.