ebook img

Practical Microservices Architectural Patterns - Event-Based Java Microservices with Spring Boot and Spring Cloud PDF

916 Pages·2019·33.342 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 Practical Microservices Architectural Patterns - Event-Based Java Microservices with Spring Boot and Spring Cloud

Practical Microservices Architectural Patterns Event-Based Java Microservices with Spring Boot and Spring Cloud — Binildas Christudas Foreword by Guy Pardon, Allard Buijze and Schahram Dustdar Practical Microservices Architectural Patterns Event-Based Java Microservices with Spring Boot and Spring Cloud Binildas Christudas Foreword by Guy Pardon, Allard Buijze and Schahram Dustdar Practical Microservices Architectural Patterns Binildas Christudas Trivandrum, Kerala, India ISBN-13 (pbk): 978-1-4842-4500-2 ISBN-13 (electronic): 978-1-4842-4501-9 https://doi.org/10.1007/978-1-4842-4501-9 Copyright © 2019 by Binildas Christudas 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, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Nikhil Karkal Development Editor: Siddhi Chavan Coordinating Editor: Divya Modi Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) 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 orders-ny@springer- sbm.com, or visit www.springeronline.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/ rights-permissions. Apress titles 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 Print and eBook Bulk Sales web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-4500-2. For more detailed information, please visit www.apress.com/source-code. Printed on acid-free paper To Sowmya, Ann, and Ria. Table of Contents About the Author ����������������������������������������������������������������������������������������������������xxi About the Technical Reviewer ������������������������������������������������������������������������������xxiii Acknowledgments �������������������������������������������������������������������������������������������������xxv Foreword �������������������������������������������������������������������������������������������������������������xxvii Introduction ����������������������������������������������������������������������������������������������������������xxxi Chapter 1: Distributed Computing Architecture Landscape �������������������������������������1 System Architectures ��������������������������������������������������������������������������������������������������������������������2 Mainframe Architecture �����������������������������������������������������������������������������������������������������������4 Client-Server Architecture �������������������������������������������������������������������������������������������������������4 Three-Tier Architecture������������������������������������������������������������������������������������������������������������4 N-Tier Architecture ������������������������������������������������������������������������������������������������������������������5 Network Architectures ������������������������������������������������������������������������������������������������������������������5 Point to Point ���������������������������������������������������������������������������������������������������������������������������6 Hub and Spoke ������������������������������������������������������������������������������������������������������������������������6 Enterprise Message Bus ����������������������������������������������������������������������������������������������������������6 Enterprise Service Bus (ESB) ���������������������������������������������������������������������������������������������������7 Software Architectures �����������������������������������������������������������������������������������������������������������������7 Application Tiers ����������������������������������������������������������������������������������������������������������������������7 Application Layers �������������������������������������������������������������������������������������������������������������������8 The Application Architecture Landscape ���������������������������������������������������������������������������������������8 Typical Application Architecture ����������������������������������������������������������������������������������������������9 Typical Deployment Architecture �������������������������������������������������������������������������������������������11 The Scalability Dilemma �������������������������������������������������������������������������������������������������������������13 Application State �������������������������������������������������������������������������������������������������������������������13 v Table of ConTenTs The Dependency Nightmare ��������������������������������������������������������������������������������������������������15 The Application Monolith �������������������������������������������������������������������������������������������������������16 The Scalable Architecture �����������������������������������������������������������������������������������������������������������17 Stateless Design ��������������������������������������������������������������������������������������������������������������������18 Divide and Conquer ���������������������������������������������������������������������������������������������������������������18 Summary�������������������������������������������������������������������������������������������������������������������������������������19 Chapter 2: Introducing Microservices ��������������������������������������������������������������������21 The Modular Monolith �����������������������������������������������������������������������������������������������������������������21 The Modular Organization �����������������������������������������������������������������������������������������������������22 The Monolith Application �������������������������������������������������������������������������������������������������������23 Monolith Application Boundary ����������������������������������������������������������������������������������������������24 Monolith Intermodule Dependency ����������������������������������������������������������������������������������������25 The Scalability Dilemma ��������������������������������������������������������������������������������������������������������26 Monolith Technology Constraints �������������������������������������������������������������������������������������������28 Introducing Microservices ����������������������������������������������������������������������������������������������������������29 Independent Modules ������������������������������������������������������������������������������������������������������������29 Intermodule Communication �������������������������������������������������������������������������������������������������30 The Microservices �����������������������������������������������������������������������������������������������������������������31 Summary�������������������������������������������������������������������������������������������������������������������������������������34 Chapter 3: Microservices in Depth �������������������������������������������������������������������������35 Look and Feel of a Microservice �������������������������������������������������������������������������������������������������36 The E-Commerce Microservice ���������������������������������������������������������������������������������������������36 No Tier, Distributed ����������������������������������������������������������������������������������������������������������������37 The Micromonolith �����������������������������������������������������������������������������������������������������������������38 Understanding Self-Contained Microservices �����������������������������������������������������������������������40 Resemblance of Microservices to SOA ����������������������������������������������������������������������������������41 Message-Oriented Microservices ������������������������������������������������������������������������������������������44 Advanced Microservices Capabilities �����������������������������������������������������������������������������������������46 Fail-Safe Microservices ���������������������������������������������������������������������������������������������������������46 vi Table of ConTenTs Scalable Microservices ���������������������������������������������������������������������������������������������������������47 Extensible Microservices �������������������������������������������������������������������������������������������������������51 Summary�������������������������������������������������������������������������������������������������������������������������������������52 Chapter 4: Microservices Architecture �������������������������������������������������������������������55 Architecture for Digital Business ������������������������������������������������������������������������������������������������56 The Digital Era �����������������������������������������������������������������������������������������������������������������������56 The Digital App ����������������������������������������������������������������������������������������������������������������������57 The Mesh App and Service Architecture ��������������������������������������������������������������������������������58 The Context for Microservices ����������������������������������������������������������������������������������������������������60 Granularity of Services ����������������������������������������������������������������������������������������������������������60 The Gateway ��������������������������������������������������������������������������������������������������������������������������64 Domain-Centric Partition �������������������������������������������������������������������������������������������������������65 The Cloud Native Shift �����������������������������������������������������������������������������������������������������������67 Web Scale Computing �����������������������������������������������������������������������������������������������������������68 The Indispensable Cloud �������������������������������������������������������������������������������������������������������������68 Cloud Architecture Model ������������������������������������������������������������������������������������������������������68 Cloud Service Model ��������������������������������������������������������������������������������������������������������������71 SaaS Maturity Models �����������������������������������������������������������������������������������������������������������72 Virtualization ��������������������������������������������������������������������������������������������������������������������������75 Virtualized Servers vs� Containers �����������������������������������������������������������������������������������������75 The Architecture of Microservices ����������������������������������������������������������������������������������������������79 Inversion of Architecture �������������������������������������������������������������������������������������������������������79 The Inner Architecture Perspective ���������������������������������������������������������������������������������������80 The Outer Architecture Perspective ���������������������������������������������������������������������������������������83 The Big Picture of MASA ��������������������������������������������������������������������������������������������������������85 Summary�������������������������������������������������������������������������������������������������������������������������������������86 Chapter 5: Essential Patterns for Microservices ����������������������������������������������������87 Orthogonal Scale Out of Services �����������������������������������������������������������������������������������������������88 Write vs� Read Transactions ��������������������������������������������������������������������������������������������������88 The Look-to-Book Challenge �������������������������������������������������������������������������������������������������89 vii Table of ConTenTs CQRS: Command Query Responsibility Segregation �������������������������������������������������������������������90 Traditional vs� CQRS-Based Software Systems ���������������������������������������������������������������������90 The Nomenclature in CQRS ���������������������������������������������������������������������������������������������������96 Event-Based CQRS Architecture �������������������������������������������������������������������������������������������������96 A Meta Model for Event-Based CQRS Design ������������������������������������������������������������������������96 Command Query Segregation Using Events ������������������������������������������������������������������������100 Scale Out for CQRS-Based Microservices ���������������������������������������������������������������������������102 Summary�����������������������������������������������������������������������������������������������������������������������������������104 Chapter 6: Distributed Messaging ������������������������������������������������������������������������105 Messaging for Resiliency ����������������������������������������������������������������������������������������������������������105 Message Persistence ����������������������������������������������������������������������������������������������������������105 Design for Various Operational Characteristics of Microservices ����������������������������������������107 A Chain Is As Strong As Its Weakest Link ����������������������������������������������������������������������������108 Synchronous vs� Asynchronous ������������������������������������������������������������������������������������������������110 Synchronous Interactions Between Microservices �������������������������������������������������������������110 Asynchronous Interactions Between Microservices ������������������������������������������������������������113 Send and Receive Messages to a Single Node RabbitMQ Broker ���������������������������������������������118 RabbitMQ Message Sender �������������������������������������������������������������������������������������������������118 RabbitMQ Message Receiver �����������������������������������������������������������������������������������������������119 Build and Run the RabbitMQ Sample ����������������������������������������������������������������������������������121 Send and Receive Messages to RabbitMQ Using Spring AMQP ������������������������������������������������124 Spring AMQP Message Listener ������������������������������������������������������������������������������������������124 Spring AMQP Message Producer �����������������������������������������������������������������������������������������127 Build and Run the Spring AMQP RabbitMQ Sample �������������������������������������������������������������128 Send and Receive Messages to Multi-Node RabbitMQ Cluster�������������������������������������������������130 Tying the Knots Between the Consumer and Producer �������������������������������������������������������������134 Correlation ID �����������������������������������������������������������������������������������������������������������������������134 Code a Custom Network Server to Handle Concurrent Heavy Traffic ����������������������������������135 The Simultaneous Exhibition Game �������������������������������������������������������������������������������������143 Message Correlation and Microservices �����������������������������������������������������������������������������145 Summary�����������������������������������������������������������������������������������������������������������������������������������145 viii Table of ConTenTs Chapter 7: Spring Boot �����������������������������������������������������������������������������������������147 Performing Data Operations Using Spring Boot and MongoDB �������������������������������������������������148 Creating a Spring Boot Project Template Using the Spring Initializer ����������������������������������149 Design and Code the Domain Entities ���������������������������������������������������������������������������������154 Code the Repositories ���������������������������������������������������������������������������������������������������������155 Code the Boot Application Executable ���������������������������������������������������������������������������������157 Build and Package the Spring Boot Application ������������������������������������������������������������������157 Run and Test the Spring Boot Application����������������������������������������������������������������������������158 Developing Using the Spring HAL Browser, HATEOAS ���������������������������������������������������������������163 The HAL Browser �����������������������������������������������������������������������������������������������������������������163 Test HAL REST End Points Using RestTemplate �������������������������������������������������������������������168 Develop a RESTful Web Service ������������������������������������������������������������������������������������������������171 REST Basics �������������������������������������������������������������������������������������������������������������������������171 HTTP Methods for CRUD Resources ������������������������������������������������������������������������������������172 Develop a REST Controller in Spring Boot ���������������������������������������������������������������������������173 Test the REST Controller Using RestTemplate ���������������������������������������������������������������������179 Summary�����������������������������������������������������������������������������������������������������������������������������������182 Chapter 8: Spring Cloud ����������������������������������������������������������������������������������������183 Spring Cloud for Microservices Architecture ����������������������������������������������������������������������������184 Feign Client Usage in Spring Cloud �������������������������������������������������������������������������������������������184 Design a Feign Client Scenario ��������������������������������������������������������������������������������������������185 Code Using Feign Client �������������������������������������������������������������������������������������������������������188 Build and Test the Feign Client ��������������������������������������������������������������������������������������������190 Hystrix Fallback�������������������������������������������������������������������������������������������������������������������������192 Design a Hystrix Fallback Scenario �������������������������������������������������������������������������������������192 Code the Hystrix Fallback Scenario �������������������������������������������������������������������������������������195 Build and Test the Hystrix Fallback Scenario�����������������������������������������������������������������������198 Hystrix Dashboard ���������������������������������������������������������������������������������������������������������������������200 Redesign a Hystrix Fallback Method �����������������������������������������������������������������������������������200 Code the New Design of Hystrix ������������������������������������������������������������������������������������������201 Build and Test the Hystrix Fallback Scenario�����������������������������������������������������������������������205 ix Table of ConTenTs Inspect Hystrix Dashboard ���������������������������������������������������������������������������������������������������206 Ribbon, the Client-Side Load Balancer �������������������������������������������������������������������������������������208 Design a Ribbon Client Scenario �����������������������������������������������������������������������������������������208 Code to Use Ribbon Client ���������������������������������������������������������������������������������������������������209 Build and Test the Ribbon Client ������������������������������������������������������������������������������������������210 Eureka, the Service Registry �����������������������������������������������������������������������������������������������������212 Design a Eureka-Enabled Scenario �������������������������������������������������������������������������������������213 Code to Use Eureka �������������������������������������������������������������������������������������������������������������214 Build and Test the Eureka Sample ���������������������������������������������������������������������������������������220 Bootstrap Your Bootstrap Server �����������������������������������������������������������������������������������������225 Zuul, the API Gateway ���������������������������������������������������������������������������������������������������������������226 The Bootstrap URL ���������������������������������������������������������������������������������������������������������������226 Design a Zuul-Enabled Scenario �����������������������������������������������������������������������������������������231 Code to Use Zuul������������������������������������������������������������������������������������������������������������������232 Build and Test the Zuul Sample �������������������������������������������������������������������������������������������235 The Config Server ���������������������������������������������������������������������������������������������������������������������237 Design a Configuration Scenario �����������������������������������������������������������������������������������������238 Code to Use Config Server ���������������������������������������������������������������������������������������������������238 Build and Test the Config Server �����������������������������������������������������������������������������������������242 Summary�����������������������������������������������������������������������������������������������������������������������������������244 Chapter 9: High Availability and Microservices ���������������������������������������������������245 High Availability �������������������������������������������������������������������������������������������������������������������������246 Measuring High Availability �������������������������������������������������������������������������������������������������246 Baselining High Availability �������������������������������������������������������������������������������������������������247 Decomposing High Availability ��������������������������������������������������������������������������������������������������247 DNS Redundancy �����������������������������������������������������������������������������������������������������������������247 DNS Load Balancing ������������������������������������������������������������������������������������������������������������249 ISP Redundancy �������������������������������������������������������������������������������������������������������������������251 Application Architecture Redundancy ����������������������������������������������������������������������������������253 Data and Storage Redundancy ��������������������������������������������������������������������������������������������255 Highly Available Microservices �������������������������������������������������������������������������������������������������259 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.