Centos7安装RabbitMQ与使用(超详细)

一、RabbitMQ的作用:

1、削峰填谷
2、解耦
3、平衡上下游的处理速度

二. rabbitmq基本原理

请添加图片描述
RabbitMQ是消息队列的一种实现,那么一个消息队列到底需要什么?答案是队列,即Queue,那么接下来所有名词都是围绕这个Queue来拓展的。

ConnectionFactory:
Connection:
Chanel:因为TCP连接的建立和释放都是十分昂贵的,
Exchange:分发消息
Queue:
BindingKey:
RoutingKey:

RabbitMQ的交换机类型:
Fanout类型交换机——扇出交换机
Direct类型交换机——直连交换机
Topic类型交换机——主题交换机
RabbitMQ的消息模型:
简单模型-Simple ============================================================

简单模型也叫直连模型,一个生产者一个消费者

获取连接的工具类:

package com.example.demo.rabbitMQ;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

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

public class RabbitMQUtils {
    private static final ConnectionFactory connectionFactory = new ConnectionFactory();
    static {
        connectionFactory.setHost("192.168.119.134");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
    }

    public static Connection getConnection(){
        try {
            return connectionFactory.newConnection();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
}

生产者:

package com.example.demo.rabbitMQ.simple;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


public class Producer {

    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 通过channel.queueDeclare()方法创建的Queue绑定在默认的交换机上,且BindingKey和队列名一致
 \* 第一个参数:队列名 == BindingKey
 \* 第二个参数:队列是否持久化,如果为false,rabbitMQ服务关闭队列消失
 \* 第三个参数:队列是否为排他队列,如果为true,队列仅供创建它的连接使用,当前连接关闭队列消失
 \* 第四个参数:队列是否自动删除,如果为true,队列中的消息消费完成,并且消费方关闭后,队列自动删除
 \*/
        channel.queueDeclare("simple-queue",true,false,false,null);
        /\*\*
 \* 第一个参数:交换机名称,如果为“”空串表示使用默认交换机,将消息投递到默认交换机,通过默认交换机投递到与之绑定的队列
 \* 第二个参数:RoutingKey==BindingKey,在简单模型中绑定在默认的交换机上的队列,BindingKey和队列名一致
 \* 第三个参数:消息的属性(消息是否持久化、消息存活时间)
 \* 第四个参数:消息内容
 \*/
        channel.basicPublish("","simple-queue",null,"Hellow World ".getBytes());
    }
}


消费者:

package com.example.demo.rabbitMQ.simple;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;


public class Consumer {
    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtils.getConnection();

        Channel channel = connection.createChannel();

        /\*\*
 \* 为了保险起见,防止生产方未启动队列未创建的情况下消费方启动后报404异常,最好在消费方中也声明创建队列,注意消费方和生产方声明的队列必须完全一致
 \* 注意:不用考虑队列是否会重复创建,在RabbitMQ中如果队列已经存在是不会被重新创建的
 \*/
        channel.queueDeclare("simple-queue",true,false,false,null);

        /\*\*
 \* 第一个参数:队列名,所要消费的队列
 \* 第二个参数:是否自动确认
 \* true表示自送确认:消息拿到了就确认
 \* false表示手动确认:消息处理完成后确认
 \*/
        channel.basicConsume("simple-queue",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body)+envelope.getDeliveryTag());

            }
        });
    }
}

工作模型-Work ============================================================

当消息处理比较耗时的时候,可能生产消息的速度会远远大于消息的消费速度,消息在队列中就会堆积越来越多,无法及时处理。为了平衡上下游的处理速度(生产速度大于消费速度)就可以使用工作模型:让多个消费者绑定到一个队列,共同消费队列中的消息提升消费速度。队列中的消息一旦消费,就会消失,因此任务是不会被重复执行的

生产者:

