农民 发表于 2024-7-14 18:32:59

如何在Ubuntu体系部署RabbitMQ服务器并公网访问【内网穿透】

前言

RabbitMQ是一个在 AMQP(高级消息队列协议)底子上完成的,可复用的企业消息体系,是当前最主流的消息中间件之一。
由erlang开发的AMQP(Advanced Message Queue 高级消息队列协议 )的开源实现,由于erlang 语言的高并发特性,性能较好,本质是个队列,FIFO 先入先出,内里存放的内容是message,下面介绍通过在ubuntu+cpolar+rabbitMQ环境下,实现mq服务端远程访问。
1.安装erlang 语言

由于rabbitMQ是erlang语言实现的,以是我们需要安装erlang
sudo apt-get install erlang-nox
2.安装rabbitMQ

安装最新版rabbitMQ
sudo apt-get install rabbitmq-server
https://img-blog.csdnimg.cn/img_convert/509939b577b38c11a252abecfc6d8fd3.png#pic_center
检察rabbitMQ状态,active(running)表示在线
sudo systemctl status rabbitmq-server
https://img-blog.csdnimg.cn/img_convert/52b8b7b62230699b6ab6a04a109b5b90.png#pic_center
设置访问MQ用户名账号和暗码,admin表示账号(可自定义),123456表示暗码(可自定义)
sudo rabbitmqctl add_user admin 123456
https://img-blog.csdnimg.cn/img_convert/e4caf5cbcab790b8c646eb1677c68abb.png#pic_center
设置上面admin用户的脚色,administrator表示是最高管理员
sudo rabbitmqctl set_user_tags admin administrator
https://img-blog.csdnimg.cn/img_convert/2a7440d98dbfc8aebec52cc9e12d4dec.png#pic_center
设置admin脚色权限
sudo rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
https://img-blog.csdnimg.cn/img_convert/6963cc1559affc28dbf646adad12504f.png#pic_center
以上信息设置好后,我们往下走。
3. 内网穿透

接着我们使用(cpolar - 安全的内网穿透工具)穿透本地MQ服务,使得远程可以进行访问毗连,cpolar支持http/https/tcp协议,不限制流量,操纵简单,无需公网IP,也无需路由器。
   cpolar官网:https://www.cpolar.com/
3.1 安装cpolar内网穿透(支持一键主动安装脚本)



[*]cpolar 安装(国内使用)
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash


[*]或 cpolar短链接安装方式:(国外使用)
curl -sL https://git.io/cpolar | sudo bash


[*]检察版本号
cpolar version


[*]token认证
登录cpolar官网背景,点击左侧的验证,检察自己的认证token,之后将token贴在命令行里
cpolar authtoken xxxxxxx
https://img-blog.csdnimg.cn/img_convert/95c6f14c92bb7b14bdff09f75880b9f9.png#pic_center


[*]向体系添加服务
sudo systemctl enable cpolar


[*]启动cpolar服务
sudo systemctl start cpolar
正常表现为active则表示服务为正常在线启动状态
3.2 创建HTTP隧道

在ubuntu体系本地安装cpolar内网穿透之后,在ubuntu欣赏器上访问本地9200端口,打开cpolar web ui界面:http://127.0.0.1:9200。
点击左侧仪表盘的隧道管理——创建隧道,由于rabbitMQ中默认的是5672端口,因此我们要来创建一条http隧道,指向5672端口:


[*]隧道名称:可自定义,注意不要重复
[*]协议:tcp
[*]本地所在:5672
[*]域名范例:选择随机域名
[*]地区:选择China VIP
点击创建
https://img-blog.csdnimg.cn/img_convert/d47245e0b75769c29ca3738c6ce269c2.png#pic_center
打开在线隧道列表,检察随机公网tcp所在,使用下面随机的tcp公网所在,即可远程毗连MQ
https://img-blog.csdnimg.cn/img_convert/7613f976e28f1ff599de823ed407e094.png#pic_center
4. 公网远程毗连

maven坐标
<dependency>
                        <groupId>com.rabbitmq</groupId>
                        <artifactId>amqp-client</artifactId>
                        <version>5.10.0</version>
                </dependency>
这里使用java 测试使用上面公网所在进行毗连,编写发布者
       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();
                }
            }
      }


编写斲丧者

      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();
      }
先启动斲丧者,然后启动发布者,然后斲丧者控制台输出斲丧者发送的消息表示乐成.我们实现了远程访问MQ。
https://img-blog.csdnimg.cn/img_convert/b468d333d3d971ad253222e2e92360a3.png#pic_center
5.固定公网TCP所在

由于以上创建的隧道使用的是随机所在隧道,所在会在24小时内变化,为了使毗连更加稳定,我们还需要固定tcp所在。
5.1 保存一个固定的公网TCP端口所在

登录cpolar官网背景,点击左侧的预留,选择保存的TCP所在。


[*]地区:选择China VIP
[*]形貌:即备注,可自定义填写
点击保存
https://img-blog.csdnimg.cn/img_convert/48248189a046764142f633652ca2f6d8.png#pic_center
所在保存乐成后,体系会生成相应的固定公网所在,将其复制下来
https://img-blog.csdnimg.cn/img_convert/da2d0cb2a5a947ea9e5b53523d6146b4.png#pic_center
5.2 配置固定公网TCP端口所在

在欣赏器上访问9200端口,登录cpolar web ui管理界面,点击左侧仪表盘的隧道管理——隧道列表,找到上面创建的隧道,点击右侧的编辑
https://img-blog.csdnimg.cn/img_convert/61b2611a9ff4d21211369ab28104d303.png#pic_center
修改隧道信息,将保存乐成的固定tcp所在配置到隧道中


[*]端口范例:修改为固定tcp端口
[*]预留的tcp所在:填写保存乐成的所在
点击更新
https://img-blog.csdnimg.cn/img_convert/6768f27d05adba61671d0789b671e58c.png#pic_center
隧道更新乐成后,点击左侧仪表盘的状态在线隧道列表,找到需要编辑的隧道,可以看到公网所在已经更新成为了固定TCP所在。
https://img-blog.csdnimg.cn/img_convert/41f3d11da8ffbe8be384485fe2234cd8.png#pic_center
更新好后,我们修改代码中的两个参数
           //cpolar公网地址,改为我们固定的地址
      factory.setHost("5.tcp.vip.cpolar.cn");
      //固定地址对应的端口号
      factory.setPort(13630);
然后我们重新启动斲丧者,再启动生产者,正常发布和斲丧消息表示乐成
https://img-blog.csdnimg.cn/img_convert/137403907a19b7ec12eeb0c4283261df.png#pic_center
转载自cpolar内网穿透的文章:无公网IP,在外公网远程访问RabbitMQ服务「内网穿透」

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 如何在Ubuntu体系部署RabbitMQ服务器并公网访问【内网穿透】