(This page has no text content)
Python Microservices Development Second Edition Build efficient and lightweight microservices using the Python tooling ecosystem Simon Fraser Tarek Ziadé BIRMINGHAM—MUMBAI Python Microservices Development Second Edition Copyright © 2021 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Producer: Shailesh Jain Acquisition Editor – Peer Reviews: Saby D'Silva Project Editor: Rianna Rodrigues Content Development Editor: Alex Patterson Copy Editor: Safis Editing Technical Editor: Karan Sonawane Proofreader: Safis Editing Indexer: Rekha Nair Presentation Designer: Pranit Padwal First published: July 2017 Second edition: September 2021
Production reference: 1280921 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80107-630-2 www.packt.com
Contributors About the authors Simon Fraser is a Site Reliability Engineer for Cisco Meraki. He has over twenty years of experience in computing, both developing and running systems. He has worked as a Systems Administrator for an internet service provider; an infrastructure and high-performance computing engineer at the Welcome Sanger Institute, and as a Firefox Release Engineer at Mozilla. He studied Cybernetics and Computer Science at the University of Reading and has also taught academic courses in programming for scientists. I would like to thank Emma Apted and Helen Cook for their frequent support and encouragement, ranging from proofreading to miming a book opening when I'm clearly not working. My father George Fraser passed on his immeasurable skill at finishing a project just before the deadline, and my mother Elizabeth Fraser showed me how to explain concepts and inform people through years of teaching. This book would also not be possible without Tarek Ziadé's work on the 1st edition and the solid base on which to build, as well as the work of Development Editor Alex Patterson, Reviewer William Kahn-Greene, and Project Editor Rianna Rodrigues. We might stand on the shoulders of giants, but we also build on a huge structure made by every one of us, so I would also like to thank everyone I've worked with over the years in all my different teams. Yes, even you. Tarek Ziadé is a Software Engineer, located in Burgundy, France. He works at Elastic, building tools for developers. Before Elastic, he worked at Mozilla for 10 years, and he founded a French Python user group, called AFPy. Tarek has also written several articles about Python for various magazines, and a few books in French and English.
I would like to thank Freya, Suki, Milo, and Amina for being so supportive of all my book projects. About the reviewer Will Kahn-Greene has been building things in Python and other languages since the late 90s. He manages the crash ingestion pipeline at Mozilla and maintains several Python libraries. When he's waiting for CI to run tests, he builds things with wood, tends his tomato plants, and cooks for four.
Contents Preface Who this book is for What this book covers To get the most out of this book Get in touch 1. Understanding Microservices The origins of service-oriented architecture The monolithic approach The microservice approach Microservice benefits Separation of concerns Smaller projects Scaling and deployment Pitfalls of microservices Illogical splitting More network interactions Data storing and sharing Compatibility issues Testing Implementing microservices with Python How web services work The WSGI standard Workers, threads, and synchronicity A worker pool approach Being asynchronous Twisted, Tornado, Greenlets, and Gevent Asynchronous Python Language performance Summary 2. Discovering Quart Making sure we have Python How Quart handles requests Routing Variables and converters
The url_for function Request Response Quart's built-in features The session object Globals Signals Extensions and middleware Templates Configuration Blueprints Error handling Custom error handler A microservice skeleton Summary 3. Coding, Testing, and Documentation: the Virtuous Cycle Different kinds of tests Unit tests Functional tests Integration tests Load tests End-to-end tests Using pytest and tox Developer documentation Tools for documentation Version control Continuous Integration and Continuous Deployment GitHub Actions Documentation Coveralls Badges Summary 4. Designing Jeeves The Jeeves bot User stories Monolithic design Model
View and template A human-readable view Slack workspaces Taking actions OAuth tokens Authentication and authorization Background tasks Putting together the monolithic design Summary 5. Splitting the Monolith Identifying potential microservices Code complexity and maintenance Metrics and Monitoring Logging Splitting a Monolith Feature Flags Refactoring Jeeves Workflow Summary 6. Interacting with Other Services Calling other web resources Finding out where to go Environment variables Service discovery Transferring data HTTP cache headers GZIP compression Protocol Buffers MessagePack Putting it together Asynchronous messages Message queue reliability Basic queues Topic exchanges and queues Publish/subscribe Putting it together Testing
Using OpenAPI Summary 7. Securing Your Services The OAuth2 protocol X.509 certificate-based authentication Token-based authentication The JWT standard PyJWT Using a certificate with JWT The TokenDealer microservice The OAuth implementation Using TokenDealer Securing your code Limiting your application scope Untrusted incoming data Redirecting and trusting queries Sanitizing input data Using Bandit linter Dependencies Web application firewall OpenResty: Lua and nginx Rate and concurrency limiting Other OpenResty features Summary 8. Making a Dashboard Building a ReactJS dashboard The JSX syntax React components Pre-processing JSX ReactJS and Quart Cross-origin resource sharing Authentication and authorization A note about Micro Frontends Getting the Slack token JavaScript authentication Summary 9. Packaging and Running Python
The packaging toolchain A few definitions Packaging The setup.py file The requirements.txt file The MANIFEST.in file Versioning Releasing Distributing Running all microservices Process management Summary 10. Deploying on AWS What is Docker? Introduction to Docker Running Quart in Docker Docker-based deployments Terraform Service discovery Docker Compose Docker Swarm Kubernetes Summary 11. What's Next? Automation Scaling Content Delivery Networks Multi-cloud deployments Lambda Functions Expanding monitoring Making promises Summary Other Books You May Enjoy Index
Preface Python Microservices Development introduces the design and creation of a microservice-based application using the popular Python programming language, and the Quart web framework. In this book, you will learn about microservice architecture and how it differs from the traditional monolithic approach, what the benefits are, as well as potential trouble that needs overcoming.
Who this book is for This book is for people familiar with the fundamentals of the Python programming language who want to start writing web services, or who have inherited a web service they want to modernize. It is expected that the reader will be familiar with simple Python structures such as functions and loops, as well as some more advanced features such as decorators. Some familiarity with using web applications would be helpful, although the fundamentals of application design are covered.
What this book covers Chapter 1, Understanding Microservices, introduces the concepts behind microservices, the differences between monolithic applications and microservices, common benefits and pitfalls, as well as testing and scaling. Chapter 2, Discovering Quart, covers the Quart web framework and the ways in which it can respond to requests, create templated documents, act as middleware, handle errors, and read configuration. Chapter 3, Coding, Testing, and Documentation: the Virtuous Cycle, teaches you about the different types of testing that are possible, what benefits each type has, and how to set up automatic testing, as well as generating documentation in CI pipelines. Chapter 4, Designing Jeeves, looks at Jeeves, which is the sample application we use in this book to explain the various concepts behind microservices. We introduce what we need Jeeves to do and describe a monolithic approach to application design, covering the web API interface, database use, and worker pools. Chapter 5, Splitting the Monolith, builds on the monolithic Jeeves described in previous chapter. This chapter offers guidance on how to identify components that may be good microservices, measuring the effects of changes on the software architecture, and how to cleanly migrate features to new microservices. Chapter 6, Interacting with Other Services, explains how to make web requests to other services, how to configure and decide where to send those queries, and how to cache results, as well as make the data transfer more efficient. Chapter 7, Securing Your Services, looks at how authentication, tokens, encryption, and security vulnerabilities are all essential topics to consider
for any service, and here we build an authentication microservice for Jeeves, as well as discuss various things to consider when making an application secure against attack. Chapter 8, Making a Dashboard, discusses how many applications will have a human view. In this chapter we add a dashboard to Jeeves so that it can be controlled using a ReactJS application, as well as discussing where best to add the front-end into the microservice architecture. Chapter 9, Packaging and Running Python, shows how, once created, an application needs to be packaged so that it can be deployed and run. In this chapter, we will learn about creating and publishing Python packages, as well as managing dependencies. Chapter 10, Deploying on AWS, looks at how cloud services provide a flexible platform to run a web service. This chapter covers creating containers for our application and deploying it in Amazon Web Services. Chapter 11, What's Next?, summarizes the main things we have learned so far, and the topics that would make good further reading, as well as technologies that will prove useful for different types of application. To get the most out of this book Readers will find the book easier to follow with some Python programming experience, or experience in a very similar programming language. The basics of Python are not covered, and for the fundamentals we recommend a book such as Learn Python Programming, also from Packt Publishing. Running the code samples from this book requires a computer with Python 3.8 or greater installed on it. Python is available for free for all popular operating systems, such as Windows, macOS and Linux, among others. The author recommends not only running the code samples, but also experimenting with them to try different things out.
Download the example code files The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Python- Microservices-Development-2nd-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out! Download the color images We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt- cdn.com/downloads/9781801076302_ColorImages.pdf. Conventions used There are a number of text conventions used throughout this book. CodeInText : Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example; " The __name__ variable, whose value will be __main__ when you run that single Python module, is the name of the application package." A block of code is set as follows: @app.route('/api', methods=['POST', 'DELETE', 'GET']) def my_microservice(): return {'Hello': 'World!'} Any command-line input or output is written as follows: pip install quart
Bold: Indicates a new term, an important word, or words that you see on the screen, for example, in menus or dialog boxes. For example: "There are many great synchronous frameworks to build microservices with Python, like Bottle, Pyramid with Cornice, or Flask." Warnings or important notes appear like this. Tips and tricks appear like this. Get in touch Feedback from our readers is always welcome. General feedback: Email feedback@packtpub.com , and mention the book's title in the subject of your message. If you have questions about any aspect of this book, please email us at questions@packtpub.com . Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book we would be grateful if you would report this to us. Please visit http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details. Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material. If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.
Share your thoughts Once you've read Python Microservices Development, Second Edition, we'd love to hear your thoughts! click here to go straight to the Amazon review page for this book and share your feedback. Your review is important to us and the tech community and will help us make sure we're delivering excellent quality content.
1 Understanding Microservices We are always trying to improve how we create software. Computer programming is less than 100 years old, and we have evolved rapidly through technology, design, and philosophy to improve the tools and applications we produce. Microservices have revolutionized software products by improving the readability and scalability of services, and have allowed organizations to speed up their release cycles and be more responsive to the needs of their customers. Everybody wants to ship new products and new features to their customers as fast as possible. They want to be agile by iterating often, and they want to ship, ship, and ship again. With thousands of customers using your service simultaneously, it is considered good practice to push an experimental feature to production and remove it again if needed, instead of waiting for months to publish it and many other features at the same time. Companies such, as Netflix, are promoting their continuous delivery techniques where small changes are made very often in production and tested on a subset of the user base. They've developed tools such as Spinnaker (http://www.spinnaker.io/) to automate as many steps as possible to update production and ship their features in the cloud as independent microservices. But if you read Hacker News or Reddit, it can be quite hard to untangle what's useful for you and what's just buzzword-compliant journalistic-style information. As Edsger Dijkstra, noted computer science researcher and discoverer of the famous shortest-path routing algorithm, put it:
"Write a paper promising salvation, make it a structured something or a virtual something, or abstract, distributed or higher-order or applicative and you can almost be certain of having started a new cult." —Edsger W. Dijkstra This book will take you through the creation of a traditional monolithic service and provide guidance on how to identify components that will be more effective as microservices. We will cover ways to integrate with other services, pass messages and schedule tasks, and securely deploy our service in Amazon Web Services. This chapter is going to help you understand what microservices are, and will then focus on the various ways in which you can implement them using Python. It is composed of the following sections: The origins of service-oriented architecture The monolithic approach to building an application The microservices approach to building applications Benefits of microservices Potential pitfalls in microservices Implementing microservices with Python Hopefully, once you've reached the end of the chapter, you will be able to dive into the rest of the book and build microservices with a good understanding of what they are and what they're not—and how you can use Python.
Loading comments...
Reply to Comment
Edit Comment