ebook img

Microservices From Day One Build robust and scalable software from the start PDF

258 Pages·2016·10.246 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Microservices From Day One Build robust and scalable software from the start

Microservices from Day One Build robust and scalable software from the start — Cloves Carneiro Jr. Tim Schmelmer Microservices from Day One Build robust and scalable software from the start Cloves Carneiro Jr. Tim Schmelmer Microservices from Day One: Build robust and scalable software from the start Cloves Carneiro Jr. Tim Schmelmer Hollywood, Florida, USA Broomfield, Colorado, USA ISBN-13 (pbk): 978-1-4842-1936-2 ISBN-13 (electronic): 978-1-4842-1937-9 DOI 10.1007/978-1-4842-1937-9 Library of Congress Control Number: 2016961230 Copyright © 2016 by Cloves Carneiro Jr. and Tim Schmelmer This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Acquisitions Editor: Louise Corrigan Development Editor: Chris Nelson Technical Reviewer: Tom Copeland Editorial Board: Steve Anglin, Pramila Balan, Laura Berendson, Aaron Black, Louise Corrigan, Jonathan Gennick, Todd Green, Robert Hutchinson, Celestin Suresh John, Nikhil Karkal, James Markham, Susan McDermott, Matthew Moodie, Natalie Pao, Gwenan Spearing Coordinating Editor: Nancy Chen Copy Editor: James A. Compton Compositor: SPi Global Indexer: SPi Global Artist: SPi Global, image courtesy of Freepik Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springer.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text are available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. Printed on acid-free paper To my wife Safora, for all her love, and for her support during the hundreds of hours that I spent on this book while she ran the family. —Tim Schmelmer For my family, Jane, Noah, and Sofia. —Cloves Carneiro Jr. Contents at a Glance About the Authors ����������������������������������������������������������������������������������������������������xv About the Technical Reviewer �������������������������������������������������������������������������������xvii Acknowledgments ��������������������������������������������������������������������������������������������������xix Introduction ������������������������������������������������������������������������������������������������������������xxi ■ Part I: Service Oriented Architectures ������������������������������������������������1 ■ Chapter 1: Microservices: The What and the Why �������������������������������������������������3 ■ Chapter 2: Rules of Engagement �������������������������������������������������������������������������19 ■ Part II: APIs ���������������������������������������������������������������������������������������23 ■ Chapter 3: Partitioning Your Business into Services �������������������������������������������25 ■ Chapter 4: Designing Your APIs ��������������������������������������������������������������������������37 ■ Chapter 5: Defining APIs ��������������������������������������������������������������������������������������59 ■ Chapter 6: Consuming Your APIs �������������������������������������������������������������������������75 ■ Chapter 7: Optimizing Your APIs �������������������������������������������������������������������������83 ■ Part III: Development and Deployment �������������������������������������������103 ■ Chapter 8: Development Environment and Workflow ����������������������������������������105 ■ Chapter 9: Testing with Services �����������������������������������������������������������������������127 ■ Chapter 10: Deploying and Running Microservices �������������������������������������������151 v ■ Contents at a GlanCe ■ Part IV: Putting Everything Together ����������������������������������������������175 ■ Chapter 11: Polyglot Services ���������������������������������������������������������������������������177 ■ Chapter 12: Single-Client Application—Mobile and Web ����������������������������������185 ■ Chapter 13: Monitoring Your Services ���������������������������������������������������������������211 ■ Chapter 14: Operational Excellence with Microservices �����������������������������������221 Index ���������������������������������������������������������������������������������������������������������������������243 vi Contents About the Authors ����������������������������������������������������������������������������������������������������xv About the Technical Reviewer �������������������������������������������������������������������������������xvii Acknowledgments ��������������������������������������������������������������������������������������������������xix Introduction ������������������������������������������������������������������������������������������������������������xxi ■ Part I: Service Oriented Architectures ������������������������������������������������1 ■ Chapter 1: Microservices: The What and the Why �������������������������������������������������3 All Successful Software Undergoes Change ��������������������������������������������������������������������3 Remember Service-Oriented Architectures? ��������������������������������������������������������������������3 What Is a Service? ������������������������������������������������������������������������������������������������������������4 What Are Microservices? ������������������������������������������������������������������������������������������������������������������������4 Microservices Are Not “CORBA over HTTP” ��������������������������������������������������������������������������������������������5 Industry Adoption of Microservices ����������������������������������������������������������������������������������6 Why Use Microservices? ��������������������������������������������������������������������������������������������������9 Advantages as Seen by Industry Experts �����������������������������������������������������������������������������������������������9 Our Reasons to Adopt a Microservices Architecture ���������������������������������������������������������������������������12 Potential Pitfalls and How to Avoid Them �����������������������������������������������������������������������13 When Should I Start with a Microservices Architecture? �����������������������������������������������17 Summary ������������������������������������������������������������������������������������������������������������������������18 ■ Chapter 2: Rules of Engagement �������������������������������������������������������������������������19 Customer-Facing Applications Cannot Directly Touch Any Data Store ���������������������������19 No Service Accesses Another Service’s Data Store �������������������������������������������������������20 Invest in Making Spinning Up New Services Trivial �������������������������������������������������������20 vii ■ Contents Build Services in a Consistent Way ��������������������������������������������������������������������������������21 Define API Contracts in Code ������������������������������������������������������������������������������������������21 Every Service Has at Least a Development and a Production Instance �������������������������22 Existing Code Bases �����������������������������������������������������������������������������������������������������������������������������22 Summary ������������������������������������������������������������������������������������������������������������������������22 ■ Part II: APIs ���������������������������������������������������������������������������������������23 ■ Chapter 3: Partitioning Your Business into Services �������������������������������������������25 General Approaches for Application Decomposition and Aggregation ���������������������������25 Application Decomposition �������������������������������������������������������������������������������������������������������������������25 Application Aggregation������������������������������������������������������������������������������������������������������������������������28 When to Use Decomposition versus Aggregation? ���������������������������������������������������������30 Decomposing and Aggregating a Sample Application ����������������������������������������������������31 Welcome to Our Bookstore! ������������������������������������������������������������������������������������������������������������������31 Partitioning the Bookstore ��������������������������������������������������������������������������������������������������������������������32 Summary ������������������������������������������������������������������������������������������������������������������������36 ■ Chapter 4: Designing Your APIs ��������������������������������������������������������������������������37 Introducing APIs �������������������������������������������������������������������������������������������������������������37 The Significance of API Design �������������������������������������������������������������������������������������������������������������38 Why is Designing a Good API Important? ����������������������������������������������������������������������������������������������38 What Makes an API Good?����������������������������������������������������������������������������������������������39 General Characteristics ������������������������������������������������������������������������������������������������������������������������39 Hypermedia Type ����������������������������������������������������������������������������������������������������������������������������������40 API Design Guide ����������������������������������������������������������������������������������������������������������������������������������41 Nonfunctional Requirements ����������������������������������������������������������������������������������������������������������������42 Implementing APIs the Right Way ����������������������������������������������������������������������������������43 Gather Requirements ����������������������������������������������������������������������������������������������������������������������������43 Maximize Information Hiding ����������������������������������������������������������������������������������������������������������������44 APIs Change ������������������������������������������������������������������������������������������������������������������������������������������45 viii ■ Contents Designing / Splitting Up by Example ������������������������������������������������������������������������������49 The Bookstore Home Page �������������������������������������������������������������������������������������������������������������������49 The Book Detail Page����������������������������������������������������������������������������������������������������������������������������52 Summary ������������������������������������������������������������������������������������������������������������������������57 ■ Chapter 5: Defining APIs ��������������������������������������������������������������������������������������59 Interface Definition Languages (IDLs) ����������������������������������������������������������������������������59 A Matter of Style: RPC versus REST �������������������������������������������������������������������������������60 RPC-Style Interface Definition Languages ��������������������������������������������������������������������������������������������60 RESTful Interface Definition Languages �����������������������������������������������������������������������������������������������60 Our Stance ��������������������������������������������������������������������������������������������������������������������������������������������61 Describing APIs using Swagger ��������������������������������������������������������������������������������������61 Hosting Swagger Specification as JSON Schemas ������������������������������������������������������������������������������62 The Swagger User Interface �����������������������������������������������������������������������������������������������������������������65 Generating Code from Swagger Definitions �����������������������������������������������������������������������������������������68 Generating the Swagger JSON Specifications ��������������������������������������������������������������������������������������71 Summary ������������������������������������������������������������������������������������������������������������������������74 ■ Chapter 6: Consuming Your APIs �������������������������������������������������������������������������75 What Are Client Libraries? ����������������������������������������������������������������������������������������������75 What Not to Do ���������������������������������������������������������������������������������������������������������������76 Don’t wait eternally ������������������������������������������������������������������������������������������������������������������������������76 Don’t make the clients smart ���������������������������������������������������������������������������������������������������������������76 Service Orchestration �����������������������������������������������������������������������������������������������������77 Caching in the Client ������������������������������������������������������������������������������������������������������78 Build Caching into Your Client Library ��������������������������������������������������������������������������������������������������78 Error Handling ����������������������������������������������������������������������������������������������������������������78 Timeouts �����������������������������������������������������������������������������������������������������������������������������������������������79 Circuit Breakers ������������������������������������������������������������������������������������������������������������������������������������79 Performance �������������������������������������������������������������������������������������������������������������������80 Updating Client Libraries ������������������������������������������������������������������������������������������������81 ix ■ Contents Service Discovery �����������������������������������������������������������������������������������������������������������81 Summary ������������������������������������������������������������������������������������������������������������������������82 ■ Chapter 7: Optimizing Your APIs �������������������������������������������������������������������������83 Measuring Performance �������������������������������������������������������������������������������������������������83 Service Access Pattern-Based Improvements ���������������������������������������������������������������85 The Blessing and the Curse of Caching ��������������������������������������������������������������������������86 Caching in the API Consumer ���������������������������������������������������������������������������������������������������������������86 Caching in the API Provider ������������������������������������������������������������������������������������������������������������������86 Make Your Service Work Less ����������������������������������������������������������������������������������������87 Use the Standard ����������������������������������������������������������������������������������������������������������������������������������87 Framework Support for HTTP Conditional GET �������������������������������������������������������������������������������������89 Caching Proxies ��������������������������������������������������������������������������������������������������������������92 Size Matters �������������������������������������������������������������������������������������������������������������������93 Result Representations�������������������������������������������������������������������������������������������������������������������������93 Partial Resources ����������������������������������������������������������������������������������������������������������������������������������94 HTTP Compression �������������������������������������������������������������������������������������������������������������������������������95 Semantic Paging �����������������������������������������������������������������������������������������������������������������������������������96 Optimizing for Efficiency of Development and Support ��������������������������������������������������98 Talk Less, Listen More ����������������������������������������������������������������������������������������������������99 Summary ����������������������������������������������������������������������������������������������������������������������101 ■ Part III: Development and Deployment �������������������������������������������103 ■ Chapter 8: Development Environment and Workflow ����������������������������������������105 The Premise ����������������������������������������������������������������������������������������������������������������105 The Development Environment Wish List ���������������������������������������������������������������������105 Ease of Development Tool Setup ��������������������������������������������������������������������������������������������������������106 Ease of Application Setup �������������������������������������������������������������������������������������������������������������������106 Automation of Application Tests ���������������������������������������������������������������������������������������������������������106 Availability of Sample Data �����������������������������������������������������������������������������������������������������������������106 Up-to-Date Dependencies ������������������������������������������������������������������������������������������������������������������106 x

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.