1. 程式人生 > >【RabbitMQ】連線RabbitMQ異常: com.rabbitmq.client.ShutdownSignalException: connection error; protocol meth

【RabbitMQ】連線RabbitMQ異常: com.rabbitmq.client.ShutdownSignalException: connection error; protocol meth

測試該工具類:

package com.wj.utils;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @建立人 wj
 * @建立時間 2018/11/5
 * @描述
 */
public class ConnectionUtil {
    private static final String host = "localhost";
    private static final int port = 5672;
    private static final String username="wj";
    private static final String password="pwd";


    /**
     * 獲取RabbitMQ Connection連線
     * @return
     * @throws IOException
     * @throws TimeoutException
     */
    public static Connection getConnection() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        return connectionFactory.newConnection();
    }
}

異常:

Exception in thread "main" java.io.IOException
	at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)
	at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)
	at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)
	at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:378)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:678)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:722)
	at upsmart.zhsen.ServerProducer.main(ServerProducer.java:33

Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; protocol method: #method<connection.close>(reply-code=530, reply-text=NOT_ALLOWED - access to vhost '/' refused for user 'wj', class-id=10, method-id=40)

解決方法:

輸入:rabbitmqctl set_permissions -p "/" username ".*" ".*" ".*"