How we built microservices
Ihor Harahatyi
Disclaimer
This is a story about how we built project
Yes, it can be done better
Yes, we broke some rules
We google it
12 factor apps
(https://12factor.net/)
Codebase, Dependencies, Con g, Backing services,
Build - release - run, Processes, Port binding,
Concurrency, Disposability, Dev/prod parity, Logs,
Admin processes
Monolith <--> Microservices
Why we use them?
Separate development of each one
Scale them separately
Fault tolerancy
Use technology we want for each one
Small services with clean code
Rancher
(https://rancher.com/)
Rancher-compose
#rancher-compose.yml
version: '2'
services:
mysql:
scale: 1
uwsgi:
scale: 2
health_check:
port: 9000
interval: 2000
unhealthy_threshold: 3
request_line: GET / HTTP/1.0
healthy_threshold: 2
response_timeout: 2000
upgrade_strategy:
start_first: true
JWT
(https://tools.ietf.org/html/rfc7519, https://jwt.io)
Smaller then json
Custom data (serialize user model)
Signature
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3O
DkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7
E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
Local environment
:8000 - frontend
:8001 - auth
:8002 - ms1
:8003 - ms3
...
New local env
add api.local.com -> 127.0.0.1 to /etc/hosts
map api.local.com/v1/ms/ to right docker
container
????
PROFIT!
Kong
(https://getkong.org/)
Docker tags
(https://github.com/ogir-ok/kong-companion)
#docker-compose.yml
microservice:
build: .
volumes:
- .:/src/
labels:
kong.uris: /v1/ms/
kong.port: '8080'
Kong versioning plugin
I want to work on issue #123
I add tag v123 to all my call
My call is mapped to right backend service
Inter ms communtication
CherryPy
(http://cherrypy.org/)
class Blog(object):
def item(self, id):
return "Post {}".format(id)
def index(self, id):
return "Blog"
class Root(object):
def index(self):
return "hello world!"
blog = Blog
Inter MS client
# client.auth.register.post(email='foo@bar.com')
requests.post(
'api.local/v1/auth/register',
json={'email': 'foo@bar.com'},
headers={pass headers here}
)
Rest is not silver bullet
RPC
Queue
Soap
???
Structlog
(https://structlog.readthedocs.io/en/stable/)
logger.bind(tracking_code=UUID4)
logger.bind(microservice_name=ms_name)
...
User's data
No DB relations
No User model
request.user = jwt.decode()
Common code?
I'll fork boilerplate
Submodule?
Pypi
Build it in jenkins
Opensource it one day
At least I can unfreeze version
Complex update
I updated common library and prod is down
We changed account_id, update all servers
It's not my job
Each team says that issue is on other side
Bulk operations
Yes we have docs
When to use?
You have free time
You need many programming languages
You have parts that are good to reuse
You have many teams
Q & A

How we buit microservices