在Kubernetes中,我已经部署了RabbitMQ pod,service和ingress。我可以访问RabbitMQ UI,我添加了一个可以访问虚拟主机“/”和密码的用户。
我尝试创建一个连接到RabbitMQ的Python pod并创建一个队列,但出现以下错误:
Traceback (most recent call last):
File "/dump_notify/producer.py", line 40, in <module>
main()
File "/dump_notify/producer.py", line 24, in main
connection = pika.BlockingConnection(parameters)
File "/opt/app-root/lib64/python3.8/site-packages/pika/adapters/blocking_connection.py", line 360, in __init__
self._impl = self._create_connection(parameters, _impl_class)
File "/opt/app-root/lib64/python3.8/site-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError
我的Python代码是这样的:
import pika
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class FileEventHandler(FileSystemEventHandler):
def __init__(self, channel):
self.channel = channel
def on_created(self, event):
if not event.is_directory:
self.channel.basic_publish(exchange='', routing_key='myqueue',
body='New file has been added to SFTP : %s' % event.src_path)
def main():
credentials = pika.PlainCredentials('Lbeodeobf', 'test')
print('Logged')
parameters = pika.ConnectionParameters('X',
5672,
'/',
credentials)
print(parameters)
connection = pika.BlockingConnection(parameters)
print("Connection...")
channel = connection.channel()
channel.queue_declare(queue='myqueue')
event_handler = FileEventHandler(channel)
observer = Observer()
observer.schedule(event_handler, path='/mnt/', recursive=False)
observer.start()
observer.join()
if __name__ == "__main__":
main()
我能错在哪里?
1条答案
按热度按时间jum4pzuy1#
因为你得到的
pika.exceptions.AMQPConnectionError
与你运行的python代码无关,这与RabbitMQ的连接错误有关。因此,您需要在PC或服务器上安装RabbitMQ。安装成功后,如果是linux服务器,然后运行以下命令sudo systemctl start rabbitmq-server
和sudo systemctl enable rabbitmq-server
,这将启动并启用rabbit服务器,然后运行python代码,这可能会解决您的问题。