Rabbitmq,连接到远程主机

时间:2017-07-04 12:50:43

标签: java tcp rabbitmq

我想远程连接到rabbitmq,我的代码是

 public class ReceiveLogsDirect {

      private static final String EXCHANGE_NAME = "direct_logs";

      public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
          factory.setUsername("guest");
          factory.setPassword("guest");
          factory.setVirtualHost("/");
          factory.setHost("192.168.23.214");
          factory.setPort(5672);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        String queueName = channel.queueDeclare().getQueue();


          channel.queueBind(queueName, EXCHANGE_NAME, "user_one");

        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        Consumer consumer = new DefaultConsumer(channel) {
          @Override
          public void handleDelivery(String consumerTag, Envelope envelope,
                                     AMQP.BasicProperties properties, byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
          }
        };
        channel.basicConsume(queueName, true, consumer);
      }
    }

192.168.23.214它是带有rabbitmq的PC的本地ipv4地址,但我有以下错误

Exception in thread "main" com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:349)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:99)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:911)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:870)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:828)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:979)
at tutorial5.ReceiveLogsDirect.main(ReceiveLogsDirect.java:21)

我的rabbitmq.config上一行:[{loopback_users, []}]. 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

最后,我将配置更改为[{rabbit, [{loopback_users, []}]}].