RabbitMQ


1.最简单的HelloWorld直连(点对点 登录加积分功能等等)


在这里插入图片描述

provider-(通道channel)>消息中间件-(channel)>customer
首先就factory工厂 然后工厂里拿连接connection 然后connection里拿通道并且设置属性
//1.先与服务器连接 创建连接工厂(提供者)
ConnectionFactory connectionFactory = new ConnectionFactory();
//设置rabbitmq的主机
connectionFactory.setHost(“8.140.174.18”);
//port
connectionFactory.setPort(5672);
//设置连接哪个虚拟主机
connectionFactory.setVirtualHost("/ems");
//设置访问虚拟主机的用户名和密码
connectionFactory.setUsername(“ems”);
connectionFactory.setPassword(“12345”);
//获取连接对象
Connection connection = connectionFactory.newConnection();
//获取连接通道对象
Channel channel = connection.createChannel();
//绑定通道到消息队列
//参数1是队列的名称 不存在的情况下自动创建
//参数2是是否持久化 是否存磁盘
//参数3 exclusive 是否独占队列 false可以被其他连接使用
//参数4 autoDelete 是否消费完成后自动删除队列
//参数5 额外附加参数
channel.queueDeclare(“hello”,false,false,false,null);
//发布消息
//1.交换机名称 2.队列名称 3.环境消息的额外设置 4.消息的具体内容 已经投放到队列中了
channel.basicPublish("",“hello”,null,“hello rabbitMQ”.getBytes());
channel.close();
connection.close();
接收者的前面一致
后边是
//1.消息队列名称 2.是否自动确认消息 3.消费消息时的回调接口
channel1.basicConsume(“hello”,false,new DefaultConsumer(channel1){
@Override//消息回调
//1.body 用来取出的消息
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(“new string(body) =”+new String(body));
}
});
//如果不关闭 就会一直监听 就会进行消息的回调 取出全部消息 test不支持多线程
//一直监听 来一个消息就进行消费
channel1.close();
connection.close();
//发布消息
//1.交换机名称 2.队列名称 3.环境消息的额外设置(可以设置重启后队列里的消息还在 MessageProperties.PERSISTENT_TEXT_PLAIN) 4.消息的具体内容 已经投放到队列中了
channel.basicPublish("",“hello”, MessageProperties.PERSISTENT_TEXT_PLAIN,“hello rabbitMQ”.getBytes());

生产者队列与消费者队列的属性要一致 是否持久化 自动删除 是否独占等等不然会报错 自动删除是要等消费者进程彻底结束才会删除

===================================================

工作队列(任务队列)

当消息处理比较耗时,生产消息的速度快于消费消息的速度,堆积很多无法及时处理。此时就可以使用work模型

channel.basicQos(1); //每次只往消费者发送一条消息 收到消费者确认消息后在发送消息。
channel.basicAck(envelope.getDeliveryTag(),false); //告诉生产者已经消息接收完成。
channel.basicConsume的第二个参数设置为false为手动应答。True为自动回复。
公平分发必须改为手动确认消息。

在这里插入图片描述
两个customer 轮询 A1 3 5 7 9 B2 4 6 8 10 这样 平均分配
但是如果一个快 一个慢还是会消息积累 希望快的多做点就引入另外一种
//手动确认,参数1 手动确认消息标识,参数2 每次确认一个
channel.basicAck(envelope.getDeliveryTag(),false);
a Thread.sleep(1000);
a b开启a因为慢一些 使用b就会能者多劳效率最大化
a b都开启了channel.basicAck(envelope.getDeliveryTag(),false);

Fanout广播模型

一个消息能被消费者都拿到
比如调用订单系统和库存系统
在这里插入图片描述

1.可以有多个消费者
2.每个消费者有自己的队列quene
3.每个队列都要绑定到交换机exchange
4.生产者的消息只能发送给交换机,交换机来决定发给哪个队列,生产者无法决定
5.队列的消费者都能拿到消息,实现消息的复用

1.provider
public class provider {
public static void main(String args[]) throws IOException {
Connection conn = RabbitMQUtils.getConn();
Channel channel = conn.createChannel();
//将通道声明指定交换机
//参数1 交换机名称 参数2交换机的类型 fanout广播 不存在就创建
channel.exchangeDeclare(“logs”,“fanout”);
//发送消息 2.路由key
channel.basicPublish(“logs”,"", null,“fanout type msg”.getBytes());
//释放资源
RabbitMQUtils.closeConn(channel,conn);
}
}

2.customer 1 2 3同时接受消息
public class customer1 {
public static void main(String args[]) throws IOException {
Connection conn = RabbitMQUtils.getConn();
Channel channel = conn.createChannel();
//交换机绑定
channel.exchangeDeclare(“logs”,“fanout”);
//队列绑定 临时队列以及临时队列名称
String queue = channel.queueDeclare().getQueue();
//绑定交换机和队列 fanout里第三个参数路由key没有用
channel.queueBind(queue,“logs”,"");
//消费消息了 参数2.自动确认 3 回调函数
channel.basicConsume(queue,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(“消费者1:”+new String(body));
}
});
}


} 4.第四种路由Routing


在这里插入图片描述

1.Routing订阅模型之直连direct
在fanout模型中,消息会被广播到每一个消费者消费,但是我们有时候就希望一些消息能被一些消费者消费,这个时候就需要用到direct类型的交换机

provider
public static void main(String[] args) throws IOException {
Connection conn = RabbitMQUtils.getConn();
Channel channel = conn.createChannel();
String routingkey=“err”;
channel.exchangeDeclare(“exchange_dir”,“direct”);
channel.basicPublish(“exchange_dir”,routingkey,null,(“This is 路由key的方式”+routingkey).getBytes());
RabbitMQUtils.closeConn(channel,conn);
}


customer


public static void main(String[] args) throws IOException {
Connection conn = RabbitMQUtils.getConn();
Channel channel = conn.createChannel();
channel.exchangeDeclare(“exchange_dir”,“direct”);
String queue = channel.queueDeclare().getQueue();
//获取交换机里三种类型的消息
channel.queueBind(queue,“exchange_dir”,“info”);
channel.queueBind(queue,“exchange_dir”,“fatal”);
channel.queueBind(queue,“exchange_dir”,“err”);
channel.queueBind(queue,“exchange_dir”,“warning”);
channel.basicConsume(queue,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(“消费者2消费了:”+new String(body));
}
});
}

5.Topic路由

路由key带有通配符 * #
表示匹配一个
com.
* 匹配com下的所有单个
*.com 通配.com的所有
*.com. * 通配两个字符中间带com的
#表示通配0个或者多个
com.# 表示com.xxx.xx.xxx都可以
和direct类型交换机配置发送消费差不多 就是类型是topic 消费者的路由key是带通配符的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值