• 远程连接RabbitMQ失败


    远程连接RabbitMQ失败

    为了避免污染宿主系统环境,于是在虚拟机中搭建了一个linux环境并且按照了rabbitmq-server。然后在远程连接的时候一直连接失败。

    官网上面给的例子都是在本地使用系统默认的guest用户连接的。没有给出远程连接的例子,于是阅读文档发现:

    When the server first starts running, and detects that its database is uninitialised or has been deleted, it initialises a fresh database with the following resources:

    a virtual host named /
    a user named guest with a default password of guest, granted full access to the / virtual host.

    也就是刚刚安装好rabbitmq-server,系统会自动创建一个名为“/”的virtual host,同时也会创建一个用户名和密码都是guest的用户,并且应用"/ virtual host"的所有访问权限。

    因此在rabbitmq安装的机器上使用官网给出的例子:

    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.Channel;
    
    public class Send {
    	
      private final static String QUEUE_NAME = "hello";
    
      public static void main(String[] argv) throws Exception {
          	      
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
    
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        
        channel.close();
        connection.close();
      }
    }
    

    运行是没问题的。如果要切换到远程机器访问的话,单纯的修改

        factory.setHost("localhost");
    

    是不行的。

    因为guest用户只是被容许从localhost访问。官网文档描述如下:

    "guest" user can only connect via localhost

    By default, the guest user is prohibited from connecting to the broker remotely; it can only connect over a > loopback interface (i.e. localhost). This applies both to AMQP and to any other protocols enabled via plugins. Any > other users you create will not (by default) be restricted in this way.

    This is configured via the loopback_users item in the configuration file.

    If you wish to allow the guest user to connect from a remote host, you should set the loopback_users configuration item to []. A complete rabbitmq.config which does this would look like:

    [{rabbit, [{loopback_users, []}]}].
    

    默认情况下,使用下面的命令:

    sudo rabbitmqctl environment
    

    会发现:

      {default_permissions,[<<".*">>,<<".*">>,<<".*">>]},
      {default_user,<<"guest">>},
      {default_user_tags,[administrator]},
      {default_vhost,<<"/">>},
      {loopback_users,[<<"guest">>]},
      {tcp_listeners,[5672]},
    

    我这快不想使用默认的guest用户,我新建立了一个用户rollen,然后授予所有权限,使用下面的命令:

    rabbitmqctl add_user rollen root
    rabbitmqctl set_user_tags rollen administrator
    rabbitmqctl set_permissions -p / rollen ".*" ".*" ".*"
    

    然后使用下面的代码远程访问

    package com.rollenholt.rabbitmq.example1;
    
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    public class Send {
        private final static String QUEUE_NAME = "hello";
    
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("192.168.126.131");
            factory.setUsername("rollen");
            factory.setPassword("root");
            factory.setPort(5672);
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello World!";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
            channel.close();
            connection.close();
        }
    }
    

    参考文档

  • 相关阅读:
    接口测试总结
    在 github 上获取源码
    推荐一个css帮助手册的版本 同时提供chm和在线
    由csdn开源项目评选中闹出刷票问题想到投票程序的设计
    由一个园友因为上传漏洞导致网站被攻破而得到的教训
    让 SVN (TortoiseSVN)提交时忽略bin和obj目录
    未能进入中断模式,原因如下:源文件“XXXXXX”不属于正在调试的项目。
    未能加载文件或程序集“XXXXX”或它的某一个依赖项。试图加载格式不正确的程序。
    .Net AppDomain.CurrentDomain.AppendPrivatePath(@"Libs");
    C# Remoting的一个简单例子
  • 原文地址:https://www.cnblogs.com/rollenholt/p/4098089.html
Copyright © 2020-2023  润新知