Playing with microservices, Docker, Python an Nameko

In the last projects that I’ve been involved with I’ve playing, in one way or another, with microservices, queues and things like that. I’m always facing the same tasks: Building RPCs, Workers, API gateways, … Because of that I’ve searching one framework to help me with those kind of stuff. Finally I discover Nameko. Basically Nameko is the Python tool that I’ve been looking for. In this post I will create a simple proof of concept to learn how to integrate Nameko within my projects. Let start.

The POC is a simple API gateway that gives me the localtime in iso format. I can create a simple Python script to do it

import datetime
import time


We also can create a simple Flask API server to consume this information. The idea is create a rpc worker to generate this information and also generate another worker to send the localtime, but taken from a PostgreSQL database (yes I know it not very useful but it’s just an excuse to use a PG database in the microservice)

We’re going to create two rpc workers. One giving the local time:

from nameko.rpc import rpc
from time import time
import datetime

class TimeService:
    name = "local_time_service"

    def local(self):
        return datetime.datetime.fromtimestamp(time()).isoformat()

And another one with the date from PostgreSQL:

from nameko.rpc import rpc
from dotenv import load_dotenv
import os
from import PgService

current_dir = os.path.dirname(os.path.abspath(__file__))

class TimeService:
    name = "db_time_service"
    conn = PgService(os.getenv('DSN'))

    def db(self):
        with self.conn:
            with self.conn.cursor() as cur:
                cur.execute("select localtimestamp")
                timestamp = cur.fetchone()
        return timestamp[0]

I’ve created a service called PgService only to learn how to create dependency providers in nameko

from nameko.extensions import DependencyProvider
import psycopg2

class PgService(DependencyProvider):

    def __init__(self, dsn):
        self.dsn = dsn

    def get_dependency(self, worker_ctx):
        return psycopg2.connect(self.dsn)

Now we only need to setup the api gateway. With Nameko we can create http entrypoint also (in the same way than we create rpc) but I want to use it with Flask

from flask import Flask
from nameko.standalone.rpc import ServiceRpcProxy
from dotenv import load_dotenv
import os

current_dir = os.path.dirname(os.path.abspath(__file__))

app = Flask(__name__)

def rpc_proxy(service):
    config = {'AMQP_URI': os.getenv('AMQP_URI')}
    return ServiceRpcProxy(service, config)

def hello():
    return "Hello"

def local_time():
    with rpc_proxy('local_time_service') as rpc:
        time = rpc.local()

    return time

def db_time():
    with rpc_proxy('db_time_service') as rpc:
        time = rpc.db()

    return time

if __name__ == '__main__':

As well as I wanna run my POC with docker, here the docker-compose file to set up the project

version: '3.4'

    image: nameko/api
    container_name: nameko.api
    hostname: api
    - "8080:8080"
    restart: always
    - rabbit
    - db.worker
    - local.worker
    - ENV=1
      context: ./api
      dockerfile: .docker/Dockerfile-api
    #- ./api:/usr/src/app:ro
    command: flask run --host= --port 8080
    container_name: nameko.db.worker
    image: nameko/db.worker
    restart: always
      context: ./workers/db.worker
      dockerfile: .docker/Dockerfile-worker
    command: /bin/bash
    container_name:  nameko.local.worker
    image: nameko/local.worker
    restart: always
      context: ./workers/local.worker
      dockerfile: .docker/Dockerfile-worker
    command: /bin/bash
    container_name: nameko.rabbit
    image: rabbitmq:3-management
    restart: always
    - "15672:15672"
    - "5672:5672"
    image: nameko/pg
    restart: always
      context: ./pg
      dockerfile: .docker/Dockerfile-pg
    #- "5432:5432"
      PGDATA: /var/lib/postgresql/data/pgdata

And that’s all. Two nameko rpc services working together behind a api gateway

Code available in my github

2 thoughts on “Playing with microservices, Docker, Python an Nameko

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.