Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

Time:2024-3-15

preamble

RabbitMQ is an AMQP (Advanced Message Queuing Protocol) based on the completion of the reusable enterprise messaging system , is currently one of the most mainstream messaging middleware .
developed by erlang AMQP (Advanced Message Queue Advanced Message Queuing Protocol) open source implementation , due to the high concurrency characteristics of the erlang language , performance is better , the essence of a queue , FIFO first-in-first-out , which stores the contents of the message , the following introduction through the ubuntu+cpolar+rabbitMQ environment to achieve remote access to the mq server-side . rabbitMQ environment, the realization of mq server-side remote access.

1. Install the erlang language

Since rabbitMQ is implemented in the erlang language, we need to install erlang.

sudo apt-get install erlang-nox

2. Install rabbitMQ

Install the latest version of rabbitMQ

sudo apt-get install rabbitmq-server

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

To view rabbitMQ status.active(running)denote

sudo systemctl status rabbitmq-server

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

Set access to MQ username account and password, admin means account (customizable), 123456 means password (customizable)

sudo rabbitmqctl add_user admin 123456

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

Set the role of the admin user above, administrator means the top administrator.

sudo rabbitmqctl set_user_tags admin administrator

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

Setting admin role permissions

sudo rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

With the above information set up, let’s move down the list.

3. Intranet penetration

Then we use cpolar to penetrate the local MQ service, so that the remote can access the connection, cpolar supports http/https/tcp protocols, unlimited traffic, easy to operate, no public IP, no router.

cpolar official website:https://www.cpolar.com/

3.1 Install cpolar Intranet Penetration (supports one-click auto-installation script)

  • cpolar installation (for domestic use)
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
  • or cpolar short link installation method: (for foreign use)
curl -sL https://git.io/cpolar | sudo bash
  • View version number
cpolar version
  • token authentication

Log in to the backend of the cpolar website, click on Authentication on the left side, check your authentication token, and then paste the token into the command line

cpolar authtoken xxxxxxx

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

  • Adding services to the system
sudo systemctl enable cpolar
  • Start the cpolar service
sudo systemctl start cpolar

The normal display isactivethen the service is in the normal online startup state

3.2 Creating an HTTP Tunnel

After installing cpolar intranet penetration locally on your ubuntu system, access local port 9200 on your ubuntu browser and open the cpolar web ui interface:.http://127.0.0.1:9200

Click Tunnel Management – Create Tunnel in the left dashboard. Since rabbitMQ defaults to port 5672, we’re going to create an http tunnel to port 5672:

  • Tunnel name: customizable, taking care not to repeat
  • Protocol: tcp
  • Local address: 5672
  • Domain type: select random domain name
  • Region: Select China VIP

strike (on the keyboard)establish

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

Open the online tunnel list, check the random public tcp address, use the following random tcp public address, you can remotely connect MQ

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

4. Remote connection to the public network

maven coordinates

<dependency>
			<groupId>com.rabbitmq</groupId>
			<artifactId>amqp-client</artifactId>
			<version>5.10.0</version>
		</dependency>

Here we use java to test the connection using the above public address, write the publisher

ConnectionFactory factory = new ConnectionFactory();
        //cpolar public address
        factory.setHost("1.tcp.cpolar.cn");
        Port number for //Public address pairs
        factory.setPort(24889);

        //User name and password
        factory.setUsername("admin");
        factory.setPassword("123456");
        Connection connection = null;
        Channel channel = null;
        try {
            // 1. Create connections and channels
            connection = factory.newConnection();
            channel = connection.createChannel();

            // 2. Declare the types of exchange and exchange for the channel
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);

            String msg = " hello world";
            // 3. Send the message to the specified exchange, the queue is specified as empty, by the exchange according to the situation need to send to which queue
            channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes());
            System.out.println("product send a msg: " + msg);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } finally {
            // 4. Close the connection
            if (channel != null) {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

Writing consumers

ConnectionFactory factory = new ConnectionFactory();
        //cpolar public address
        factory.setHost("1.tcp.cpolar.cn");
        Port number for //Public address pairs
        factory.setPort(24889);

        //User name and password
        factory.setUsername("admin");
        factory.setPassword("123456");
        Connection connection = null;
        Channel channel = null;
        try {
            // 1. Create connections and channels
            connection = factory.newConnection();
            channel = connection.createChannel();

            // 2. Declare the exchange and the exchange type for the channel.
            channel.exchangeDeclare("exchange", BuiltinExchangeType.FANOUT);

            // 3. Create a queue of random names
            String queueName = channel.queueDeclare().getQueue();

            // 4. Creating bindings for exchange and queues
            channel.queueBind(queueName, "exchange", "");
            System.out.println(" **** Consumer1 keep alive ,waiting for messages, and then deal them");
            // 5. Generate consumers and listen via callbacks
            Consumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope,
                                           com.rabbitmq.client.AMQP.BasicProperties properties, byte[] body) throws IOException {

                    // Get the content of the message and process it
                    String msg = new String(body, "UTF-8");
                    System.out.println("*********** Consumer1" + " get message :[" + msg + "]");
                }
            };
            // 6. Consuming messages
            channel.basicConsume(queueName, true, consumer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

First start the consumer, then start the publisher, then the consumer console outputs the message sent by the consumer indicating success. We have implemented remote access to MQ.

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

5. Fixed public TCP address

Since the tunnel created above uses a random address tunnel, the address will change within 24 hours, in order to make the connection more stable, we also need to fix the tcp address.

5.1 Reserve a fixed public TCP port address

Log in to the background of the cpolar official website, click Reservations on the left side, and select the reserved TCP address.

  • Region: Select China VIP
  • Description: i.e. notes, can be customized to fill in

strike (on the keyboard)reservations

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

After the address reservation is successful, the system will generate the corresponding fixed public network address, copy it down

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

5.2 Configuring Fixed Public TCP Port Addresses

Access port 9200 on your browser, log in to the cpolar web ui management interface, click Tunnel Management – Tunnel List on the left dashboard, find the tunnel you created above and click Edit on the right side

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

Modify the tunnel information to configure the reserved successful fixed tcp address into the tunnel

  • Port type: change to fixed tcp port
  • Reserved tcp address: fill in the address of the successful reservation

strike (on the keyboard)update

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

After the tunnel is successfully updated, click Status Online Tunnel List on the left dashboard, find the tunnel that needs to be edited, and you can see that the public address has been updated to a fixed TCP address.

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

After the update, we change two parameters in the code.

//cpolar public address, change it to our fixed address.
        factory.setHost("5.tcp.vip.cpolar.cn");
        //Fixed address corresponding port number
        factory.setPort(13630);

Then we restart the consumer, then the producer, and publish and consume the message as normal.

Solving Public Remote Access Problems after Installing RabbitMQ Service on Linux Ubuntu with the Help of cpolar Intranet Penetration Technology

Recommended Today

[linux] Permission Understanding

catalogs 1. shell commands and how they work 2. The concept of authority 3. Authority management 2.1 Classification of document visitors (persons) 2.2 File types and access rights (thing attributes) 2.3 Representation of file permission values 2.4 Methods for setting file access rights 3. The file directive 4. Permissions for directories★ 5. Sticky bits★ 6. […]