package com.example.demo.rabbitMQ.work;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Producer {

    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 通过channel.queueDeclare()方法创建的Queue绑定在默认的交换机上,且BindingKey和队列名一致
 \* 第一个参数:队列名 == BindingKey
 \* 第二个参数:队列是否持久化,如果为false,rabbitMQ服务关闭队列消失
 \* 第三个参数:队列是否为排他队列,如果为true,队列仅供创建它的连接使用,当前连接关闭队列消失
 \* 第四个参数:队列是否自动删除,如果为true,队列中的消息消费完成,并且消费方关闭后,队列自动删除
 \*/
        channel.queueDeclare("work-queue",true,false,false,null);

        for (int i = 0;i <= 1000 ;i++){
            /\*\*
 \* 第一个参数:交换机名称,如果为“”空串表示使用默认交换机,将消息投递到默认交换机,通过默认交换机投递到与之绑定的队列
 \* 第二个参数:RoutingKey==BindingKey,在工作模型中绑定在默认的交换机上的队列,BindingKey和队列名一致
 \* 第三个参数:消息的属性(消息是否持久化、消息存活时间)
 \* 第四个参数:消息内容
 \*/
            channel.basicPublish("","work-queue",null,("Hello World"+ i + " ").getBytes());
        }
    }
}

消费者1:

package com.example.demo.rabbitMQ.work;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;

public class ConsumerOne {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 为了保险起见,防止生产方未启动队列未创建的情况下消费方启动后报404异常,最好在消费方中也声明创建队列,注意消费方和生产方声明的队列必须完全一致
 \* 注意:不用考虑队列是否会重复创建,在RabbitMQ中如果队列已经存在是不会被重新创建的
 \*/
        channel.queueDeclare("work-queue",true,false,false,null);
        /\*\*
 \* 设置每次抓取的数据条数
 \* 不设置:默认平分队列中的消息,如果队列中中有100条数据,那么两个消费方各抓取50条数据进行消费
 \* 设置 :按照设置的条数抓取,如果设置1,那么消费方从队列中每次抓取1条数据进行消费,消费完成后再抓取1条,直到队列中没有消息
 \*/
        channel.basicQos(1);

        /\*\*
 \* 第一个参数:队列名,所要消费的队列
 \* 第二个参数:是否自动确认
 \* true表示自送确认:消息拿到了就确认
 \* false表示手动确认:消息处理完成后确认
 \*/

        channel.basicConsume("work-queue",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body)+envelope.getDeliveryTag());
                /\*\*
 \* 手动确认方式
 \* 第一个参数:包裹的标签(消息的标签,RabbitMQ将每个消息看成是一个包裹),是个整数
 \* 第二个参数:是否多条消息批量确认,如果第一、二、三...条消息没有确认,后面一条消息确认被消费了,那么前面所有的消息都会被确认消费了
 \*/
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }

}

消费者2:

package com.example.demo.rabbitMQ.work;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;

public class ConsumerTwo {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 为了保险起见,防止生产方未启动队列未创建的情况下消费方启动后报404异常,最好在消费方中也声明创建队列,注意消费方和生产方声明的队列必须完全一致
 \* 注意:不用考虑队列是否会重复创建,在RabbitMQ中如果队列已经存在是不会被重新创建的
 \*/
        channel.queueDeclare("work-queue",true,false,false,null);

        /\*\*
 \* 设置每次抓取的数据条数
 \* 不设置:默认平分队列中的消息,如果队列中中有100条数据,那么两个消费方各抓取50条数据进行消费
 \* 设置 :按照设置的条数抓取,如果设置1,那么消费方从队列中每次抓取1条数据进行消费,消费完成后再抓取1条,直到队列中没有消息
 \*/
        channel.basicQos(1);

        /\*\*
 \* 第一个参数:队列名,所要消费的队列
 \* 第二个参数:是否自动确认
 \* true表示自送确认:消息拿到了就确认
 \* false表示手动确认:消息处理完成后确认
 \*/

        channel.basicConsume("work-queue",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(new String(body)+envelope.getDeliveryTag());
                /\*\*
 \* 手动确认方式
 \* 第一个参数:包裹的标签(消息的标签,RabbitMQ将每个消息看成是一个包裹),是个整数
 \* 第二个参数:是否多条消息批量确认,如果第一、二、三...条消息没有确认,后面一条消息确认被消费了,那么前面所有的消息都会被确认消费了
 \*/
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }

}

广播模型-Fanout ============================================================

在广播模式下,生产者将消息发送给交换机后,交换机将消息推送给每一个绑定在交换机上的队列,每一个队列的消费者都能拿到消息。在Fanout模式中,一条消息,会被所有绑定在该交换机上的队列消费。

生产者:

