rabbitmq on docker + ssh port forward = local rabbitmq

rabbitmq on docker + ssh port forward = local rabbitmq

set up a rabbit mq server on a remote server named openwrt

docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672  rabbitmq

image-20210920111227086

image-20210920111300913

set up port forward on windows

inspired by remote port forward on VSCODE

ssh -L 5672:127.0.0.1:5672 openwrt -N -v -v

image-20210920112118836

access rabbit mq server as if it is on windows

// Recv.java
// https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/java/Send.java
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class Recv {

    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);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
    }
}
// Send.java
// https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/java/Recv.java
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

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");
        try (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(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}

run Recv.java first

image-20210920110838837

then run Send.java

image-20210920110917994

switch back to Recv.java

image-20210920110931769

references

Downloading and Installing RabbitMQ — RabbitMQ

[macos - How does VSCode Remote Development] [Forward Port] work? - Stack Overflow

networking - ssh tunnel refusing connections with "channel 2: open failed" - Server Fault

RabbitMQ tutorial - "Hello World!" — RabbitMQ

posted @ 2021-09-20 12:26  Blithe-Chiang  阅读(174)  评论(0编辑  收藏  举报