上图显示了正在尝试实现的当前体系结构。目前,我正在远程服务器上运行zookeeper和kakfa(两者均是新手),并且此通知服务应用程序是使用flask构建的,flask充当了消费者(kafka-python),其他应用程序App1.Appn将充当了生产者。 好吧,我能够独立运行可以正常运行的消费者和生产者。 但是我试图让消费者接受通知服务,什么也没发生。
app.py
def send_notification_from_consumer(payload):
# sending notification using firebase
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000, debug=True)
consumer.py
from kafka import KafkaConsumer
import json
from app import send_notification_from_consumer as snc
def notification_consumer():
print("---inside consumer-->")
consumer = KafkaConsumer(
'send-notification',
bootstrap_servers=['remote_server_ip:9092'],
auto_offset_reset='earliest',
enable_auto_commit=True,
group_id='my-group',
value_deserializer=lambda m: json.loads(m.decode("utf-8")))
for message in consumer:
print("----->",message.value)
snc(message.value)
producer.py
from kafka import KafkaProducer
import json
producer = KafkaProducer(bootstrap_servers=['remote_server_ip:9092'],
value_serializer=lambda m: json.dumps(m).encode('utf-8'))
def on_send_success(record_metadata):
print(record_metadata.topic)
print(record_metadata.partition)
print(record_metadata.offset)
def on_send_error(excp):
log.error('I am an errback', exc_info=excp)
for i in range(1):
name = {"phone_number_list": ["1234567890", "1234567891"],
"payload": {"title":"Hello from another ",
"description":"Test notification by notification service",
"banner_image":"", "id":123, "action":{"type":"web", "button_label":"", "link":""},
"app_name":"app_name"}
}
producer.send('send-notification', name).add_callback(on_send_success).add_errback(on_send_error)
是否可以在不使用HTTP请求的情况下调用使用者,因为我只是在kafka使用者中调用send_notification_from_consumer。正确的做法是什么?我应该如何在烧瓶中运行使用者。
我也尝试过此solution 但是我收到了这个错误。这个解决方案可以扩展吗?
RuntimeError: Working outside of request context.