Book Image

Python Microservices Development

Book Image

Python Microservices Development

Overview of this book

We often deploy our web applications into the cloud, and our code needs to interact with many third-party services. An efficient way to build applications to do this is through microservices architecture. But, in practice, it's hard to get this right due to the complexity of all the pieces interacting with each other. This book will teach you how to overcome these issues and craft applications that are built as small standard units, using all the proven best practices and avoiding the usual traps. It's a practical book: you’ll build everything using Python 3 and its amazing tooling ecosystem. You will understand the principles of TDD and apply them. You will use Flask, Tox, and other tools to build your services using best practices. You will learn how to secure connections between services, and how to script Nginx using Lua to build web application firewall features such as rate limiting. You will also familiarize yourself with Docker’s role in microservices, and use Docker containers, CoreOS, and Amazon Web Services to deploy your services. This book will take you on a journey, ending with the creation of a complete Python application based on microservices. By the end of the book, you will be well versed with the fundamentals of building, designing, testing, and deploying your Python microservices.
Table of Contents (20 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Introduction

Preface

If we try to deploy our web applications into the cloud, it requires our code to interact with many third-party services. Using microservice architectures, you can build applications that will allow you to manage these interactions. However, this comes with its own set of challenges, since each set has its own complexity, and getting their interaction right isn't easy. This easy-to-follow guide covers techniques to help you overcome these challenges. You will learn how to best design, write, test, and deploy your microservices. The real-world examples will help Python developers create their own Python microservices using the most efficient methods. By the end of this book, you will have acquired the skills to craft applications that are built as small standard units, using all the proven best practices and avoiding the usual traps. Also, this is a useful guide for the vast community of Python developers who are shifting from monolithic design to the new microservice-based development paradigm.

What this book covers

Chapter 1, Understanding Microservices, defines what microservices are, and their roles in modern web applications. It also introduces Python and explains why it's great for building microservices.

Chapter 2, Discovering Flask, introduces Flask and goes through its main features. It showcases the framework with a sample web application that will be the basis for building microservices.

Chapter 3, Coding, Testing, and Documenting - the Virtuous Cycle, describes the Test-Driven Development and Continuous Integration approach, and how to use it in practice to build and package Flask applications.

Chapter 4, Designing Runnerly, takes you through the app features and user stories, explains how it could be built as a monolithic app, then decomposes it into microservices and explains how they interact with the data. It will also introduce the Open API 2.0 specification (ex-Swagger), which can be used to describe HTTP APIs.

Chapter 5, Interacting with Other Services, explains how a service interacts with backend services, how to deal with network splits and other interaction problems, and how to test the service in isolation.

Chapter 6, Securing Your Services, explains how to secure your microservices and how to deal with user authentication, service-to-service authentication, as well as user management. It will also introduce the reader to fraud and abuse, and how to mitigate it.

Chapter 7, Monitoring Your Services, explains how to add logging and metrics in your code, and how to make sure you have a clear global understanding of what's going on in your application to track down issues and understand your services usage.

Chapter 8, Bringing It All Together, describes how to design and build a JavaScript application that leverages and uses the microservices in an end-user interface.

Chapter 9, Packaging and Running Runnerly, describes how to package, build, and run the whole Forrest application. As a developer, it's vital to be able to run all the parts that compose your application into a single dev box.

Chapter 10, Containerized Services, explains what is virtualization, how to use Docker, and also how to Dockerize your services.

Chapter 11, Deploying on AWS, introduces you to existing cloud service providers and then to the AWS world, and shows how to instantiate servers and use the major AWS services that are useful to run a microservices-based application. It also introduces CoreOS, a Linux distribution specifically created to deploy Docker containers in the cloud.

Chapter 12, What Next?, concludes the book by giving some hints on how your microservices can be built independently from specific cloud providers and virtualization technologies, to avoid the trap of putting all your eggs in the same basket. It emphasizes what you learned in Chapter 9, Packaging and Running Runnerly.

What you need for this book

To execute the commands and applications in this book, you will need Python 3.x, Virtualenv 1.x, and Docker CE installed on your system. Detailed instructions are given in the chapters where needed.

Who this book is for

If you are a developer who has basic knowledge of Python, the command line, and HTTP-based application principles, and who wants to learn how to build, test, scale, and manage Python 3 microservices, then this book is for you. No prior experience of writing microservices in Python is assumed.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The only hint we're using async is the async keyword, which marks the handle function as being a coroutine."

A block of code is set as follows:

    import time

    def application(environ, start_response):
        headers = [('Content-type', 'application/json')]
        start_response('200 OK', headers)
    return bytes(json.dumps({'time': time.time()}), 'utf8')

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    from greenlet import greenlet
    def test1(x, y): 
z = gr2.switch(x+y)
        print(z)

Any command-line input or output is written as follows:

docker-compose up

New terms and important words are shown in bold.

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-Microservices-Development. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.