package com.example.demo.rabbitMQ.fanout;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Producer {
    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();

        /\*\*
 \* 声明一个Fanout类型的交换机
 \* 第一个参数:交换机名称
 \* 第二个参数:交换机类型:BuiltinExchangeType.FANOUT、BuiltinExchangeType.TOPIC、BuiltinExchangeType.DIRECT
 \*/
        channel.exchangeDeclare("fanout-exchange", BuiltinExchangeType.FANOUT);

        /\*\*
 \* 注意:通过channel.queueDeclare()方法创建的Queue绑定在默认的交换机上,且BindingKey和队列名一致
 \* 第一个参数:队列名
 \* 第二个参数:队列是否持久化,如果为false,rabbitMQ服务关闭队列消失
 \* 第三个参数:队列是否为排他队列,如果为true,队列仅供创建它的连接使用,当前连接关闭队列消失
 \* 第四个参数:队列是否自动删除,如果为true,队列中的消息消费完成,并且消费方关闭后,队列自动删除
 \*/
        channel.queueDeclare("fanout-queue",true,false,false,null);

        /\*\*
 \* 将队列绑定到交换机上
 \* 第一个参数:队列名称
 \* 第二个参数:交换机名称
 \* 第三个参数:BindingKey
 \*/
        channel.queueBind("fanout-queue","fanout-exchange","fanout-model");

        /\*\*
 \* 第一个参数:交换机名称,如果为“”空串表示使用默认交换机,将消息投递到默认交换机,通过默认交换机投递到与之绑定的队列
 \* 第二个参数:RoutingKey==BindingKey,在Fanout模式下指定RoutingKey没有意义,Fanout模式下每个与该交换机绑定的队列都能拿到消息
 \* 第三个参数:消息的属性(消息是否持久化、消息存活时间)
 \* 第四个参数:消息内容
 \*/
        channel.basicPublish("fanout-exchange","fanout-model",null,"Hellow World ".getBytes());
    }
}

消费者1:

package com.example.demo.rabbitMQ.fanout;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;

public class ConsumerOne {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 声明一个Fanout类型的交换机
 \* 第一个参数:交换机名称
 \* 第二个参数:交换机类型
 \*/
        channel.exchangeDeclare("fanout-exchange", BuiltinExchangeType.FANOUT);
        /\*\*
 \* 注意:通过channel.queueDeclare()方法创建的Queue绑定在默认的交换机上,且BindingKey和队列名一致
 \* 第一个参数:队列名
 \* 第二个参数:队列是否持久化,如果为false,连接关闭队列消失
 \* 第三个参数:队列是否为排他队列,如果为true,队列仅供创建它的连接使用,当前连接关闭队列消失
 \* 第四个参数:队列是否自动删除,如果为true,队列中的消息消费完成,并且消费方关闭后,队列自动删除
 \* getQueue()方法可获取队列名称
 \*/
        String queueName = channel.queueDeclare("fanout-queue-one", true, false, false, null).getQueue();
        /\*\*
 \* 将队列绑定到交换机上
 \* 第一个参数:队列名称
 \* 第二个参数:交换机名称
 \* 第三个参数:BindingKey
 \*/
        channel.queueBind(queueName,"fanout-exchange","fanout-model-one");
        /\*\*
 \* 从队列中消费消息
 \* 第一个参数:队列名称
 \* 第二个参数:是否自动确认
 \* 第三个参数:消费者
 \*/
        channel.basicConsume("fanout-queue-one",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body)+"One"+" "+envelope.getDeliveryTag());
                /\*\*
 \* 确认消息是否消费,给队列反馈
 \* 第一个参数:包裹(消息)标签
 \* 第二个参数:是否多条消息批量确认,如果第一、二、三...条消息没有确认,后面一条消息确认被消费了,那么前面所有的消息都会被确认消费了
 \*/
                channel.basicAck(envelope.getDeliveryTag(),true);
            }
        });
    }
}

消费者2:

package com.example.demo.rabbitMQ.fanout;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;

