Blog Archives

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


Microservice container with Guzzle

This days I’m reading about Microservices. The idea is great. Instead of building a monolithic script using one language/framowork. We create isolated services and we build our application using those services (speaking HTTP between services and application).

That’s means we’ll have several microservices and we need to use them, and maybe sometimes change one service with another one. In this post I want to build one small container to handle those microservices. Similar idea than Dependency Injection Containers.

As we’re going to speak HTTP, we need a HTTP client. We can build one using curl, but in PHP world we have Guzzle, a great HTTP client library. In fact Guzzle has something similar than the idea of this post: Guzzle services, but I want something more siple.

Imagine we have different services:
One Silex service (PHP + Silex)

use Silex\Application;

$app = new Application();

$app->get('/hello/{username}', function($username) {
    return "Hello {$username} from silex service";


Another PHP service. This one using Slim framework

use Slim\Slim;

$app = new Slim();

$app->get('/hello/:username', function ($username) {
    echo "Hello {$username} from slim service";


And finally one Python service using Flask framework

from flask import Flask, jsonify
app = Flask(__name__)

def show_user_profile(username):
    return "Hello %s from flask service" % username

if __name__ == "__main__":, host='', port=5000)

Now, with our simple container we can use one service or another

use Symfony\Component\Config\FileLocator;
use MSIC\Loader\YamlFileLoader;
use MSIC\Container;

$container = new Container();

$ymlLoader = new YamlFileLoader($container, new FileLocator(__DIR__));

echo $container->getService('flaskServer')->get('/hello/Gonzalo')->getBody() . "\n";
echo $container->getService('silexServer')->get('/hello/Gonzalo')->getBody() . "\n";
echo $container->getService('slimServer')->get('/hello/Gonzalo')->getBody() . "\n";

And that’s all. You can see the project in my github account.