logo资料库

Building Reactive Microservices in Java.pdf

第1页 / 共83页
第2页 / 共83页
第3页 / 共83页
第4页 / 共83页
第5页 / 共83页
第6页 / 共83页
第7页 / 共83页
第8页 / 共83页
资料共83页,剩余部分请下载后查看
Cover
Copyright
Table of Contents
Chapter 1. Introduction
Preparing Your Environment
Chapter 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!
Project Creation
Write Your First Verticle
Using RxJava
Packaging Your Application as a Fat Jar
Logging, Monitoring, and Other Production Elements
Summary
Chapter 3. Building Reactive Microservices
First Microservices
Implementing HTTP Microservices
Getting Started
The Verticle
HTTP Microservice
Using Routes and Parameters
Producing JSON
Packaging and Running
Consuming HTTP Microservices
Project Creation
Calling the Service More Than Once
Are These Microservices Reactive Microservices?
The Vert.x Event Bus—A Messaging Backbone
Message-Based Microservices
Project Creation
Writing the Message-Driven Verticle
Initiating Message-Based Interactions
Are We Reactive Now?
Elasticity
Resilience
Summary
Chapter 4. Building Reactive Microservice Systems
Service Discovery
Client- and Server-Side Service Discovery
Vert.x Service Discovery
Stability and Resilience Patterns
Managing Failures in Reactive Microservices
Using Timeouts
Circuit Breakers
Health Checks and Failovers
Summary
Chapter 5. Deploying Reactive Microservices in OpenShift
What Is OpenShift?
Build Configuration
Deployment Configurations
Pods
Services and Routes
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
Chapter 6. Conclusion
What Have We Learned?
Microservices Aren’t Easy
The Evolution of the Microservice Paradigm
Vert.x Versatility
About the Author
Building Reactive Microservices in Java Asynchronous and Event-Based Application Design Clement Escoffier Beijing Beijing Boston Boston Farnham Sebastopol Farnham Sebastopol Tokyo 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. Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest Editor: Brian Foster Production Editor: Shiny Kalapurakkel Copyeditor: Christina Edwards Proofreader: Sonia Saruba May 2017: First Edition 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 1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Preparing Your Environment 2 2. Understanding Reactive Microservices and Vert.x. . . . . . . . . . . . . . . . 3 Reactive Programming 4 Reactive Systems 7 Reactive Microservices 8 What About Vert.x ? 9 Asynchronous Development Model 11 Verticles—the Building Blocks 14 From Callbacks to Observables 15 Let’s Start Coding! 17 Summary 21 3. Building Reactive Microservices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 First Microservices 23 Implementing HTTP Microservices 24 Consuming HTTP Microservices 28 Are These Microservices Reactive Microservices? 31 The Vert.x Event Bus—A Messaging Backbone 32 Message-Based Microservices 33 Initiating Message-Based Interactions 35 Are We Reactive Now? 37 Summary 40 4. Building Reactive Microservice Systems. . . . . . . . . . . . . . . . . . . . . . . . 43 Service Discovery 43 Stability and Resilience Patterns 47 Summary 54 iii
5. Deploying Reactive Microservices in OpenShift. . . . . . . . . . . . . . . . . . 57 What Is OpenShift? 57 Installing OpenShift on Your Machine 60 Deploying a Microservice in OpenShift 62 Service Discovery 64 Scale Up and Down 65 Health Check and Failover 67 Using a Circuit Breaker 68 But Wait, Are We Reactive? 70 Summary 71 6. Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 What Have We Learned? 73 Microservices Aren’t Easy 74 The Evolution of the Microservice Paradigm 75 Vert.x Versatility 76 iv | Table of Contents
CHAPTER 1 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 do 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. 1
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. 2 | Chapter 1: Introduction
分享到:
收藏