public class ConsumerTwo {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 声明一个Fanout类型的交换机
 \* 第一个参数:交换机名称
 \* 第二个参数:交换机类型
 \*/
        channel.exchangeDeclare("fanout-exchange", BuiltinExchangeType.FANOUT);
        /\*\*
 \* 注意:通过channel.queueDeclare()方法创建的Queue绑定在默认的交换机上,且BindingKey和队列名一致
 \* 第一个参数:队列名
 \* 第二个参数:队列是否持久化,如果为false,rabbitMQ服务关闭队列消失
 \* 第三个参数:队列是否为排他队列,如果为true,队列仅供创建它的连接使用,当前连接关闭队列消失
 \* 第四个参数:队列是否自动删除,如果为true,队列中的消息消费完成,并且消费方关闭后,队列自动删除
 \*/
        channel.queueDeclare("fanout-queue-two",true,false,false,null);
        /\*\*
 \* 将队列绑定到交换机上
 \* 第一个参数:队列名称
 \* 第二个参数:交换机名称
 \* 第三个参数:BindingKey
 \*/
        channel.queueBind("fanout-queue-two","fanout-exchange","fanout-model-two");
        /\*\*
 \* 从队列中消费消息
 \* 第一个参数:队列名称
 \* 第二个参数:是否自动确认
 \* 第三个参数:消费者
 \*/
        channel.basicConsume("fanout-queue-two",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body)+"Two");
            }
        });
    }
}

消费者3:

package com.example.demo.rabbitMQ.fanout;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;

public class ConsumerThree {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 声明一个Fanout类型的交换机
 \* 第一个参数:交换机名称
 \* 第二个参数:交换机类型
 \*/
        channel.exchangeDeclare("fanout-exchange", BuiltinExchangeType.FANOUT);
        /\*\*
 \* 注意:通过channel.queueDeclare()方法创建的Queue绑定在默认的交换机上,且BindingKey和队列名一致
 \* 第一个参数:队列名
 \* 第二个参数:队列是否持久化,如果为false,连接关闭队列消失
 \* 第三个参数:队列是否为排他队列,如果为true,队列仅供创建它的连接使用,当前连接关闭队列消失
 \* 第四个参数:队列是否自动删除,如果为true,队列中的消息消费完成,并且消费方关闭后,队列自动删除
 \* getQueue()方法可获取队列名称
 \*/
        String queueName = channel.queueDeclare("fanout-queue-one", true, false, false, null).getQueue();
        /\*\*
 \* 将队列绑定到交换机上
 \* 第一个参数:队列名称
 \* 第二个参数:交换机名称
 \* 第三个参数:BindingKey
 \*/
        channel.queueBind(queueName,"fanout-exchange","fanout-model-one");
        /\*\*
 \* 设置每次抓取的数据条数
 \* 不设置:默认平分队列中的消息,如果队列中中有100条数据,那么两个消费方各抓取50条数据进行消费
 \* 设置 :按照设置的条数抓取,如果设置1,那么消费方从队列中每次抓取1条数据进行消费,消费完成后再抓取1条,直到队列中没有消息
 \*/
        channel.basicQos(1);
        /\*\*
 \* 从队列中消费消息
 \* 第一个参数:队列名称
 \* 第二个参数:是否自动确认
 \* 第三个参数:消费者
 \*/
        channel.basicConsume(queueName,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body)+"Three"+" "+envelope.getDeliveryTag());
            }
        });
    }
}

定向模型-Direct ============================================================

在定向模型中,生产者将消息发送给交换机后,交换机将消息根据RoutingKey\BindingKey推送到对应的队列上。Exchange不再把消息交给每一个绑定的队列,而是根据消息的Routing Key进行判断,只有队列的BindingKey与消息的 RoutingKey完全一致,才会接收到消息
生产者:

package com.example.demo.rabbitMQ.direct;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("direct-exchange", BuiltinExchangeType.DIRECT);
        //队列1
        String queue1Name = channel.queueDeclare("direct-queue-one", true, false, false, null).getQueue();
        channel.queueBind(queue1Name,"direct-exchange","direct-model-one");
        //队列2
        String queue2Name = channel.queueDeclare("direct-queue-two", true, false, false, null).getQueue();
        channel.queueBind(queue2Name,"direct-exchange","direct-model-two");

        channel.basicPublish("direct-exchange","direct-model-two",null,"JAVA是世界上最好的语言 ".getBytes(StandardCharsets.UTF\_8));
    }
}


消费者1:

