Skip to content

Commit

Permalink
Adding microservices.
Browse files Browse the repository at this point in the history
  • Loading branch information
gpkc committed May 27, 2018
1 parent 9ae6e92 commit 82dfa59
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Nameko microservices

Simple microservices example using Nameko.


## Running

`$ docker-compose up`
16 changes: 16 additions & 0 deletions airports/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3

RUN apt-get update && apt-get -y install netcat && apt-get clean

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY config.yml ./
COPY run.sh ./
COPY airports.py ./

RUN chmod +x ./run.sh

CMD ["./run.sh"]
21 changes: 21 additions & 0 deletions airports/airports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import uuid

from nameko.rpc import rpc
from nameko_redis import Redis


class AirportsService:
name = "airports_service"

redis = Redis('development')

@rpc
def get(self, airport_id):
airport = self.redis.get(airport_id)
return airport

@rpc
def create(self, airport):
airport_id = uuid.uuid4().hex
self.redis.set(airport_id, airport)
return airport_id
3 changes: 3 additions & 0 deletions airports/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AMQP_URI: amqp://${RABBIT_USER}:${RABBIT_PASSWORD}@${RABBIT_HOST}:${RABBIT_PORT}/
REDIS_URIS:
development: redis://${REDIS_HOST}:${REDIS_PORT}/0
2 changes: 2 additions & 0 deletions airports/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nameko
nameko-redis
13 changes: 13 additions & 0 deletions airports/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

until nc -z ${RABBIT_HOST} ${RABBIT_PORT}; do
echo "$(date) - waiting for rabbitmq..."
sleep 1
done

until nc -z ${REDIS_HOST} ${REDIS_PORT}; do
echo "$(date) - waiting for redis..."
sleep 1
done

nameko run --config config.yml airports
56 changes: 56 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
version: "2"
services:

redis:
image: redis:4-alpine
command: ["redis-server", "--appendonly", "yes"]
hostname: redis
volumes:
- redis-data:/data

rabbit:
image: rabbitmq:3.6-management
ports:
- "15672:15672"

airports:
build:
context: airports
depends_on:
- rabbit
environment:
REDIS_HOST: "redis"
REDIS_PORT: "6379"
RABBIT_PASSWORD: "guest"
RABBIT_USER: "guest"
RABBIT_HOST: "rabbit"
RABBIT_PORT: "5672"

trips:
build:
context: trips
depends_on:
- rabbit
environment:
REDIS_HOST: "redis"
REDIS_PORT: "6379"
RABBIT_PASSWORD: "guest"
RABBIT_USER: "guest"
RABBIT_HOST: "rabbit"
RABBIT_PORT: "5672"

gateway:
build:
context: gateway
depends_on:
- rabbit
ports:
- "8000:8000"
environment:
RABBIT_PASSWORD: "guest"
RABBIT_USER: "guest"
RABBIT_HOST: "rabbit"
RABBIT_PORT: "5672"

volumes:
redis-data:
16 changes: 16 additions & 0 deletions gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3

RUN apt-get update && apt-get -y install netcat && apt-get clean

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY config.yml ./
COPY run.sh ./
COPY gateway.py ./

RUN chmod +x ./run.sh

CMD ["./run.sh"]
1 change: 1 addition & 0 deletions gateway/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AMQP_URI: amqp://${RABBIT_USER}:${RABBIT_PASSWORD}@${RABBIT_HOST}:${RABBIT_PORT}/
35 changes: 35 additions & 0 deletions gateway/gateway.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import json

from nameko.rpc import RpcProxy
from nameko.web.handlers import http


class GatewayService:
name = 'gateway'

airports_rpc = RpcProxy('airports_service')
trips_rpc = RpcProxy('trips_service')

@http('GET', '/airport/<string:airport_id>')
def get_airport(self, request, airport_id):
airport = self.airports_rpc.get(airport_id)
return json.dumps({'airport': airport})

@http('POST', '/airport')
def post_airport(self, request):
data = json.loads(request.get_data(as_text=True))
airport_id = self.airports_rpc.create(data['airport'])

return airport_id

@http('GET', '/trip/<string:trip_id>')
def get_trip(self, request, trip_id):
trip = self.trips_rpc.get(trip_id)
return json.dumps({'trip': trip})

@http('POST', '/trip')
def post_trip(self, request):
data = json.loads(request.get_data(as_text=True))
trip_id = self.trips_rpc.create(data['airport_from'], data['airport_to'])

return trip_id
1 change: 1 addition & 0 deletions gateway/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nameko
8 changes: 8 additions & 0 deletions gateway/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

until nc -z ${RABBIT_HOST} ${RABBIT_PORT}; do
echo "$(date) - waiting for rabbitmq..."
sleep 1
done

nameko run --config config.yml gateway
16 changes: 16 additions & 0 deletions trips/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3

RUN apt-get update && apt-get -y install netcat && apt-get clean

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY config.yml ./
COPY run.sh ./
COPY trips.py ./

RUN chmod +x ./run.sh

CMD ["./run.sh"]
3 changes: 3 additions & 0 deletions trips/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AMQP_URI: amqp://${RABBIT_USER}:${RABBIT_PASSWORD}@${RABBIT_HOST}:${RABBIT_PORT}/
REDIS_URIS:
development: redis://${REDIS_HOST}:${REDIS_PORT}/1
2 changes: 2 additions & 0 deletions trips/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nameko
nameko-redis
13 changes: 13 additions & 0 deletions trips/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

until nc -z ${RABBIT_HOST} ${RABBIT_PORT}; do
echo "$(date) - waiting for rabbitmq..."
sleep 1
done

until nc -z ${REDIS_HOST} ${REDIS_PORT}; do
echo "$(date) - waiting for redis..."
sleep 1
done

nameko run --config config.yml trips
24 changes: 24 additions & 0 deletions trips/trips.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import uuid

from nameko.rpc import rpc
from nameko_redis import Redis


class AirportsService:
name = "trips_service"

redis = Redis('development')

@rpc
def get(self, trip_id):
trip = self.redis.get(trip_id)
return trip

@rpc
def create(self, airport_from_id, airport_to_id):
trip_id = uuid.uuid4().hex
self.redis.set(trip_id, {
"from": airport_from_id,
"to": airport_to_id
})
return trip_id

0 comments on commit 82dfa59

Please sign in to comment.