forked from gpkc/nameko-microservices
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
nameko | ||
nameko-redis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
AMQP_URI: amqp://${RABBIT_USER}:${RABBIT_PASSWORD}@${RABBIT_HOST}:${RABBIT_PORT}/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nameko |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
nameko | ||
nameko-redis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |