Share E-Book

gRPC Microservices in Go (Hüseyin Babal)(Z-Library)

Author

Rating 4.0 AI initial rating

Log in to rate

Go
Language English

gRPC is a high-performance messaging protocol that’s extraordinarily well-suited for microservices applications. Learn how to build fast and secure gRPC-based microservices using the powerful Go programming language. The powerful gRPC Remote Procedure Call framework delivers superior speed and security over protocols like REST. When paired with Golang’s low-level efficiency and flexibility, gRPC and Go become a killer combination for latency-sensitive microservices applications. gRPC Microservices in Go shows you how to utilize these powerful tools to build production-grade microservices. You’ll learn to develop microservice inter-service communication patterns that are powered by gRPC, design backward compatible APIs, and apply hexagonal architecture to microservices.

Format PDF
Size 7.9 MB
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
M A N N I N G Hüseyin Babal
Page 2
gRPC Microservices in Go HÜSEYIN BABAL M A N N I N G SHELTER ISLAND
Page 3
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2024 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. The author and publisher have made every effort to ensure that the information in this book was correct at press time. The author and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from any usage of the information herein. Manning Publications Co. Development editor: Doug Rudder 20 Baldwin Road Technical editor: Bartosz Solowiej PO Box 761 Review editor: Adriana Sabo Shelter Island, NY 11964 Production editor: Deirdre S. Hiam Copy editor: Michele Mitchell Proofreader: Meredith Mix Technical proofreader: Peter Hampton Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781633439207 Printed in the United States of America
Page 4
iii brief contents PART 1 GRPC AND MICROSERVICES ARCHITECTURE ...........................1 1 ■ Introduction to Go gRPC microservices 3 2 ■ gRPC meets microservices 14 PART 2 DEVELOPING, TESTING, AND DEPLOYING A GRPC MICROSERVICE APPLICATION .............................................29 3 ■ Getting up and running with gRPC and Golang 31 4 ■ Microservice project setup 47 5 ■ Interservice communication 65 6 ■ Resilient communication 81 7 ■ Testing microservices 103 8 ■ Deployment 127 PART 3 GRPC AND MICROSERVICES ARCHITECTURE .......................151 9 ■ Observability 153
Page 5
iv contents preface viii acknowledgments x about this book xii about the author xv about the cover illustration xvi PART 1 GRPC AND MICROSERVICES ARCHITECTURE ................ 1 1 Introduction to Go gRPC microservices 3 1.1 Benefits of gRPC microservices 4 Performance 4 ■ Code generation and interoperability 5 ■ Fault tolerance 5 ■ Security 6 ■ Streaming 6 1.2 REST vs. RPC 6 1.3 When to use gRPC 7 Who is this book for? 8 1.4 Production-grade use cases 8 Microservices 9 ■ Container runtime 10 ■ CI/CD pipeline 11 Monitoring and observability 11 ■ Public access 12 2 gRPC meets microservices 14 2.1 Monolithic architecture 15 Development 15 ■ Deployment 15 ■ Scaling 16
Page 6
CONTENTS v 2.2 Scale cube 16 X-axis scaling 16 ■ Z-axis scaling 17 ■ Y-axis scaling 17 2.3 Microservice architecture 17 Handling data consistency 18 ■ Saga pattern 18 ■ Choreography- based saga 19 ■ Orchestrator-based saga 21 2.4 Service discovery 22 2.5 Using gRPC for interservice communication 23 Working with protocol buffers 24 ■ Generating Go source code 25 ■ Connecting wires 27 PART 2 DEVELOPING, TESTING, AND DEPLOYING A GRPC MICROSERVICE APPLICATION ....................................29 3 Getting up and running with gRPC and Golang 31 3.1 Protocol buffers 32 Defining message type 32 ■ Protocol buffer encoding 33 3.2 Generating stubs 36 Protocol buffer compiler installation 36 ■ Using the protocol buffer compiler 36 3.3 Maintaining .proto files 38 Proto project structure 38 ■ Automation for source code generation 40 3.4 Backward and forward compatibility 43 Adding new fields 43 ■ Upgrading the server but not the client 43 Upgrading the client but not the server 44 ■ Adding/removing oneof fields 44 ■ Moving fields out of or into oneof fields 45 4 Microservice project setup 47 4.1 Hexagonal architecture 47 Application 48 ■ Actors 48 ■ Ports 48 ■ Adapters 48 4.2 Order service implementation 49 Project folders 50 ■ Initializing a Go project 51 ■ Implementing the application core 51 ■ Implementing ports 53 ■ Implementing adapters 54 ■ Implementing a gRPC adapter 57 ■ Dependency injection and running the application 61 ■ Calling a gRPC endpoint 63
Page 7
CONTENTSvi 5 Interservice communication 65 5.1 gRPC service-to-service communication 66 Server-side load balancing 66 ■ Client-side load balancing 67 5.2 Depending on a module and implementing ports and adapters 68 Payment port 69 ■ Payment adapter 69 ■ Implementing the payment port 70 ■ Implementing the payment adapter 71 Client configuration for a payment stub 72 ■ Using a payment adapter in gRPC 73 5.3 Error handling 75 Status codes 75 ■ Returning an error code and message 76 Errors with details 77 ■ Handling errors on the client side 78 Running the Payment service 80 6 Resilient communication 81 6.1 Resiliency patterns 82 Timeout pattern 82 ■ Retry pattern 85 ■ Circuit breaker pattern 87 6.2 Error handling 92 gRPC error model 93 ■ gRPC error response 93 6.3 Securing gRPC communication 95 TLS handshake 95 ■ Certificate generation 97 ■ gRPC TLS credentials 99 7 Testing microservices 103 7.1 Testing pyramid 104 7.2 Testing with a unit test 105 System under test 105 ■ Test workflow 106 ■ Working with mocks 107 ■ Implementing a mock 108 ■ Automatic mock generation 111 7.3 Integration testing 112 Test suite preparation 112 ■ Working with Testcontainers 113 7.4 End-to-end tests 115 Specifications 116 ■ Understanding Docker Compose service definitions 116 ■ End-to-end test folder structure 117 ■ Database layer 117 ■ The Payment service layer 118 ■ The Order service layer 119 ■ Running tests against the stack 120 7.5 Test coverage 123 Coverage information 123 ■ Testing in a CI pipeline 124
Page 8
CONTENTS vii 8 Deployment 127 8.1 Docker 128 Building images 128 8.2 Kubernetes 130 Kubernetes architecture 130 ■ Kubernetes resources 132 Eagle view of microservices deployment 133 ■ Pod 134 Deployment 135 ■ Service 137 ■ NGINX Ingress controller 140 8.3 Certificate management 141 Installation 142 ■ ClusterIssuer 142 ■ Certificate usage in Ingress 143 ■ Certificates on the client side 144 8.4 Deployment strategies 144 RollingUpdate 145 ■ Blue-Green Deployment 146 ■ Canary deployment 147 ■ Final thoughts on deployment 148 PART 3 GRPC AND MICROSERVICES ARCHITECTURE .............151 9 Observability 153 9.1 Observability 154 Traces 154 ■ Metrics 155 ■ Logs 157 9.2 OpenTelemetry 158 Instrumentation locations 159 ■ Instrumentation 159 ■ Metric backend 159 ■ Service performance monitoring 160 9.3 Observability in Kubernetes 161 Jaeger All in One 161 ■ OpenTelemetry Collector 162 Prometheus 162 ■ Jaeger installation 162 ■ OpenTelemetry interceptor for the Order service 163 ■ Understanding the metrics of the Order service 165 ■ Application logging 168 ■ Logs collection 169 ■ Elasticsearch as a logging backend 170 Kibana as a logging dashboard 172 index 175
Page 9
viii preface I started as a software developer in 2007, and over the years, I’ve seen the evolution of web-based projects and the shift toward enterprise applications. In 2014, I was introduced to microservices and have advocated for this modern software architecture ever since. In my journey with microservices, I’ve used various technologies to develop and deploy microservices to production environments. A tool that caught my attention was gRPC, which is a high-performance, open-source framework developed by Google and built on top of HTTP/2. I was fascinated by gPRC’s potential regarding microservices communication. Its support for multiple languages, including Go, and its ease of building cloud-native applications made it an obvious choice for me. However, I soon realized that there weren’t enough resources to cover all the pieces of microservices to help developers like me get started. There were individual tutorials like the Hello World program and a couple about Protobuf, but no compre- hensive guides existed that explained gRPC and Protobuf with production-grade examples. Over time, I worked through the processes and learned to use gRPC to build microservices that communicate effectively with each other. gRPC has never let me down. That’s why I decided to write this book. I wanted to provide a comprehensive resource to help developers get up to speed with the technology and put it into prac- tice. In this book, I have drawn on my experience with microservices and gRPC to pro- vide a complete guide to building microservices with both gRPC and Protobuf. We’ll write the code in Go, a popular programming language for building cloud-native
Page 10
PREFACE ix applications and microservices. Throughout the book, we’ll use an e-commerce appli- cation, deployed on Kubernetes, as an example to guide you on your own journey into gRPC and microservices. We’ll also cover advanced topics such as error handling, test- ing, and security, which are vital for building product-grade applications. I wrote this book to make it easier for developers like me to get started with gRPC, and I hope you’ll find it a valuable resource. Thank you for choosing gRPC Microser- vices in Go.
Page 11
x acknowledgments Writing a book is never easy, and writing gRPC Microservices in Go was no exception. I have spent countless hours working on this book both in various cafes and at home, and while traveling, to produce comprehensive content. First and foremost, I want to thank my beautiful wife, Emel, who has supported me throughout this journey. She has always encouraged me to pursue my passion, even when I was exhausted and felt like giving up. Her unwavering support and under- standing have been a constant source of inspiration for me. Next, I want to extend my heartfelt gratitude to my editor, Doug Rudder, for his support during several months of work on this book. His feedback, suggestions, and edits have been invaluable, and I could not have completed this book without him. I also want to thank my technical editor, Bartosz Solowiej; his attention to detail and thoroughness helped me refine and polish the technical aspects of the book. My thanks to everyone else at Manning: my project editor, Deirdre Hiam; my copyeditor, Michelle Mitchell; and my proofreader, Meredith Mix. I want to thank the reviewers who provided valuable feedback during the writing process. Your comments and suggestions helped me improve the book’s content and make it more useful for readers. Thank you to Alain Couniot, Alceu Rodrigues de Fre- itas Junior, Alessandro Campeis, Andrea Monacchi, Ashish Kumar Pani, Borko Djurkovic, Cameron Singe, Dr. Keith L Mannock, Dylan Guedes, Fatih Akturk, Ger- mano Rizzo, Gowtham Sadasivam, Horaci Macias, Jeelani Shaik, Christian B. Madsen, Joel Holmes, Jonathan Reeves, Karthikeyarajan Rajendran, Kelum Prabath Senanay- ake, Manzur Mukhitdinov, Marco Massenzio, Michael Haller, Mikael Dautrey, Muneeb
Page 12
ACKNOWLEDGMENTS xi Shaikh, Neil Croll, Nolan To, George Onofrei, Peter Hampton, Rahul Modpur, Rich Yonts, Ryan Burrows, Ryan Huber, Satadru Roy, Stanley Anozie, Syed Basheeruddin Ahmed, Tim Homan, Vadim Turkov, and Walter Alexander Mata Lopez. Finally, I want to express my sincere gratitude to the Go, gRPC, and Kubernetes communities. Your contributions, support, and enthusiasm have been a constant source of motivation and inspiration for me. I am honored to be part of such a vibrant and dynamic community. Thank you all for your support and encouragement throughout this journey.
Page 13
xii about this book gRPC Microservices in Go was written for anyone who wants to apply production-grade practices from gRPC, Go, and/or Kubernetes to microservice applications that run in a live environment. It starts with microservices theory and cloud-native application development, then dives into the technical development of microservices in Kuberne- tes using Go and gRPC. Who should read this book This book will help developers design and implement their microservices projects using Go and gRPC in the Kubernetes environment. There are some examples of Kubernetes deployment and gRPC usage online, but this book provides a step-by-step explanation of a gRPC microservice life cycle, from beginning to production. For this reason, this book will be a good reference for architects, chief technology officers, and engineering managers in applying microservice principles to development life cycles. How this book is organized: A road map This book has three sections that cover nine chapters. Part 1 contains theoretical information about microservices and gRPC: ■ Chapter 1 briefly introduces gRPC microservices and provides an overview of the book. ■ Chapter 2 discusses microservices, including their communication patterns, and explains how to use gRPC during development.
Page 14
ABOUT THIS BOOK xiii Part 2 provides step-by-step instructions for implementing gRPC microservices and deploying them to the Kubernetes environment: ■ Chapter 3 provides information about installing gRPC and related tools (e.g., Protobuf) for project development. ■ Chapter 4 explains hexagonal architecture and how to use it in microservices. It also explains how to structure a Go project with gRPC clients and servers to build a microservices project. ■ Chapter 5 explains how gRPC client–server interaction can be used in microser- vice service-to-service communication. ■ Chapter 6 describes how important resiliency is and how to apply resiliency pat- terns to gRPC service communications. The primary goals of this chapter are reader understanding of failover scenarios and recovering from them. ■ Chapter 7 explains how to write unit and integration tests for microservices. This is especially important for the microservices environment because many changing pieces must be maintained properly. ■ Chapter 8 goes deep into some of the Kubernetes resources and shows how to use them to deploy gRPC microservices to the Kubernetes environment. This chapter also covers how to expose services to the public. Part 3 covers gRPC microservice observability in the Kubernetes environment: ■ Chapter 9 focuses on observability and demonstrates how to integrate observ- ability tools into gRPC microservices for better visibility on the entire platform. About the code This book contains many examples of source code both in numbered listings and in line with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. Sometimes code is also in bold to high- light code that has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code. In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and listings include line-continuation markers (➥). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts. You can get executable snippets of code from the liveBook (online) version of this book at https://livebook.manning.com/book/grpc-microservices-in-go. The complete code for the examples in the book is available for download from the Manning web- site at https://www.manning.com/books/grpc-microservices-in-go, and from GitHub at https://github.com/huseyinbabal/microservices.
Page 15
ABOUT THIS BOOKxiv liveBook discussion forum Purchase of gRPC Microservices in Go includes free access to liveBook, Manning’s online reading platform. Using liveBook’s exclusive discussion features, you can attach com- ments to the book globally or to specific sections or paragraphs. It’s a snap to make notes for yourself, ask and answer technical questions, and receive help from the author and other users. To access the forum, go to https://livebook.manning.com/ book/grpc-microservices-in-go/discussion. You can also learn more about Manning’s forums and the rules of conduct at https://livebook.manning.com/discussion. Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We sug- gest you try asking the author some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.
Page 16
xv about the author HÜSEYIN BABAL has been a software developer since 2007. Since 2016, he has built cloud-native applications with Kubernetes in major cloud providers. He has also worked with enterprise companies to help them with their DevOps and microservice transition projects. You can find him giving presentations at global conferences or doing live coding sessions on his Twitch channel.
Page 17
xvi about the cover illustration The figure on the cover of gRPC Microservices in Go is “Bohemienne de Prague,” or “Bohemian woman from Prague,” taken from a collection by Jacques Grasset de Saint- Sauveur, published in 1797. Each illustration is finely drawn and colored by hand. In those days, it was easy to identify where people lived and what their trade or sta- tion in life was just by their dress. Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional cul- ture centuries ago, brought back to life by pictures from collections such as this one.
Page 18
Part 1 gRPC and microservices architecture There are three types of companies in software architecture: those that are content with their monolith applications, those that plan to switch to microser- vices, and those that already use microservices in production. Each company has its reasons for choosing a particular architecture, but using microservices comes with its own set of challenges. For instance, when you decide to break down a monolith application into services, you need to figure out how to manage com- munication between the services. In part 1, we will first look at the big picture of an e-commerce application and then delve into microservices architecture and its critical requirements, such as fault tolerance, security, continuous integration/continuous deployment (CI/CD), public access, and scaling, among others. Proper communication pat- terns between microservices are essential, and we will cover this topic as well. I will also introduce gRPC and show how it fits into a microservices environment for service-to-service communication. You will become familiar with gRPC and see how it prioritizes security and performance to give you a seamless experience.
Page 19
(This page has no text content)
Page 20
3 Introduction to Go gRPC microservices Good architecture design and proper technology selection help ensure a high- quality product by eliminating repetitive work and providing the best tool kit for software development and maintenance. While microservice architecture can be implemented in any language, Go is particularly suited for building high- performance cloud-native distributed applications such as microservices in Kubernetes on a large scale. Microservices with gRPC communication have already enabled many companies to implement their products with small services based on their business capabilities and have let those services communicate smoothly with each other and the public. With the help of Go, the distribution of those services becomes easier due to its fast compilation, ability to generate executable binaries, This chapter covers Introducing Go gRPC microservices Comparing gRPC with REST Understanding when to use gRPC Applying gRPC microservices to production-grade use cases
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List