package com.example.demo.rabbitMQ.direct;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ConsumerOne {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("direct-exchange", BuiltinExchangeType.DIRECT);
        String queueName = channel.queueDeclare("direct-queue-one", true, false, false, null).getQueue();
        channel.queueBind(queueName,"direct-exchange","direct-model-one");

        channel.basicConsume(queueName,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body, StandardCharsets.UTF\_8)+"One");
            }
        });
    }
}


消费者2:

package com.example.demo.rabbitMQ.direct;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;

public class ConsumerTwo {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("direct-exchange", BuiltinExchangeType.DIRECT);

        String queueName = channel.queueDeclare("direct-queue-two", true, false, false, null).getQueue();
        channel.queueBind(queueName,"direct-exchange","direct-model-two");

        channel.basicConsume(queueName,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body)+"Two");
            }
        });
    }
}


主题模型-Topic ============================================================

在主题模式下,生产者将消息发送给交换机后,交换机根据RoutingKey\BindingKey匹配队列,将消息推送到匹配的队列上。在topic模式下通过#号和*号进行模糊匹配,通过.进行分割,#号表示可能有一个或多个单词,也可能没有;*号表示有且仅有一个单词

生产者:

package com.example.demo.rabbitMQ.topic;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Producer {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 声明一个TOPIC类型的交换机
 \* 第一个参数:交换机名称
 \* 第二个参数:交换机类型:BuiltinExchangeType.FANOUT、BuiltinExchangeType.TOPIC、BuiltinExchangeType.DIRECT
 \*/
        channel.exchangeDeclare("topic-exchange", BuiltinExchangeType.TOPIC);
        /\*\*
 \* 声明队列1:
 \* 注意:通过channel.queueDeclare()方法创建的Queue绑定在默认的交换机上,且BindingKey和队列名一致
 \* 第一个参数:队列名
 \* 第二个参数:队列是否持久化,如果为false,rabbitMQ服务关闭队列消失
 \* 第三个参数:队列是否为排他队列,如果为true,队列仅供创建它的连接使用,当前连接关闭队列消失
 \* 第四个参数:队列是否自动删除,如果为true,队列中的消息消费完成,并且消费方关闭后,队列自动删除
 \*/
        String queue1Name = channel.queueDeclare("topic-queue-one", true, false, false, null).getQueue();
        /\*\*
 \* 将队列绑定到交换机上
 \* 第一个参数:队列名称
 \* 第二个参数:交换机名称
 \* 第三个参数:BindingKey,在topic模式下通过#和\*进行模糊匹配,通过.进行分割,#号表示可能有一个或多个单词,也可能没有;\*号表示有且仅有一个单词
 \*/
        channel.queueBind(queue1Name,"topic-exchange","topic-model.#");

        /\*\*
 \* 声明队列2:
 \* 注意:通过channel.queueDeclare()方法创建的Queue绑定在默认的交换机上,且BindingKey和队列名一致
 \* 第一个参数:队列名
 \* 第二个参数:队列是否持久化,如果为false,rabbitMQ服务关闭队列消失
 \* 第三个参数:队列是否为排他队列,如果为true,队列仅供创建它的连接使用,当前连接关闭队列消失
 \* 第四个参数:队列是否自动删除,如果为true,队列中的消息消费完成,并且消费方关闭后,队列自动删除
 \*/
        String queue2Name = channel.queueDeclare("topic-queue-two", true, false, false, null).getQueue();
        /\*\*
 \* 将队列绑定到交换机上
 \* 第一个参数:队列名称
 \* 第二个参数:交换机名称
 \* 第三个参数:BindingKey,在topic模式下通过#和\*进行模糊匹配,通过.进行分割,#号表示可能有一个或多个单词,也可能没有;\*号表示有且仅有一个单词
 \*/
        channel.queueBind(queue2Name,"topic-exchange","topic-model.\*");

        /\*\*
 \* 声明队列3:
 \* 注意:通过channel.queueDeclare()方法创建的Queue绑定在默认的交换机上,且BindingKey和队列名一致
 \* 第一个参数:队列名
 \* 第二个参数:队列是否持久化,如果为false,rabbitMQ服务关闭队列消失
 \* 第三个参数:队列是否为排他队列,如果为true,队列仅供创建它的连接使用,当前连接关闭队列消失
 \* 第四个参数:队列是否自动删除,如果为true,队列中的消息消费完成,并且消费方关闭后,队列自动删除
 \*/
        String queue3Name = channel.queueDeclare("topic-queue-three", true, false, false, null).getQueue();
        /\*\*
 \* 将队列绑定到交换机上
 \* 第一个参数:队列名称
 \* 第二个参数:交换机名称
 \* 第三个参数:BindingKey,在topic模式下通过#和\*进行模糊匹配,通过.进行分割,#号表示可能有一个或多个单词,也可能没有;\*号表示有且仅有一个单词
 \*/
        channel.queueBind(queue3Name,"topic-exchange","topic-model.three.#");

        /\*\*
 \* 第一个参数:交换机名称,如果为“”空串表示使用默认交换机,将消息投递到默认交换机,通过默认交换机投递到与之绑定的队列
 \* 第二个参数:RoutingKey==BindingKey,在topic模式下通过#和\*进行模糊匹配,通过.进行分割,#号表示可能有一个或多个单词,也可能没有;\*号表示有且仅有一个单词
 \* 第三个参数:消息的属性(消息是否持久化、消息存活时间)
 \* 第四个参数:消息内容
 \*/
        channel.basicPublish("topic-exchange","topic-model.three.java",null,"java是世界上最优秀的语言".getBytes());

    }
}


