📄 Page
1
(This page has no text content)
📄 Page
2
(This page has no text content)
📄 Page
3
(This page has no text content)
📄 Page
4
(This page has no text content)
📄 Page
5
Learn Microservices with Spring Boot 3 A Practical Approach Using Event- Driven Architecture, Cloud-Native Patterns, and Containerization Third Edition Moisés Macero García Tarun Telang Learn Microservices with Spring Boot 3: A Practical Approach Using Event-Driven Architecture, Cloud-Native Patterns, and Containerization, Third Edition Moisés Macero García Tarun Telang New York, NY, USA Hyderabad, Telangana, India ISBN-13 (pbk): 978-1-4842-9756-8 ISBN-13 (electronic): 978-1-4842-9757-5 https://doi.org/10.1007/978-1-4842-9757-5 Copyright © 2023 by Moisés Macero García and Tarun Telang
📄 Page
6
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
📄 Page
7
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: Melissa Duffy Development Editor: Laura Berendson Coordinating Editor: Gryffin Winkler Copy Editor: Kezia Endsley Cover designed by eStudioCalamar Cover image by Image by James DeMers on Pixabay (www.pixabay.com) Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. 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 booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
📄 Page
8
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://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 (https://github.com/Apress). For more detailed information, please visit https://www. apress.com/gp/services/source-code. Paper in this product is recyclable This book is dedicated to my grandparents, parents, wife Nikita, and son Vihan. They have always been a source of inspiration and encouragement to me. It’s also for all of the software and technology creators who work hard to make our planet a better place to live. —Tarun Telang Table of Contents About the Authors ������������������������������ ������������������������������ ������������������������������ �������� xiii About the Technical Reviewer ������������������������������ ������������������������������ ���������������������xv Acknowledgments ������������������������������ ������������������������������ ������������������������������ �������xvii About this Book ������������������������������
📄 Page
9
������������������������������ ������������������������������ ������������������������������ ������������xix Chapter 1: Setting the Scene ������������������������������ ������������������������������ ������������������������ 1 Who Are You? ������������������������������ ������������������������������ ������������������������������ ������������������������������ �������� 3 How Is This Book Different from Other Books and Guides? ������������������������������ ���������������������������� 5 Learning: An Incremental Process ������������������������������ ������������������������������ ������������������������������ � 5 Is This a Guide or a Book? ������������������������������ ������������������������������ ������������������������������ ������������� 5 From Basics to Advanced Topics ������������������������������ ������������������������������ ������������������������������ ��������� 6
📄 Page
10
Skeleton with Spring Boot, the Professional Way ������������������������������ ������������������������������ �������� 7 Test-Driven Development ������������������������������ ������������������������������ ������������������������������ �������������� 7 Microservices ������������������������������ ������������������������������ ������������������������������ ������������������������������ �� 7 Event-Driven Systems ������������������������������ ������������������������������ ������������������������������ ������������������� 8 Nonfunctional Requirements ������������������������������ ������������������������������ ������������������������������ ��������� 8 Online Content ������������������������������ ������������������������������ ������������������������������ ������������������������������ ������ 9 ��������������������������
📄 Page
11
Summary�������������������������� ������������������������������ ������������������������������ ������������������������������ ������������������� 9 Chapter 2: Basic Concepts ������������������������������ ������������������������������ �������������������������� 11 Spring��������������������������� ������������������������������ ������������������������������ ������������������������������ ��������������������� 11 Spring Boot ������������������������������ ������������������������������ ������������������������������ ������������������������������ ��������� 16 Lombok and Java ������������������������������ ������������������������������ ������������������������������ ������������������������������ 18 Testing Basics ������������������������������ ������������������������������ ������������������������������ ������������������������������ ����� 21
📄 Page
12
Test-Driven Development ������������������������������ ������������������������������ ������������������������������ ������������ 21 Behavior-Driven Development ������������������������������ ������������������������������ ������������������������������ ����� 22 v Table of ConTenTs JUnit 5 ������������������������������ ������������������������������ ������������������������������ ������������������������������ ����������� 23 Mockito ������������������������������ ������������������������������ ������������������������������ ������������������������������ ���������� 23 AssertJ ������������������������������ ������������������������������ ������������������������������ ������������������������������ ���������� 27
📄 Page
13
Testing in Spring Boot ������������������������������ ������������������������������ ������������������������������ ����������������������� 28 Logging ������������������������������ ������������������������������ ������������������������������ ������������������������������ ��������������� 29 Summary and Achievements ������������������������������ ������������������������������ ������������������������������ ������������ 30 Chapter 3: A Basic Spring Boot Application ������������������������������ ����������������������������� 33 Setting Up the Development Environment ������������������������������ ������������������������������ ���������������������� 34 Java Development Kit 17 ������������������������������ ������������������������������ ������������������������������ ������������� 34 Integrated Development Environment (IDE) ������������������������������ ������������������������������
📄 Page
14
������������������������������ ��������������� 34 HTTPie ������������������������������ ������������������������������ ������������������������������ ������������������������������ ����������� 35 POST Request ������������������������������ ������������������������������ ������������������������������ ������������������������������ 36 cURL ������������������������������ ������������������������������ ������������������������������ ������������������������������ �������������� 36 The Skeleton Web App ������������������������������ ������������������������������ ������������������������������ ���������������������� 36 Spring Boot Autoconfiguration ������������������������������ ������������������������������ ������������������������������ ���������� 41
📄 Page
15
Three-Tier, Three-Layer Architecture ������������������������������ ������������������������������ ������������������������������ 47 Modeling the Domain ������������������������������ ������������������������������ ������������������������������ ������������������������ 50 Domain Definition and Domain-Driven Design ������������������������������ ������������������������������ ���������� 50 Domain Classes ������������������������������ ������������������������������ ������������������������������ ��������������������������� 52 Business Logic ������������������������������ ������������������������������ ������������������������������ ������������������������������ ���� 55 What You Need ������������������������������ ������������������������������ ������������������������������ ���������������������������� 55
📄 Page
16
Random Challenges ������������������������������ ������������������������������ ������������������������������ ��������������������� 55 Attempt Verification ������������������������������ ������������������������������ ������������������������������ ��������������������� 59 Presentation Layer ������������������������������ ������������������������������ ������������������������������ ���������������������������� 64 REST ������������������������������ ������������������������������ ������������������������������ ������������������������������ �������������� 64 REST APIs with Spring Boot ������������������������������ ������������������������������ ������������������������������ ��������� 65 Designing the APIs ������������������������������ ������������������������������ ������������������������������ ����������������������� 66
📄 Page
17
Your First Controller ������������������������������ ������������������������������ ������������������������������ ��������������������� 67 How Automatic Serialization Works ������������������������������ ������������������������������ ��������������������������� 69 Testing Controllers with Spring Boot ������������������������������ ������������������������������ ������������������������� 73 Summary and Achievements ������������������������������ ������������������������������ ������������������������������ ������������ 86 vi Table of ConTenTs Chapter 4: A Minimal Frontend with React ������������������������������ ������������������������������ � 89 A Quick Intro to React and Node ������������������������������ ������������������������������ ������������������������������ ������� 90
📄 Page
18
Setting Up the Development Environment ������������������������������ ������������������������������ ���������������������� 91 The React Skeleton ������������������������������ ������������������������������ ������������������������������ ��������������������������� 92 A JavaScript Client ������������������������������ ������������������������������ ������������������������������ ���������������������������� 94 The Challenge Component ������������������������������ ������������������������������ ������������������������������ ���������������� 96 The Main Structure of a Component ������������������������������ ������������������������������ �������������������������� 99 Rendering ������������������������������ ������������������������������ ������������������������������ ������������������������������ ���� 101 Integration with the App ������������������������������ ������������������������������
📄 Page
19
������������������������������ ������������������������������ ������������ 104 Running the Frontend the First Time ������������������������������ ������������������������������ ����������������������������� 105 Debugging ������������������������������ ������������������������������ ������������������������������ ������������������������������ ��������� 105 Adding CORS Configuration to the Spring Boot App ������������������������������ ������������������������������ ������ 107 Playing with the Application ������������������������������ ������������������������������ ������������������������������ ������������ 108 Deploying the React App ������������������������������ ������������������������������ ������������������������������ ����������������� 109 Summary and Achievements ������������������������������ ������������������������������ ������������������������������
📄 Page
20
������������������������������ ���������� 112 Chapter 5: The Data Layer ������������������������������ ������������������������������ ������������������������ 115 The Data Model ������������������������������ ������������������������������ ������������������������������ ������������������������������ � 117 Choosing a Database ������������������������������ ������������������������������ ������������������������������ ���������������������� 119 SQL vs� NoSQL ������������������������������ ������������������������������ ������������������������������ ��������������������������� 121 H2, Hibernate, and JPA ������������������������������ ������������������������������ ������������������������������ �������������� 122 Spring Boot Data JPA ������������������������������ ������������������������������ ������������������������������