Article directory
Preface
RabbitMQ is a reusable enterprise messaging system based on AMQP (Advanced Message Queuing Protocol). It is one of the most mainstream message middleware currently.
The open source implementation of AMQP (Advanced Message Queue Advanced Message Queuing Protocol) developed by erlang. Due to the high concurrency characteristics of the erlang language, it has good performance. It is essentially a queue. FIFO is first in, first out, and the content stored in it is message. The following is introduced through the Under the ubuntu+cpolar+rabbitMQ environment, remote access to the mq server is achieved.
1. Install erlang language
Since rabbitMQ is implemented in 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
Check rabbitMQ status, active(running)
indicating online
sudo systemctl status rabbitmq-server
Set the username, account, and password for accessing MQ. admin represents the account number (can be customized), and 123456 represents the password (can be customized).
sudo rabbitmqctl add_user admin 123456
Set the role of the admin user above, administrator means the highest administrator
sudo rabbitmqctl set_user_tags admin administrator
Set admin role permissions
sudo rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
After setting the above information, let’s go down.
3. Intranet penetration
Then we use [cpolar] ( cpolar - a secure intranet penetration tool ) to penetrate the local MQ service so that remote access connections can be made. cpolar supports the http/https/tcp protocol, does not limit traffic, is simple to operate, and does not require a public network IP , and no router is required.
cpolar official website: https://www.cpolar.com/
3.1 Install cpolar intranet penetration (supports one-click automatic 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 cpolar official website backend, click Verify on the left, check your authentication token, and then paste the token in the command line
cpolar authtoken xxxxxxx
- Add services to the system
sudo systemctl enable cpolar
- Start cpolar service
sudo systemctl start cpolar
If it is displayed normally active
, it means that the service is in a normal online startup state.
3.2 Create HTTP tunnel
After installing cpolar intranet penetration locally on the ubuntu system, access the local 9200 port on the ubuntu browser and open the cpolar web ui interface: http://127.0.0.1:9200 .
Click Tunnel Management on the left dashboard - Create Tunnel. Since the default port in rabbitMQ is 5672, we need to create an http tunnel pointing to port 5672:
- Tunnel name: Customizable, be careful not to repeat it
- Protocol: tcp
- Local address: 5672
- Domain name type: Choose a random domain name
- Region: Select China VIP
Click创建
Open the online tunnel list, check the random public tcp address, and use the random tcp public address below to connect to MQ remotely.
4. Public network remote connection
maven coordinates
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.10.0</version>
</dependency>
Here we use java to test using the above public network address to connect and write the publisher
ConnectionFactory factory = new ConnectionFactory();
//cpolar公网地址
factory.setHost("1.tcp.cpolar.cn");
//公网地址对于的端口号
factory.setPort(24889);
//用户名和密码
factory.setUsername("admin");
factory.setPassword("123456");
Connection connection = null;
Channel channel = null;
try {
// 1.创建连接和通道
connection = factory.newConnection();
channel = connection.createChannel();
// 2.为通道声明exchange和exchange的类型
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
String msg = " hello world";
// 3.发送消息到指定的exchange,队列指定为空,由exchange根据情况判断需要发送到哪些队列
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.关闭连接
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();
}
}
}
Write consumer
ConnectionFactory factory = new ConnectionFactory();
//cpolar公网地址
factory.setHost("1.tcp.cpolar.cn");
//公网地址对于的端口号
factory.setPort(24889);
//用户名和密码
factory.setUsername("admin");
factory.setPassword("123456");
Connection connection = null;
Channel channel = null;
try {
// 1.创建连接和通道
connection = factory.newConnection();
channel = connection.createChannel();
// 2.为通道声明exchange以及exchange类型
channel.exchangeDeclare("exchange", BuiltinExchangeType.FANOUT);
// 3.创建随机名字的队列
String queueName = channel.queueDeclare().getQueue();
// 4.建立exchange和队列的绑定关系
channel.queueBind(queueName, "exchange", "");
System.out.println(" **** Consumer1 keep alive ,waiting for messages, and then deal them");
// 5.通过回调生成消费者并进行监听
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
com.rabbitmq.client.AMQP.BasicProperties properties, byte[] body) throws IOException {
// 获取消息内容然后处理
String msg = new String(body, "UTF-8");
System.out.println("*********** Consumer1" + " get message :[" + msg + "]");
}
};
// 6.消费消息
channel.basicConsume(queueName, true, consumer);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
Start the consumer first, then start the publisher, and then the consumer console outputs the message sent by the consumer to indicate success. We have implemented remote access to MQ.
5. Fixed public network 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 network TCP port address
Log in to the cpolar official website backend, click Reserve on the left, and select the reserved TCP address.
- Region: Select China VIP
- Description: Notes, which can be customized
Click保留
After the address is successfully reserved, the system will generate the corresponding fixed public address and copy it.
5.2 Configure a fixed public network TCP port address
Access port 9200 on the browser, log in to the cpolar web ui management interface, click Tunnel Management - Tunnel List on the left dashboard, find the tunnel created above, and click Edit on the right
Modify the tunnel information and configure the successfully reserved fixed tcp address into the tunnel.
- Port type: modified to fixed tcp port
- Reserved tcp address: fill in the successfully reserved address
Click更新
After the tunnel is successfully updated, click the status online tunnel list on the left dashboard to find the tunnel that needs to be edited. You can see that the public network address has been updated to a fixed TCP address.
After updating, we modify the two parameters in the code
//cpolar公网地址,改为我们固定的地址
factory.setHost("5.tcp.vip.cpolar.cn");
//固定地址对应的端口号
factory.setPort(13630);
Then we restart the consumer, then the producer, and publish and consume messages normally to indicate success.
Reprinted from cpolar intranet penetration article: No public network IP, remote access to RabbitMQ service from external public network "intranet penetration"