forked from instana/robot-shop
-
Notifications
You must be signed in to change notification settings - Fork 1
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
10 changed files
with
169 additions
and
8 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
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
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
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,14 @@ | ||
version: '3' | ||
services: | ||
rabbitmq: | ||
image: rabbitmq:3.7-alpine | ||
ports: | ||
- "5672" | ||
payment: | ||
build: | ||
context: . | ||
image: steveww/rs-payment | ||
depends_on: | ||
- rabbitmq | ||
ports: | ||
- "8080:8080" |
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
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,48 @@ | ||
import json | ||
import pika | ||
|
||
class Publisher: | ||
HOST = 'rabbitmq' | ||
VIRTUAL_HOST = '/' | ||
EXCHANGE='robot-shop' | ||
TYPE='direct' | ||
ROUTING_KEY = 'orders' | ||
|
||
def __init__(self, logger): | ||
self._logger = logger | ||
self._params = pika.connection.ConnectionParameters( | ||
host=self.HOST, | ||
virtual_host=self.VIRTUAL_HOST, | ||
credentials=pika.credentials.PlainCredentials('guest', 'guest')) | ||
self._conn = None | ||
self._channel = None | ||
|
||
def _connect(self): | ||
if not self._conn or self._conn.is_closed: | ||
self._conn = pika.BlockingConnection(self._params) | ||
self._channel = self._conn.channel() | ||
self._channel.exchange_declare(exchange=self.EXCHANGE, exchange_type=self.TYPE) | ||
self._logger.info('connected to broker') | ||
|
||
def _publish(self, msg): | ||
self._channel.basic_publish(exchange=self.EXCHANGE, | ||
routing_key=self.ROUTING_KEY, | ||
body=json.dumps(msg).encode()) | ||
self._logger.info('message sent') | ||
|
||
#Publish msg, reconnecting if necessary. | ||
def publish(self, msg): | ||
if self._channel is None: | ||
self._connect() | ||
try: | ||
self._publish(msg) | ||
except pika.exceptions.ConnectionClosed: | ||
self._logger.info('reconnecting to queue') | ||
self._connect() | ||
self._publish(msg) | ||
|
||
def close(self): | ||
if self._conn and self._conn.is_open: | ||
self._logger.info('closing queue connection') | ||
self._conn.close() | ||
|
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Flask | ||
requests | ||
pika | ||
instana |
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
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
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