消费者1:

package com.example.demo.rabbitMQ.topic;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ConsumerOne {
    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 为了保险起见,防止生产方未启动队列\交换机未创建消的情况下费方启动后报404异常,最好在消费方中也声明创建队列,注意消费方和生产方声明的队列\交换机必须完全一致
 \* 注意:不用考虑队列\交换机是否会重复创建,在RabbitMQ中如果队列已经存在是不会被重新创建的
 \*/

        //声明交换机
        channel.exchangeDeclare("topic-exchange", BuiltinExchangeType.TOPIC);
        //声明队列
        String queue1Name = channel.queueDeclare("topic-queue-one", true, false, false, null).getQueue();
        //将队列绑定到交换机上
        channel.queueBind(queue1Name,"topic-exchange","topic-model.#");
        //设置每次抓取的数据条数
        channel.basicQos(1);
        /\*\*
 \* 第一个参数:被消费的队列名
 \* 第二个参数:是否自动确认
 \* 第三个参数:使用默认的消费者
 \*/
        channel.basicConsume(queue1Name,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body, StandardCharsets.UTF\_8));
                //在手动确认的情况下使用
                /\*\*
 \* 确认消息是否消费,给队列反馈
 \* 第一个参数:包裹(消息)标签
 \* 第二个参数:是否多条消息批量确认,如果第一、二、三...条消息没有确认,后面一条消息确认被消费了,那么前面所有的消息都会被确认消费了
 \*/
                //channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }
}


消费者2:

package com.example.demo.rabbitMQ.topic;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ConsumerTwo {
    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 为了保险起见,防止生产方未启动队列\交换机未创建消的情况下费方启动后报404异常,最好在消费方中也声明创建队列,注意消费方和生产方声明的队列\交换机必须完全一致
 \* 注意:不用考虑队列\交换机是否会重复创建,在RabbitMQ中如果队列已经存在是不会被重新创建的
 \*/

        //声明交换机
        channel.exchangeDeclare("topic-exchange", BuiltinExchangeType.TOPIC);
        //声明队列
        String queue2Name = channel.queueDeclare("topic-queue-two", true, false, false, null).getQueue();
        //将队列绑定到交换机上
        channel.queueBind(queue2Name,"topic-exchange","topic-model.\*");
        //设置每次抓取的数据条数
        channel.basicQos(1);
        /\*\*
 \* 第一个参数:被消费的队列名
 \* 第二个参数:是否自动确认
 \* 第三个参数:使用默认的消费者
 \*/
        channel.basicConsume(queue2Name,false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body, StandardCharsets.UTF\_8));
                //在手动确认的情况下使用
                /\*\*
 \* 确认消息是否消费,给队列反馈
 \* 第一个参数:包裹(消息)标签
 \* 第二个参数:是否多条消息批量确认,如果第一、二、三...条消息没有确认,后面一条消息确认被消费了,那么前面所有的消息都会被确认消费了
 \*/
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }
}


消费者3:

