Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. version: '3'
  2. services:
  3.  
  4. appone:
  5. container_name: appone
  6. restart: always
  7. build:
  8. context: ./appone
  9. dockerfile: Dockerfile
  10. environment:
  11. FLASK_APP: ./app.py
  12. volumes:
  13. - './appone:/code/:cached'
  14. ports:
  15. - "5001:5001"
  16.  
  17. apptwo:
  18. container_name: apptwo
  19. restart: always
  20. build:
  21. context: ./apptwo
  22. dockerfile: Dockerfile
  23. environment:
  24. FLASK_DEBUG: 1
  25. FLASK_APP: ./app.py
  26. volumes:
  27. - ./apptwo:/code:cached
  28. ports:
  29. - "5002:5002"
  30.  
  31. rabbitmq:
  32. image: "rabbitmq:3-management"
  33. hostname: "rabbit"
  34. ports:
  35. - "15672:15672"
  36. - "5672:5672"
  37. labels:
  38. NAME: "rabbitmq"
  39. volumes:
  40. - ./rabbitmq/rabbitmq-isolated.conf:/etc/rabbitmq/rabbitmq.config
  41.  
  42. from flask import Flask
  43. from flask_restful import Resource, Api
  44. import pika
  45.  
  46. app = Flask(__name__)
  47. api = Api(app)
  48.  
  49. app.config['DEBUG'] = True
  50.  
  51. message = "Hello World, its me appone"
  52.  
  53.  
  54. class HelloWorld(Resource):
  55. def get(self):
  56. connection = pika.BlockingConnection(
  57. pika.ConnectionParameters(host='rabbitmq'))
  58. channel = connection.channel()
  59.  
  60. channel.queue_declare(queue='hello', durable=True)
  61.  
  62. channel.basic_publish(exchange='', routing_key='hello', body='Hello World!', properties=pika.BasicProperties(delivery_mode=2))
  63.  
  64. connection.close()
  65.  
  66. return {'message': message}
  67.  
  68.  
  69. api.add_resource(HelloWorld, '/api/appone/post')
  70.  
  71. if __name__ == '__main__':
  72. # Development
  73. app.run(host="0.0.0.0", port=5001)
  74.  
  75. from flask import Flask
  76. from flask_restful import Resource, Api
  77. import pika
  78. from threading import Thread
  79.  
  80. app = Flask(__name__)
  81. api = Api(app)
  82.  
  83. app.config['DEBUG'] = True
  84.  
  85. data = []
  86.  
  87. connection = pika.BlockingConnection(
  88. pika.ConnectionParameters(host='rabbitmq'))
  89.  
  90. channel = connection.channel()
  91.  
  92. channel.queue_declare(queue='hello', durable=True)
  93.  
  94. def callback(ch, method, properties, body):
  95. data.append(body)
  96. ch.basic_ack(delivery_tag = method.delivery_tag)
  97.  
  98. channel.basic_consume(queue='hello', on_message_callback=callback)
  99.  
  100. thread = Thread(channel.start_consuming())
  101. thread.start()
  102.  
  103. class HelloWorld(Resource):
  104. def get(self):
  105. return {'message': data}
  106.  
  107. api.add_resource(HelloWorld, '/api/apptwo/get')
  108.  
  109. if __name__ == '__main__':
  110. app.run(debug=True, host="0.0.0.0", port=5002)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement