How we realized SOA by Python at PyCon JP 2015

89
How we realize SOA by Python In PyCon JP 2015

Transcript of How we realized SOA by Python at PyCon JP 2015

Page 1: How we realized SOA by Python at PyCon JP 2015

How we realize SOA by PythonIn PyCon JP 2015

Page 2: How we realized SOA by Python at PyCon JP 2015

#PyConJP_C

Page 3: How we realized SOA by Python at PyCon JP 2015

SOA meaning Service Oriented Architecture

Page 4: How we realized SOA by Python at PyCon JP 2015

What's SOA

SOA is an approach to create a system

based on small servers separated for small

functions.

Page 5: How we realized SOA by Python at PyCon JP 2015

SOA

WebAppSearch

Auth

Other

Images

Page 6: How we realized SOA by Python at PyCon JP 2015

https://www.nginx.com/blog/building-microservices-using-an-api-gateway/

Page 7: How we realized SOA by Python at PyCon JP 2015

So... is the SOA comfy?

• Yes.

• But... no silver bullet.

Page 8: How we realized SOA by Python at PyCon JP 2015

Pros

• Separated responsibilities

• Rapid integration

• Flexible scaling

Page 9: How we realized SOA by Python at PyCon JP 2015

Cons

• Lots of servers

●To create

●To deploy

●To monitor

Page 10: How we realized SOA by Python at PyCon JP 2015

Can't realize SOA with legacy style

Page 11: How we realized SOA by Python at PyCon JP 2015

• Direct transmitting

• Manual deploying

• Manual testing

Page 12: How we realized SOA by Python at PyCon JP 2015

So how we realize SOA

Page 13: How we realized SOA by Python at PyCon JP 2015

Today you can learn

• Case study of SOA by Python

• Practices to manage lots of services

Page 14: How we realized SOA by Python at PyCon JP 2015

Today you can't lean

• Step by step guide

• So much about Python's core

Page 15: How we realized SOA by Python at PyCon JP 2015

Agenda

About Our case Summary

Architecture Creating MonitoringTesting Deploying

Page 16: How we realized SOA by Python at PyCon JP 2015

Who I am

• Hiroki Kiyohara (hirokiky)

• BePROUD Inc

• Developer, Consultant, Trainer

• Admin of djangoproject.jp

Page 17: How we realized SOA by Python at PyCon JP 2015

Who we are

• News paper company

●Over 400,000 paid user (Web)

• Migrating to in-house production

Page 18: How we realized SOA by Python at PyCon JP 2015

Our case

Page 19: How we realized SOA by Python at PyCon JP 2015

Architecture

Page 20: How we realized SOA by Python at PyCon JP 2015

Architecture

Page 21: How we realized SOA by Python at PyCon JP 2015

Architecture

REST API

Page 22: How we realized SOA by Python at PyCon JP 2015

Architecture Separated from Web

Page 23: How we realized SOA by Python at PyCon JP 2015

API Gateway

• Proxy for back-end servers

• Handling permissions

Page 24: How we realized SOA by Python at PyCon JP 2015

Creating

Page 25: How we realized SOA by Python at PyCon JP 2015

Creating

• Need to create many services

• Share docs

Page 26: How we realized SOA by Python at PyCon JP 2015

Django

• Easy to create

• Many libraries

• Community and knowledge

Page 27: How we realized SOA by Python at PyCon JP 2015

DjangoRestFramework

• Framework on Django to create REST API

• Useful modules

• Smart Auth/Authz framework

Page 28: How we realized SOA by Python at PyCon JP 2015

DRF: Serializer

• Django's Form for REST API

●Nested

●Array of objects

Page 29: How we realized SOA by Python at PyCon JP 2015

Serializer is really simple

Page 30: How we realized SOA by Python at PyCon JP 2015

Validating by serializer>>> from search.serializers import BulkPostSerializer>>> BulkPostSerializer(data={... "name": "hiroki",... "posts": [... {"title": "PyCon JP 2015",... "body": "This is awesome event"},... {"title": "How we realize SOA",... "body": "Python is awesome"},... ]... })>>> serializer.is_valid()True>>> serializer.validated_dataOrderedDict([('name', 'hiroki'), ('pos...

Page 31: How we realized SOA by Python at PyCon JP 2015

cookiecutter

• Template of repository

• Share best practices

Page 32: How we realized SOA by Python at PyCon JP 2015

. ├── README.md ├── project_name

│ ├── manage.py │ └── project_name │ ├── settings │ │ ├── __init__.py │ │ └── test.py │ ├── urls.py │ └── wsgi.py

├── .elasticbeanstalk/ ├── .coveragerc ├── .gitignore ├── circle.yml ├── Dockerfile ├── Dockerrun.aws.json ├── requirements.txt ├── setup.cfg └── tox.ini

Page 33: How we realized SOA by Python at PyCon JP 2015

OK so... created

• Many applications

• Share docs

Page 34: How we realized SOA by Python at PyCon JP 2015

django-rest-swagger

• API docs from docstring and code

• Demo-able API docs

Page 35: How we realized SOA by Python at PyCon JP 2015

django-rest-swagger

Page 36: How we realized SOA by Python at PyCon JP 2015

django-rest-swagger

Page 37: How we realized SOA by Python at PyCon JP 2015

Creating

• Django

• DjangoRestFramework

• cookiecutter

• tox

Page 38: How we realized SOA by Python at PyCon JP 2015

Testing

Page 39: How we realized SOA by Python at PyCon JP 2015

Testing

• Handy unit tests

• E2E tests

Page 40: How we realized SOA by Python at PyCon JP 2015

I know you won't run. MEE TOO

• Complex

• Slow

• Useless

Page 41: How we realized SOA by Python at PyCon JP 2015

Speed up by django's setting

• On memory sqlite

• Dummy or local cache

• Light hasher

Refer Two scoops of Django

Page 42: How we realized SOA by Python at PyCon JP 2015

Use nice tools

• tox

• flake8

• ...

Page 43: How we realized SOA by Python at PyCon JP 2015

tox

• Just type `tox`

• Run several tests in separated virtualenv

Page 44: How we realized SOA by Python at PyCon JP 2015

[tox]envlist = py34, flake8skipsdist = Truesetupdir = ./myprj/[testenv:py34]deps = coverage -rrequirements.txtsetenv = DJANGO_SETTINGS_MODULE = myprj.settings.testcommands = coverage erase coverage run myprj/manage.py test myprj coverage report[testenv:flake8]basepython = python3.4deps = flake8commands = flake8 myprj

Page 45: How we realized SOA by Python at PyCon JP 2015

[tox]envlist = py34, flake8skipsdist = Truesetupdir = ./myprj/[testenv:py34]deps = coverage -rrequirements.txtsetenv = DJANGO_SETTINGS_MODULE = myprj.settings.testcommands = coverage erase coverage run myprj/manage.py test myprj coverage report[testenv:flake8]basepython = python3.4deps = flake8commands = flake8 myprj

Installing dependencies

Page 46: How we realized SOA by Python at PyCon JP 2015

[tox]envlist = py34, flake8skipsdist = Truesetupdir = ./myprj/[testenv:py34]deps = coverage -rrequirements.txtsetenv = DJANGO_SETTINGS_MODULE = myprj.settings.testcommands = coverage erase coverage run myprj/manage.py test myprj coverage report[testenv:flake8]basepython = python3.4deps = flake8commands = flake8 myprj

Using settings for test

Page 47: How we realized SOA by Python at PyCon JP 2015

[tox]envlist = py34, flake8skipsdist = Truesetupdir = ./myprj/[testenv:py34]deps = coverage -rrequirements.txtsetenv = DJANGO_SETTINGS_MODULE = myprj.settings.testcommands = coverage erase coverage run myprj/manage.py test myprj coverage report[testenv:flake8]basepython = python3.4deps = flake8commands = flake8 myprj

Running tests with coverage

Page 48: How we realized SOA by Python at PyCon JP 2015

[tox]envlist = py34, flake8skipsdist = Truesetupdir = ./myprj/[testenv:py34]deps = coverage -rrequirements.txtsetenv = DJANGO_SETTINGS_MODULE = myprj.settings.testcommands = coverage erase coverage run myprj/manage.py test myprj coverage report[testenv:flake8]basepython = python3.4deps = flake8commands = flake8 myprj

Static analysis

Page 49: How we realized SOA by Python at PyCon JP 2015

Just...

$ tox

Page 50: How we realized SOA by Python at PyCon JP 2015

flake8

• Code style check

• Static analysis

Page 51: How we realized SOA by Python at PyCon JP 2015

flake8

• Don't review about syntax

• Remove tiny and messy code bugs

Page 52: How we realized SOA by Python at PyCon JP 2015

And more

• responses

• testfixtures

• factory-boy

Page 53: How we realized SOA by Python at PyCon JP 2015

Of cause, DO NOT

• Accessing outer services

• Setting middle wares locally

Page 54: How we realized SOA by Python at PyCon JP 2015

E2E testing

• Using requests

• Checking connection of each services

Page 55: How we realized SOA by Python at PyCon JP 2015

Locust.io

• Load tests by Python

• Distributed clients but aggregated report

Page 56: How we realized SOA by Python at PyCon JP 2015

Test

• Faster Django setting

• tox

• flake8

• locust.io

Page 57: How we realized SOA by Python at PyCon JP 2015

Deploying

Page 58: How we realized SOA by Python at PyCon JP 2015

Reduce costs to deploy

• Automated deploy by ElasticBeanstalk

• Master deploying from CI

Page 59: How we realized SOA by Python at PyCon JP 2015

Auto deploy

• Master branch is on development env

• Deploying from CI tool

Page 60: How we realized SOA by Python at PyCon JP 2015

Deploying

Page 61: How we realized SOA by Python at PyCon JP 2015

Deploying

Deploy It

Page 62: How we realized SOA by Python at PyCon JP 2015

Deploying

GREEN

Page 63: How we realized SOA by Python at PyCon JP 2015

Deploying

Push

Page 64: How we realized SOA by Python at PyCon JP 2015

Deploying

Deploy

Page 65: How we realized SOA by Python at PyCon JP 2015

Deploying

OK Pulling

Page 66: How we realized SOA by Python at PyCon JP 2015

Deploying

Deployed

Page 67: How we realized SOA by Python at PyCon JP 2015

Also from CLI or Web console

• Just type `$ eb deploy`

• Or through the Web

Page 68: How we realized SOA by Python at PyCon JP 2015

Deploy

• GitHub

• CircleCI

• Docker

• ElasticBeanstalk

Page 69: How we realized SOA by Python at PyCon JP 2015

Monitoring

Page 70: How we realized SOA by Python at PyCon JP 2015

A lots of servers to monitor

• Many services, lots servers

• Immutable infrastructure

Page 71: How we realized SOA by Python at PyCon JP 2015

Monitoring tools

Page 72: How we realized SOA by Python at PyCon JP 2015

Sentry

• Event (log) aggregation platform

• Manage console of each events

Page 73: How we realized SOA by Python at PyCon JP 2015
Page 74: How we realized SOA by Python at PyCon JP 2015
Page 75: How we realized SOA by Python at PyCon JP 2015

Sentry

• Many events but one notification

• Marked as `Resolved`, it'll come again

Page 76: How we realized SOA by Python at PyCon JP 2015

Fluentd

• Aggregating access log

• S3 and BigQuery

Page 77: How we realized SOA by Python at PyCon JP 2015

NewRelic

• Resource monitoring for containers

• And APM is awesome

Page 78: How we realized SOA by Python at PyCon JP 2015

NewRelic APM

• Analyzing Python application

• Which progress is slow?

Page 79: How we realized SOA by Python at PyCon JP 2015
Page 80: How we realized SOA by Python at PyCon JP 2015

Rundeck

• Job scheduler

• Better crond

Page 81: How we realized SOA by Python at PyCon JP 2015

Rundeck

Page 82: How we realized SOA by Python at PyCon JP 2015

Monitor

• Sentry

• Fluentd

• NewRelic

• Rundeck

Page 83: How we realized SOA by Python at PyCon JP 2015

So...

Page 84: How we realized SOA by Python at PyCon JP 2015

Will SOA be a silver bullet?

Page 85: How we realized SOA by Python at PyCon JP 2015

No

Page 86: How we realized SOA by Python at PyCon JP 2015

Pros

• Separated responsibilities

• Rapid integration

• Flexible scaling

Page 87: How we realized SOA by Python at PyCon JP 2015

But you need to do

• Easy creating

• Handy testing

• Auto deploying

• Relief monitoring

Page 88: How we realized SOA by Python at PyCon JP 2015

What should we do next?

• Non-blocking

• Nicer container platform

Page 89: How we realized SOA by Python at PyCon JP 2015

Thank you

• For listening

• For this nice event