spring boot链接rabbitMQ问题
spring boot链接rabbitMQ问题
1、远程链接超时org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection timed out: connect
1.在虚拟机上面打开5672和15672端口具体操作如下
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
1.1 安装iptables防火墙
yum install iptables-services
1.2 编辑防火墙配置文件
vi /etc/sysconfig/iptables
编辑内容如下:
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
#-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
#这里是你要暴露的端口start
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5672 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 15672 -j ACCEPT
#这里是你要暴露的端口end
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq! #保存退出
1.3 最后重启防火墙使配置生效
systemctl restart iptables.service
1.4 设置防火墙开机启动
systemctl enable iptables.service
1.5 使用telnet测试端口是否可以访问(如果telnet命令用不了自己百度跟着教程做就可以了)运行成功的效果就是没有效果
2、问题二:Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; protocol method:
- 输入你的机器的IP:15672进入rabbitMQ的登陆管理界面
2.1 点击admin,进去点击set permission
2.2 就OK了
代码测试链接
添加依赖
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
测试代码
private String QUEUE_NAME = "hello";
@Test
public void test1() throws Exception{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.9.167");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("123456");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World from Windows!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
最后注意:远程链接不能用默认的guest账号,需要添加新的管理员,具体教程在我之前的Linux安装rabbitMQ中