守望者--AIR技术交流

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
热搜: ANE FlasCC 炼金术
查看: 2730|回复: 0

[服务器环境] 远程连接RabbitMQ失败

[复制链接]
  • TA的每日心情
    擦汗
    2018-4-10 15:18
  • 签到天数: 447 天

    [LV.9]以坛为家II

    1742

    主题

    2094

    帖子

    13万

    积分

    超级版主

    Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18

    威望
    562
    贡献
    29
    金币
    52683
    钢镚
    1422

    开源英雄守望者

    发表于 2017-10-20 10:36:50 | 显示全部楼层 |阅读模式
    来源:http://www.cnblogs.com/rollenholt/p/4098089.html


    为了避免污染宿主系统环境,于是在虚拟机中搭建了一个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();
        }
    }

    参考文档


    ==============================================================================

    本博客已经废弃,不在维护。新博客地址:http://wenchao.ren

    守望者AIR技术交流社区(www.airmyth.com)
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    
    关闭

    站长推荐上一条 /4 下一条

    QQ|手机版|Archiver|网站地图|小黑屋|守望者 ( 京ICP备14061876号

    GMT+8, 2024-4-17 02:44 , Processed in 0.041528 second(s), 31 queries .

    守望者AIR

    守望者AIR技术交流社区

    本站成立于 2014年12月31日

    快速回复 返回顶部 返回列表