package com.example.demo.rabbitMQ.topic;

import com.example.demo.rabbitMQ.RabbitMQUtils;
import com.rabbitmq.client.\*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ConsumerThree {
    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        /\*\*
 \* 为了保险起见,防止生产方未启动队列\交换机未创建消的情况下费方启动后报404异常,最好在消费方中也声明创建队列,注意消费方和生产方声明的队列\交换机必须完全一致
 \* 注意:不用考虑队列\交换机是否会重复创建,在RabbitMQ中如果队列已经存在是不会被重新创建的
 \*/

        //声明交换机
        channel.exchangeDeclare("topic-exchange", BuiltinExchangeType.TOPIC);
        //声明队列
        String queue3Name = channel.queueDeclare("topic-queue-two", true, false, false, null).getQueue();
        //将队列绑定到交换机上
        channel.queueBind(queue3Name,"topic-exchange","topic-model");
        //设置每次抓取的数据条数
        channel.basicQos(1);
        /\*\*
 \* 第一个参数:被消费的队列名
 \* 第二个参数:是否自动确认
 \* 第三个参数:使用默认的消费者
 \*/
        channel.basicConsume(queue3Name,false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body, StandardCharsets.UTF\_8));
                //在手动确认的情况下使用
                /\*\*
 \* 确认消息是否消费,给队列反馈
 \* 第一个参数:包裹(消息)标签
 \* 第二个参数:是否多条消息批量确认,如果第一、二、三...条消息没有确认,后面一条消息确认被消费了,那么前面所有的消息都会被确认消费了
 \*/
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }
}


SpringBoot+RabbitMQ

第一步:先导依赖

<!--============================================RabbitMQ相关依赖开始============================================-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.4</version>
        </dependency>
<!--============================================RabbitMQ相关依赖结束============================================-->

第二步:配置RabbitMQ

spring:
  rabbitmq:
    host: 192.168.119.134
    port: 5672
    username: rabbit
    password: 123456

第三步:配置RabbitMQ使用的序列化

// 消息的消费方json数据的反序列化
@Bean
public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(
    			ConnectionFactory connectionFactory){
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(new Jackson2JsonMessageConverter());
    return factory;
}

// 定义使用json的方式转换数据
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
    RabbitTemplate amqpTemplate = new RabbitTemplate();
    amqpTemplate.setConnectionFactory(connectionFactory);
    amqpTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    return amqpTemplate;
}

第四步:根据情况选择合适的模型

一、简单模型 ============================================================

特点:队列是绑定在默认交换机上,BindingKey就是队列名

消息投递方:
在SpringBoot中即可使用RabbitTemplate进行消息的投递,也可使用原生的方式进行消息投递

package com.example.demo.rabbitMQ.springBootrabbitMQ.simple;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rabbit")
public class Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("simple")
    public String sendMessage(){
        rabbitTemplate.convertAndSend("simple-queue","落霞与孤鹜齐飞,秋水共长天一色");
        return "成功";
    }
}

消息消费方
在SpringBoot中即可使用注解@RabbitListener进行消息的投递,也可使用原生的方式进行消息投递

package com.example.demo.rabbitMQ.springBootrabbitMQ.simple;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {
    /\*\*
 \* 1、@RabbitListener(queues = {"topic-queue-one"} ),一个@RabbitListener就是一个消费者
 \* 2、使用queues,如果队列不存在就会报异常,使用queuesToDeclare,如果队列不存在就创建队列
 \* 3、注意:SpringBoot提供了一个很好的消息确认机制,如果消费方消费的过程中有异常,一定要像service层一样抛出异常不能捕捉
 \* 4、监听队列,如果有消息就进行消费
 \* 5、简单模型和工作模型的queue都是绑定在默认交换机上
 \*/
    @RabbitListener(queuesToDeclare = {@Queue(name = "simple-queue",durable = "true")})
    public void getMessage(String message){
        System.out.println("springBoot+rabbitMQ: "+message);
    }
}

二、Work模型 ============================================================

特点:工作模型只是在简单模型的基础上增加了多个消费者

消息投递方

package com.example.demo.rabbitMQ.springBootrabbitMQ.work;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rabbit")
public class Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping(value = "/work",method = RequestMethod.GET)
    public void sendMessage(){
        rabbitTemplate.convertAndSend("work-queue-one","祖国您好!");
    }
}

