ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setPort(5672);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setHost("localhost");
connectionFactory.setVirtualHost("/");
var conn = connectionFactory.newConnection();
var channel = conn.createChannel();
conn.close();
这段代码会在channel建立的时候报Connection reset,用多种编程语言试过,请问可能有些什么原因,以下是RabbitMQ的日志
2023-06-29 18:53:02.258000+08:00 [info] <0.1204.0> accepting AMQP connection <0.1204.0> (127.0.0.1:2202 -> 127.0.0.1:5672)
2023-06-29 18:53:02.285000+08:00 [info] <0.1204.0> connection <0.1204.0> (127.0.0.1:2202 -> 127.0.0.1:5672): user 'guest' authenticated and granted access to vhost '/'
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> crasher:
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> initial call: rabbit_reader:init/3
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> pid: <0.1204.0>
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> registered_name: []
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> exception exit: {unexpected_message,{'EXIT',#Port<0.7257>,einval}}
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> in function rabbit_reader:handle_other/2 (rabbit_reader.erl, line 644)
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> in call from rabbit_reader:mainloop/4 (rabbit_reader.erl, line 535)
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> in call from rabbit_reader:run/1 (rabbit_reader.erl, line 457)
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> in call from rabbit_reader:start_connection/5 (rabbit_reader.erl, line 356)
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> ancestors: [<0.1202.0>,<0.599.0>,<0.598.0>,<0.597.0>,<0.595.0>,
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> <0.594.0>,rabbit_sup,<0.233.0>]
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> message_queue_len: 0
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> messages: []
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> links: [<0.1202.0>]
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> dictionary: [{{ch_pid,<0.1217.0>},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {1,#Ref<0.3908685128.610795521.253910>}},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {process_name,
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {rabbit_reader,<<"127.0.0.1:2202 -> 127.0.0.1:5672">>}},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {client_properties,
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> [{<<"product">>,longstr,<<"RabbitMQ">>},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"version">>,longstr,
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> <<"6.5.0+4c91cae8ae5eb0194e02a83f1b0cedfe29ad8312">>},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"platform">>,longstr,<<".NET">>},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"copyright">>,longstr,
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> <<"Copyright (c) 2007-2020 VMware, Inc.">>},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"information">>,longstr,
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> <<"Licensed under the MPL. See https://www.rabbitmq.com/%22%3E%3E},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"capabilities">>,table,
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> [{<<"publisher_confirms">>,bool,true},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"exchange_exchange_bindings">>,bool,true},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"basic.nack">>,bool,true},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"consumer_cancel_notify">>,bool,true},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"connection.blocked">>,bool,true},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"authentication_failure_close">>,bool,true}]},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<<"connection_name">>,void,undefined}]},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {{channel,1},
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> {<0.1217.0>,{method,rabbit_framing_amqp_0_9_1}}}]
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> trap_exit: true
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> status: running
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> heap_size: 1598
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> stack_size: 28
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> reductions: 9979
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0> neighbours:
2023-06-29 18:53:05.482000+08:00 [error] <0.1204.0>
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> supervisor: {<0.1202.0>,rabbit_connection_sup}
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> errorContext: child_terminated
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> reason: {unexpected_message,{'EXIT',#Port<0.7257>,einval}}
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> offender: [{pid,<0.1204.0>},
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> {id,reader},
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> {mfargs,{rabbit_reader,start_link,
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> [<0.1203.0>,
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> {acceptor,{127,0,0,1},5672}]}},
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> {restart_type,intrinsic},
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> {shutdown,300000},
2023-06-29 18:53:05.494000+08:00 [error] <0.1202.0> {child_type,worker}]
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> supervisor: {<0.1202.0>,rabbit_connection_sup}
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> errorContext: shutdown
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> reason: reached_max_restart_intensity
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> offender: [{pid,<0.1204.0>},
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> {id,reader},
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> {mfargs,{rabbit_reader,start_link,
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> [<0.1203.0>,
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> {acceptor,{127,0,0,1},5672}]}},
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> {restart_type,intrinsic},
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> {shutdown,300000},
2023-06-29 18:53:05.495000+08:00 [error] <0.1202.0> {child_type,worker}]
网络问题:检查网络连接是否正常,并确保能够连接到 RabbitMQ 服务器。您可以尝试通过 ping 命令或 telnet 命令来测试与服务器的连接。
防火墙配置:确保防火墙允许与 RabbitMQ 服务器的通信。如果您的服务器上有防火墙,您需要确保 RabbitMQ 的端口(默认情况下为5672)是开放的。
证书配置:如果您使用了 SSL/TLS 连接,可能会涉及到证书配置。确保您的证书配置正确,并且证书没有过期或被撤销。
RabbitMQ 服务器配置:检查 RabbitMQ 服务器的配置文件,确保它正确配置并且没有错误。特别注意检查监听地址、端口和身份验证等配置项。
并发连接限制:RabbitMQ 默认有一些并发连接限制。如果您的应用程序同时创建了大量的连接和通道,可能会达到服务器的限制。您可以尝试减少并发连接数量,或者调整 RabbitMQ 服务器的配置来提高并发连接的限制。
RabbitMQ创建Channel时报Connection reset错误通常是由于连接被重置或中断导致的。这可能是由于网络问题、RabbitMQ服务器故障或客户端代码问题引起的。
要解决这个问题,可以尝试以下几个步骤:
检查网络连接是否正常。如果你的网络连接不稳定或中断,就可能导致连接被重置。你可以尝试重新连接网络或使用其他网络连接方式。
检查RabbitMQ服务器是否正常运行。如果RabbitMQ服务器出现故障或停止运行,就可能导致连接被重置。你可以尝试重新启动RabbitMQ服务器或联系管理员以解决问题。
检查客户端代码是否正确。如果你的客户端代码存在问题,就可能导致连接被重置。你可以检查代码中是否存在错误,并尝试修复它们。
调整RabbitMQ的连接参数。你可以尝试调整RabbitMQ的连接参数,例如心跳间隔、连接超时等,以优化连接性能并减少连接被重置的可能性。
如果以上方法都无法解决问题,你可以尝试使用RabbitMQ的其他API或库来创建Channel,或者联系RabbitMQ的支持团队以获取更多帮助。
ConnectionFactory、Connection、Channel都是RabbitMQ对外提供的API中最基本的对象。
Connection是RabbitMQ的socket链接,它封装了socket协议相关部分逻辑。
ConnectionFactory如名称,是客户端与broker的tcp连接工厂,负责根据uri创建Connection。
Channel是我们与RabbitMQ打交道的最重要的一个接口,我们大部分的业务操作是在Channel这个接口中完成的,包括定义Queue、定义Exchange、绑定Queue与Exchange、发布消息等。如果每一次访问RabbitMQ都建立一个Connection,在消息量大的时候建立TCP Connection的开销将是巨大的,效率也较低。Channel是在connection内部建立的逻辑连接,如果应用程序支持多线程,通常每个thread创建单独的channel进行通讯,AMQP method包含了channel id帮助客户端和message broker识别channel,所以channel之间是完全隔离的。Channel作为轻量级的Connection极大减少了操作系统建立TCP connection的开销
注:channel 与 connection区别
A Connection represents a real TCP connection to the message broker, whereas aChannelis a virtual connection (AMPQ connection) inside it. This way you can use as many (virtual) connections as you want inside your application without overloading the broker with TCP connections.
You can use one Channel for everything. However, if you have multiple threads, it's suggested to use a different Channel for each thread.
There is no direct relation betweenChannelandQueue. AChannelis used to send AMQP commands to the broker. This can be the creation of a queue or similar, but these concepts are not tied together.
Consumerruns in its own thread allocated from the consumer thread pool. If multiple Consumers are subscribed to the same Queue, the broker uses round-robin to distribute the messages between them equally.
It is also possible to attach the sameConsumerto multiple Queues. You can understand Consumers as callbacks. These are called everytime a message arrives on a Queue the Consumer is bound to. For the case of the Java Client, each Consumers has a methodhandleDelivery(...), which represents the callback method. What you typically do is, subclassDefaultConsumerand overridehandleDelivery(...). Note: If you attach the same Consumer instance to multiple queues, this method will be called by different threads. So take care of synchronization if necessary.
部分源码:
问题已解决,是因为RabbitMQ版本太低(3.11.x),不能兼容Erlang 26,升级RabbitMQ后问题解决,感谢大家的作答