Building Reactive Microservices in Java Asynchronous and Event-Based Application Design Clement Escoffier Beijing Boston Farnham Sebastopol Tokyo Building Reactive Microservices in Java by Clement Escoffier Copyright © 2017 O’Reilly Media All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://oreilly.com/safari) For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com Editor: Brian Foster Production Editor: Shiny Kalapurakkel Copyeditor: Christina Edwards Proofreader: Sonia Saruba Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest First Edition May 2017: Revision History for the First Edition 2017-04-06: First Release The O’Reilly logo is a registered trademark of O’Reilly Media, Inc Building Reactive Microservices in Java, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limi‐ tation responsibility for damages resulting from the use of or reliance on this work Use of the information and instructions contained in this work is at your own risk If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsi‐ bility to ensure that your use thereof complies with such licenses and/or rights 978-1-491-98626-4 [LSI] Table of Contents Introduction Preparing Your Environment 2 Understanding Reactive Microservices and Vert.x Reactive Programming Reactive Systems Reactive Microservices What About Vert.x ? Asynchronous Development Model Verticles—the Building Blocks From Callbacks to Observables Let’s Start Coding! Summary 11 14 15 17 21 Building Reactive Microservices 23 First Microservices Implementing HTTP Microservices Consuming HTTP Microservices Are These Microservices Reactive Microservices? The Vert.x Event Bus—A Messaging Backbone Message-Based Microservices Initiating Message-Based Interactions Are We Reactive Now? Summary 23 24 28 31 32 33 35 37 40 Building Reactive Microservice Systems 43 Service Discovery Stability and Resilience Patterns Summary 43 47 54 iii Deploying Reactive Microservices in OpenShift 57 What Is OpenShift? Installing OpenShift on Your Machine Deploying a Microservice in OpenShift Service Discovery Scale Up and Down Health Check and Failover Using a Circuit Breaker But Wait, Are We Reactive? Summary 57 60 62 64 65 67 68 70 71 Conclusion 73 What Have We Learned? Microservices Aren’t Easy The Evolution of the Microservice Paradigm Vert.x Versatility iv | Table of Contents 73 74 75 76 CHAPTER Introduction This report is for developers and architects interested in developing microservices and distributed applications It does not explain the basics of distributed systems, but instead focuses on the reactive benefits to build efficient microservice systems Microservices can be seen as an extension of the basic idea of modularity: programs connected by message-passing instead of direct API calls so that they can be distributed among multiple services Why are microser‐ vices so popular? It’s basically due to the combination of two factors: cloud computing and the need to scale up and down quickly Cloud computing makes it convenient to deploy thousands of small serv‐ ices; scaling makes it necessary to so In this report, we will see how Eclipse Vert.x (http://vertx.io) can be used to build reactive microservice systems Vert.x is a toolkit to build reactive and distributed systems Vert.x is incredibly flexible Because it’s a toolkit, you can build simple network utilities, modern web applications, a system ingesting a huge amount of messages, REST services, and, obviously, microservices This malleability gives Vert.x great popularity, a large community, and a vibrant ecosystem Vert.x was already promoting microservices before it became so pop‐ ular Since the beginning, Vert.x has been tailored to build applica‐ tions composed by a set of distributed and autonomous services Systems using Vert.x are built upon the reactive system principles (http://reactivemanifesto.org) They are responsive, elastic, resilient, and use asynchronous message passing to interact This report goes beyond Vert.x and microservices It looks at the whole environment in which a microservice system runs and intro‐ duces the many tools needed to get the desired results On this jour‐ ney, we will learn: • What Vert.x is and how you can use it • What reactive means and what reactive microservices are • How to implement microservices using HTTP or messages • The patterns used to build reactive microservice systems • How to deploy microservices in a virtual or cloud environment The code presented in this report is available from https:// github.com/redhat-developer/reactive-microservices-in-java Preparing Your Environment Eclipse Vert.x requires Java 8, which we use for the different exam‐ ples provided in this report We are going to use Apache Maven to build them Make sure you have the following prerequisites installed: • JDK 1.8 • Maven 3.3+ • A command-line terminal (Bash, PowerShell, etc.) Even if not mandatory, we recommend using an IDE such as the Red Hat Development Suite (https://developers.redhat.com/products/ devsuite/overview) In the last chapter, we use OpenShift, a container platform built on top of Kubernetes (https://kubernetes.io) to run containerized microservices To install OpenShift locally, we recom‐ mend Minishift (https://github.com/minishift/minishift) or the Red Hat Container Development Kit (CDK) v3 You can download the CDK from https://developers.redhat.com/products/cdk/download Let’s get started | Chapter 1: Introduction CHAPTER Understanding Reactive Microservices and Vert.x Microservices are not really a new thing They arose from research conducted in the 1970s and have come into the spotlight recently because microservices are a way to move faster, to deliver value more easily, and to improve agility However, microservices have roots in actor-based systems, service design, dynamic and auto‐ nomic systems, domain-driven design, and distributed systems The fine-grained modular design of microservices inevitably leads devel‐ opers to create distributed systems As I’m sure you’ve noticed, dis‐ tributed systems are hard They fail, they are slow, they are bound by the CAP and FLP theorems In other words, they are very compli‐ cated to build and maintain That’s where reactive comes in 30+ Years of Evolution The actor model was introduced by C Hewitt, P Bishop, and R Steiger in 1973 Autonomic computing, a term coined in 2001, refers to the self-managing characteristics (self-healing, selfoptimization, etc.) of distributed computing resources But what is reactive? Reactive is an overloaded term these days The Oxford dictionary defines reactive as “showing a response to a stimu‐ lus.” So, reactive software reacts and adapts its behavior based on the stimuli it receives However, the responsiveness and adaptability promoted by this definition are programming challenges because the flow of computation isn’t controlled by the programmer but by the stimuli In this chapter, we are going to see how Vert.x helps you be reactive by combining: • Reactive programming—A development model focusing on the observation of data streams, reacting on changes, and propagat‐ ing them • Reactive system—An architecture style used to build responsive and robust distributed systems based on asynchronous message-passing A reactive microservice is the building block of reactive microservice systems However, due to their asynchronous aspect, the implemen‐ tation of these microservices is challenging Reactive programming reduces this complexity How? Let’s answer this question right now Reactive Programming Figure 2-1 Reactive programming is about flow of data and reacting to it Reactive programming is a development model oriented around data flows and the propagation of data In reactive programming, the stimuli are the data transiting in the flow, which are called streams There are many ways to implement a reactive programming model In this report, we are going to use Reactive Extensions (http://reactivex.io/) where streams are called observables, and con‐ sumers subscribe to these observables and react to the values (Figure 2-1) | Chapter 2: Understanding Reactive Microservices and Vert.x ... provides reactive systems with two critical properties: • Elasticity—The ability to scale horizontally (scale out/in) • Resilience—The ability to handle failure and recover Elasticity comes from the... Asynchronisity • Resilience • Elasticity Reactive microservices are autonomous They can adapt to the avail‐ ability or unavailability of the services surrounding them However, autonomy comes paired with... independently, and cooperate with others as needed A reactive microservice uses asynchronous messagepassing to interact with its peers It also receives messages and has the ability to produce responses