消息消费方

package com.example.demo.rabbitMQ.springBootrabbitMQ.work;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    /\*\*
 \* 1、@RabbitListener(queues = {"topic-queue-one"} ),一个@RabbitListener就是一个消费者
 \* 2、使用queues,如果队列不存在就会报异常,使用queuesToDeclare,如果队列不存在就创建队列
 \* 3、注意:SpringBoot提供了一个很好的消息确认机制,如果消费方消费的过程中有异常,一定要像service层一样抛出异常不能捕捉
 \* 4、监听队列,如果有消息就进行消费
 \* 5、简单模型和工作模型的queue都是绑定在默认交换机上,工作模型就是在简单模型的基础上增加了多个消费者
 \*/

    @RabbitListener(queuesToDeclare = @Queue("work-queue-one"))
    public void getMessageOne(String message){
        System.out.println(message);
    }

    @RabbitListener(queuesToDeclare = @Queue("work-queue-two"))
    public void getMessageTwo(String message){
        System.out.println(message);
    }
}

三、Fanout模型 ============================================================

特点:凡是绑定在该交换机上的队列都会收到消息,无关路由键

消息投递方

package com.example.demo.rabbitMQ.springBootrabbitMQ.fanout;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("rabbit")
public class Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("fanout")
    //fanout类型的交换机无关路由键
    public void sendMessage(){
        rabbitTemplate.convertAndSend("fanout-exchange","","这是fanout类型的交换机");
    }
}

消息消费方

package com.example.demo.rabbitMQ.springBootrabbitMQ.fanout;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

public class Consumer {
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "fanout-queue-one"),
            exchange = @Exchange(name = "fanout-exchange",type = ExchangeTypes.FANOUT),
            key = "fanout-bindingKey-one"
    )})
    public void getMessageOne(String message){
        System.out.println(message);
    }

    @RabbitListener(bindings = @QueueBinding(
            //默认是持久化的
            value = @Queue("fanout-queue-two"),
            exchange = @Exchange(name = "fanout-exchange",type = ExchangeTypes.FANOUT),
            //fanout模式下可以不指定key
            key = "fanout-bindingKey-two"
    ))
    public void getMessageTwo(String message){
        System.out.println(message);
    }
}

四、Direct模型 ============================================================

特点:根据RoutingKey向对应的队列投递消息

消息投递方

package com.example.demo.rabbitMQ.springBootrabbitMQ.direct;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rabbit")
public class Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/direct")
    public void sendMessage(){
        rabbitTemplate.convertAndSend("direct-exchange","direct-bindingKey-one","五十六个民族,五十六只花");
    }
}


消息消费方

package com.example.demo.rabbitMQ.springBootrabbitMQ.direct;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(name = "direct-queue-one"),exchange = @Exchange(name = "direct-exchange",type = ExchangeTypes.DIRECT),key = "direct-bindingKey-one")})
    public void getMessageOne(String message){
        System.out.println(message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct-queue-two"),exchange = @Exchange(name = "direct-exchange",type = ExchangeTypes.DIRECT),key = "direct-bindingKey-two"))
    public void getMessageTwo(String message){
        System.out.println(message);
    }
}


五、Topic模型 ============================================================

特点:根据RoutingKey向匹配的队列投递消息,其BindingKey中含有#号和*号,#匹配多个单词,*号匹配一个单词

消息投递方

package com.example.demo.rabbitMQ.springBootrabbitMQ.topic;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rabbit")
public class Producer {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/topic")
    public void sendMessage(){
        rabbitTemplate.convertAndSend("topic-exchange","topic-bindingKey-one.java","行路难,行路难,多歧路,今安在,长风破浪会有时,直挂云帆济沧海");
    }
}


消息消费方

package com.example.demo.rabbitMQ.springBootrabbitMQ.topic;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic-queue-one"),
            exchange = @Exchange(name = "topic-exchange",type = ExchangeTypes.TOPIC),
            key = "topic-bindingKey-one"
    ))
    public void getMessageOne(String message){
        System.out.println(message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic-queue-two"),
            exchange = @Exchange(name = "topic-exchange",type = ExchangeTypes.TOPIC),
            key = "topic-bindingKey-one.\*"
  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值