一步一步教你用rabbitMQ------Linux环境安装rabbitMQ及客户端简单使用

Linux环境安装rabbitMQ


安装

提示:安装看这篇linux中RabbitMQ安装教程,按照这篇安装就可以了,里面写的很详细了我就不写了。

踩坑

示例:linux下载插件There are no enabled repos. Run “yum repolist all” to see the repos you have.报错解决

是不是没有先安装wget 源

先安装wget,在备份如果这个指令执行失败没有关系可能是你先备份了的缘故

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

下载对应版本repo文件, 放入/etc/yum.repos.d/里

在这里插入图片描述
在这里插入图片描述
再执行 yum -y update就可以成功了

安装完,输入IP:15672就可以访问了

在这里插入图片描述

简单使用

我们来个helloworld  的demo

pom文件引入

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

连接类

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class ConnectionUtils {
    /**
     * 获取MQ的连接
     * @return MQ连接资源
     */
    public static Connection getConnection() throws Exception{
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置RabbitMQ相关信息
        factory.setHost("192.168.3.40");
        factory.setVirtualHost("tianz");
        factory.setUsername("admin");
        factory.setPassword("123456");
        factory.setPort(5672);
        //创建一个新的连接
        Connection connection = factory.newConnection();
        return connection;
    }
}

为了省事这里我就抛出异常了,不处理了,开发时别这么干

另外一定要注意,client端通信是5672,web浏览器管理界面是15672
并且guest是本地环境的默认角色,一定要自己新建个账号管理员,安装那里我已经建好了

生产者

public class Producer {
    private static final String QUEUE_NAME = "helloworld";

    public void testSendMessage() throws Exception {

            //获取一个链接
            Connection connection = ConnectionUtils.getConnection();

            //从连接中获取一个通道
            Channel channel = connection.createChannel();

            channel.queueDeclare(QUEUE_NAME,false,false,false,null);

            //发送一个消息
            String msg = "Hello World!";

            channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());

            //关闭通道和连接
            channel.close();
            connection.close();

    }

    public static void main(String[] args) throws Exception {

            new Producer().testSendMessage();

    }
}

消费者

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP;
public class Customer {
    private final static String QUEUE_NAME = "helloworld";

    public static void main(String[] args) throws Exception{


        //创建一个新的连接
        Connection connection = ConnectionUtils.getConnection();

        //创建一个通道
        Channel channel = connection.createChannel();

        //声明要关注的队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println("Customer Waiting Received messages");

        //DefaultConsumer类实现了Consumer接口,通过传入一个频道,
        // 告诉服务器我们需要那个频道的消息,如果频道中有消息,就会执行回调函数handleDelivery
        Consumer consumer = new DefaultConsumer(channel) {
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body)
                    throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("Customer Received '" + message + "'");
            }
        };
        //自动回复队列应答 -- RabbitMQ中的消息确认机制
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}

另外也要注意,你起的队列名即使没有,也会帮你创建。

那看下结果:目前队列里是没有消息的,
在这里插入图片描述
启动生产者,消息队列有1条消息了。
在这里插入图片描述

这时启动消费者,看日志,消费者消费了1条
在这里插入图片描述
在这里插入图片描述
当然,你生成了多少条,消费者就会消费多少条。这也就是rabbmit常见模式里最简单的
在这里插入图片描述

下篇我们来讲其他几种模式

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值