CentOS 7 急速入门 RabbitMQ 推送消息 “Hello World”

注意要点

  • 准备一台2G以上内存的Linux机器
  • 检查Linux机器和本机的网络、防火墙等基础设施是否可以访问
  • Linux机器是否能够和本机Ping通

进入安装:

## 1. 首先在Linux上进行一些软件的准备工作,yum下来一些基础的软件包
yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ kernel-devel m4 ncurses-devel tk tc xz

## 2. 下载RabbitMQ所需软件包(本神在这里使用的是 RabbitMQ3.6.5 稳定版本)
wget www.rabbitmq.com/releases/erlang/erlang-18.3-1.el7.centos.x86_64.rpm
wget http://repo.iotti.biz/CentOS/7/x86_64/socat-1.7.3.2-5.el7.lux.x86_64.rpm
wget www.rabbitmq.com/releases/rabbitmq-server/v3.6.5/rabbitmq-server-3.6.5-1.noarch.rpm

## 3. 安装服务命令
rpm -ivh erlang-18.3-1.el7.centos.x86_64.rpm
rpm -ivh socat-1.7.3.2-5.el7.lux.x86_64.rpm
rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm
## 4. 修改用户登录与连接心跳检测,注意修改
vim /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/ebin/rabbit.app
修改点1:loopback_users 中的 <<"guest">>,把<< >>去掉只保留guest (用于用户登录)
修改点2:heartbeat 为10(用于心跳连接)

## 5. 安装管理插件

## 5.1 首先启动服务(后面 | 包含了停止、查看状态以及重启的命令)
/etc/init.d/rabbitmq-server start | stop | status | restart
## 5.2 查看服务有没有启动: lsof -i:5672 (5672是Rabbit的默认端口)
rabbitmq-plugins enable rabbitmq_management
## 5.3 可查看管理端口有没有启动:
lsof -i:15672 或者 netstat -tnlp | grep 15672
## 6. 一切OK 我们访问地址,输入用户名密码均为 guest
## http://你的ip地址:15672/
## 7. 如果一切顺利,那么到此为止,我们的环境已经安装完啦

访问管理台:http://ip地址:15672/ 来访问图形界面 默认用户名均为guest
在这里插入图片描述
访问成功
在这里插入图片描述
搭建生产者和消费者推送 “Hello World”

  1. 首先第一步准备打开IDEA(或者Eclipse)开发工具,新建一个maven工程,引入对应的依赖包(pom.xml)
<!-- amqp-client 版本和我们机器版本一致 -->
<dependency>
	<groupId>com.rabbitmq</groupId>
	<artifactId>amqp-client</artifactId>
	<version>3.6.5</version>
</dependency>
  1. 建立生产者(Producer)
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 测试Rabbit
 * @author tuan
 * @date 2021/01/19 13:51
 **/
public class HelloWorldProducer {
	//队列名称
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception{
        ConnectionFactory cf = new ConnectionFactory();
        //rabbitmq监听IP
        cf.setHost("192.168.10.136");
        //rabbitmq默认监听端口,注意要记得开启端口
        cf.setPort(5672);

        //设置访问的用户
        cf.setUsername("guest");
        cf.setPassword("guest");
        //建立连接
        Connection conn = cf.newConnection();
        //创建消息通道
        Channel channel = conn.createChannel();

        String msg = "hello world!!!! 你好啊~";
        //创建hello队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        //发送消息
        channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
        System.out.println("send msg "+ msg + " to ["+ QUEUE_NAME +"] queue !");

        channel.close();
        conn.close();

    }

}

  1. 建立消费者(Consumer)
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * rabbitmq 消费者
 * @author tuan
 * @date 2021/01/19 13:53
 **/
public class HelloWorldConsumer {
	//队列名称
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args) throws IOException, TimeoutException, TimeoutException {

        ConnectionFactory cf = new ConnectionFactory();
        //rabbitmq监听IP
        cf.setHost("192.168.10.136");
        //rabbitmq默认监听端口,注意要记得开启端口
        cf.setPort(5672);

        //设置访问的用户
        cf.setUsername("guest");
        cf.setPassword("guest");
        //建立连接
        Connection conn = cf.newConnection();
        //创建消息通道
        Channel channel = conn.createChannel();

        //创建hello队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" Waiting for msg....");
        //创建消费者,并接受消息
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                    throws IOException {
                String msg = new String(body, "UTF-8");
                System.out.println("Received is = '" + msg + "'");
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

}

我们可以先把消费者开启等待接收消息,你会发现你的Rabbit管理台Queues队列多了hello组来接收消息,我们再去多次启动生产者没一条消息都可以被消费者监听到并消费掉

 Waiting for msg....
Received is = 'hello world!!!! 你好啊~'
Received is = 'hello world!!!! 你好啊~'
Received is = 'hello world!!!! 你好啊~'
Received is = 'hello world!!!! 你好啊~'

github源码:https://github.com/TuanPen/rabbit_hello

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值