Python Microservices Development

Book description

A practical approach to conquering the complexities of Microservices using the Python tooling ecosystem

Key Features

  • A very useful guide for Python developers who are shifting to the new microservices-based development
  • A concise, up-to-date guide to building efficient and lightweight microservices in Python using Flask, Tox, and other tools
  • Learn to use Docker containers, CoreOS, and Amazon Web Services to deploy your services

Book Description

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.

What you will learn

  • Explore what microservices are and how to design them
  • Use Python 3, Flask, Tox, and other tools to build your services using best practices
  • Learn how to use a TDD approach
  • Discover how to document your microservices
  • Configure and package your code in the best way
  • Interact with other services
  • Secure, monitor, and scale your services
  • Deploy your services in Docker containers, CoreOS, and Amazon Web Services

Who this book is for

This book is for developers who have basic knowledge of Python, the command line, and HTTP-based application principles, and those who want to learn how to build, test, scale, and manage Python 3 microservices. No prior experience of writing microservices in Python is assumed.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Understanding Microservices
    1. Origins of Service-Oriented Architecture
    2. The monolithic approach
    3. The microservice approach
    4. Microservice benefits
      1. Separation of concerns
      2. Smaller projects
      3. Scaling and deployment
    5. Microservices pitfalls
      1. Illogical splitting
      2. More network interactions
      3. Data storing and sharing
      4. Compatibility issues
      5. Testing
    6. Implementing microservices with Python
      1. The WSGI standard
      2. Greenlet and Gevent
      3. Twisted and Tornado
      4. asyncio
      5. Language performances
    7. Summary
  3. Discovering Flask
    1. Which Python?
    2. How Flask handles requests
      1. Routing
        1. Variables and converters
        2. The url_for function
      2. Request
      3. Response
    3. Flask built-in features
      1. The session object
      2. Globals
      3. Signals
      4. Extensions and middlewares
      5. Templates
      6. Configuration
      7. Blueprints
      8. Error handling and debugging
        1. Custom error handler
        2. The debug mode
    4. A microservice skeleton
    5. Summary
  4. Coding, Testing, and Documenting - the Virtuous Cycle
    1. Different kinds of tests
      1. Unit tests
      2. Functional tests
      3. Integration tests
      4. Load tests
      5. End-to-end tests
    2. Using WebTest
    3. Using pytest and Tox
    4. Developer documentation
    5. Continuous Integration
      1. Travis-CI
      2. ReadTheDocs
      3. Coveralls
    6. Summary
  5. Designing Runnerly
    1. The Runnerly application
      1. User stories
    2. Monolithic design
      1. Model
      2. View and Template
      3. Background tasks
        1. Strava token
      4. Authentication and authorization
      5. Putting together the monolithic design
    3. Splitting the monolith
    4. Data Service
    5. Using Open API 2.0
    6. More splitting
    7. Summary
  6. Interacting with Other Services
    1. Synchronous calls
      1. Using Session in a Flask app
      2. Connection pooling
      3. HTTP cache headers
      4. Improving data transfer
        1. GZIP compression
        2. Binary payloads
      5. Putting it together
    2. Asynchronous calls
      1. Task queues
      2. Topic queues
      3. Publish/subscribe
      4. RPC over AMQP
      5. Putting it together
    3. Testing
      1. Mocking synchronous calls
      2. Mocking asynchronous calls
        1. Mocking Celery
        2. Mocking other asynchronous calls
    4. Summary
  7. Monitoring Your Services
    1. Centralizing logs
      1. Setting up Graylog
      2. Sending logs to Graylog
      3. Adding extra fields
    2. Performance metrics
      1. System metrics
      2. Code metrics
      3. Web server metrics
    3. Summary
  8. Securing Your Services
    1. The OAuth2 protocol
    2. Token-based authentication
      1. The JWT standard
      2. PyJWT
      3. X.509 certificate-based authentication
      4. The TokenDealer microservice
        1. The POST/oauth/token implementation
      5. Using TokenDealer
    3. Web application firewall
      1. OpenResty - Lua and nginx
        1. Rate and concurrency limiting
        2. Other OpenResty features
    4. Securing your code
      1. Asserting incoming data
      2. Limiting your application scope
      3. Using Bandit linter
  9. Bringing It All Together
    1. Building a ReactJS dashboard
      1. The JSX syntax
      2. React components
    2. ReactJS and Flask
      1. Using Bower, npm, and Babel
      2. Cross-origin resource sharing
    3. Authentication and authorization
      1. Interacting with Data Service
      2. Getting the Strava token
      3. JavaScript authentication
  10. Packaging and Running Runnerly
    1. The packaging toolchain
      1. A few definitions
      2. Packaging
        1. The setup.py file
        2. The requirements.txt file
        3. The MANIFEST.in file
      3. Versioning
      4. Releasing
      5. Distributing
    2. Running all microservices
    3. Process management
    4. Summary
  11. Containerized Services
    1. What is Docker?
    2. Docker 101
    3. Running Flask in Docker
    4. The full stack - OpenResty, Circus and Flask
      1. OpenResty
      2. Circus
    5. Docker-based deployments
      1. Docker Compose
      2. Introduction to Clustering and Provisioning
    6. Summary
  12. Deploying on AWS
    1. AWS overview
    2. Routing - Route53, ELB, and AutoScaling
    3. Execution - EC2 and Lambda
    4. Storage - EBS, S3, RDS, ElasticCache, and CloudFront
      1. Messaging - SES, SQS, and SNS
        1. Simple Email Service (SES)
        2. Simple Queue Service (SQS)
        3. Simple Notification Service (SNS)
      2. Provisioning and deployment - CloudFormation and ECS
    5. Deploying on AWS - the basics
      1. Setting up your AWS account
      2. Deploying on EC2 with CoreOS
    6. Deploying with ECS
    7. Route53
    8. Summary
  13. What Next?
    1. Iterators and generators
    2. Coroutines
    3. The asyncio library
    4. The aiohttp framework
    5. Sanic
    6. Asynchronous versus synchronous

Product information

  • Title: Python Microservices Development
  • Author(s): Tarek Ziadé
  • Release date: July 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781785881114