rabbitmq安装到使用遇到的问题总结

1. 远程访问用户的创建

rqbbitmq默认的guest用户只能通过localhost访问,不管是管理面板15672还是客户端连接端口5672,一次需要创建能够外部访问的账户,网络上好多都是修改配置文件,不太方便操作。下面用命令行简单操作创建出用户。

# 创建用户,密码
sudo rabbitmqctl add_user username userpassed

#列出所有的用户
sudo rabbitmqctl list_users

#赋予其administrator角色(给雷角色标签的作用域可查看官网)
sudo rabbitmqctl set_user_tags user_admin administrator

做完这些之后,就可以通过http://ipAddress:15672登录管理面板了,但是此时还没有虚拟机的权限,通过java客户端连接之后,会报下章节的错误,此时添加虚拟机权限就行了。

2.添加虚拟机访问权限

承接上章节:

错误详情:

Exception in thread "main" java.io.IOException
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:124)
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:120)
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:142)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:407)
    at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63)
    at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:177)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1150)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1109)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1067)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1224)
    at cn.cnsy123.rabbitmq.test.RabbitMQTest.main(RabbitMQTest.java:20)
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 'admin', class-id=10, method-id=40)
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:443)
    at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:263)
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:136)
    ... 8 more

此时,添加虚拟机的权限:

# 对何种资源具有配置、写、读的权限通过正则表达式来匹配,具体命令如下:
# set_permissions [-p <vhostpath>] <user> <conf> <write> <read>
# 其中,<conf> <write> <read>的位置分别用正则表达式来匹配特定的资源,
# 如'^(amq\.gen.*|amq\.default)$'可以匹配server生成的和默认的exchange,'^$'不匹配任何资源

sudo rabbitmqctl  set_permissions -p /  username '.*' '.*' '.*'

3.rabbitmq连接工厂创建连接实例

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.7.1</version>
</dependency>
private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.1.100");
        factory.setUsername("iot");
        factory.setPassword("iot");
        Connection connection = null;
        Channel channel = null;

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();

            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello World!";
            while (true){
                message = String.valueOf(System.currentTimeMillis());
                System.out.println("chno:"+channel.getChannelNumber());
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
                System.out.println(" [x] Sent '" + message + "'");
                Thread.sleep(200);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            connection.close();
            channel.close();
        }
    }

 

4.使用连接池创建连接实例

pom依赖:

        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
public class RabbitMQUtil {
    private static String host = "192.168.1.100";
    private static final String QUEUE_NAME = "hello";
    private static CachingConnectionFactory cachingConnectionFactory;

    static {
        cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setCacheMode(CachingConnectionFactory.CacheMode.CHANNEL);
        cachingConnectionFactory.setHost(host);
        cachingConnectionFactory.setUsername("iot");
        cachingConnectionFactory.setPassword("iot");
    }
    private static Channel getChannel() {
        return cachingConnectionFactory.createConnection().createChannel(false);
    }
    public static void sendAndClose(String exchange, String routingKey, AMQP.BasicProperties props, byte[] body) throws IOException, TimeoutException {
        Channel ch = getChannel();
        ch.basicPublish(exchange, routingKey, props, body);
        ch.close();
    }
}

 

posted @ 2019-06-23 21:00  dyigstraw  阅读(427)  评论(0编辑  收藏  举报
foot