rabbitmq
目录
官网:Messaging that just works — RabbitMQ
mq的优势
-
应用解耦
-
异步提速
-
削峰填谷
mq的劣势
-
降低系统的可用性
系统引入的外部依赖越多,系统稳定性越差,一旦mq宕机,整个系统就会瘫痪
-
系统的复杂度提高
保证消息的同步消息、避免重复消费、消息丢失等一系列问题
-
一致性问题
如何保证多个服务写操作一致
rabbitmq基于AMQP协议
AMQP,即Advanced Message Queuing Protocol(高级消息队列协议),是一个网络协议,是应用层协议的一个开放标准,为面向消息的中间设计。
官网给的六种模式实现

1、用到的工具类
RabbitConstant.class和RabbitUtils.class
public class RabbitConstant {
public static final String QUEUE_HELLOWORD = "helloworld";
public static final String QUEUE_SMS = "queuesms";
public static final String EXCHANGE_WEATHER = "weather";
public static final String EXCHANGE_WEATHER_ROUTING = "weather_routing";
public static final String EXCHANGE_WEATHER_TOPIC = "weather_topic";
public static final String QUEUE_BAIDU = "baidu";
public static final String QUEUE_SINA = "sina";
}
package com.liheng.rabbitmq.util;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* @Author liheng
* @Date 2021/10/20 下午2:20
* @Version 1.0
*/
public class RabbitUtils {
private static ConnectionFactory connectionFactory = new ConnectionFactory();
static {
connectionFactory.setHost("xxx.xxx.xx.xx");//主机地址
connectionFactory.setPort(5672);
connectionFactory.setUsername("liheng123");
connectionFactory.setPassword("liheng123");
connectionFactory.setVirtualHost("liheng123Virtual");
}
public static Connection getConnection(){
Connection connection = null;
try {
connection = connectionFactory.newConnection();
return connection;
} catch (Exception e) {
throw new RuntimeException();
}
}
}
2、第一种:"helloworld"实现
Consumer.class 消费者代码
package com.liheng.rabbitmq.helloword;
import com.liheng.rabbitmq.util.RabbitConstant;
import com.liheng.rabbitmq.util.RabbitUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @Author liheng
* @Date 2021/10/20 上午11:53
* @Version 1.0
*/
public class Consumer {
public static void main(String[] args) {
//获取tcp长链接
try {
Connection connection = RabbitUtils.getConnection();
//创建通信 通道
Channel channel = connection.createChannel();
/**
* @param queue the name of the queue
* @param durable true if we are declaring a durable queue (the queue will survive a server restart)
* @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
* @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
* @param arguments other properties (construction arguments) for the queue
参数1:队列名称
参数2:是否持久化 false不持久化 mq停掉就会丢失数据
参数3:是否私有化,false表示所有消费这都可以消费,true表示只有第一次拥有它的消费这才可以使用
参数4:是否自动删除,false表示连接停掉之后不自动删除这个队列
参数5:其他额外参数
*/
channel.queueDeclare(RabbitConstant.QUEUE_HELLOWORD,false,false,false,null);
//从mq中去消费
channel.basicConsume(RabbitConstant.QUEUE_HELLOWORD,false,new Reciver(channel));
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Reciver extends DefaultConsumer{
private Channel channel;
public Reciver(Channel channel) {
super(channel);
this.channel = channel;
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body);
System.out.println("接受到的消息:"+ message);
System.out.println("消息的tagId:"+ envelope.getDeliveryTag());
//false 代表只前后当前的消息,true代表签收所有未签收的消息
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
Producer.class 消费者
package com.liheng.rabbitmq.helloword;
import com.liheng.rabbitmq.util.RabbitConstant;
import com.liheng.rabbitmq.util.RabbitUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @Author liheng
* @Date 2021/10/20 上午11:37
* @Version 1.0
*/
public class Producer {
public static void main(String[] args) {
//获取tcp长链接
try {
Connection connection = RabbitUtils.getConnection();
//创建通信 通道
Channel channel = connection.createChannel();
//RabbitConstant.QUEUE_HELLOWORD 如果没有当前队列 会自动生成队列
/**
* @param queue the name of the queue
* @param durable true if we are declaring a durable queue (the queue will survive a server restart)
* @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
* @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
* @param arguments other properties (construction arguments) for the queue
参数1:队列名称
参数2:是否持久化 false不持久化 mq停掉就会丢失数据
参数3:是否私有化,false表示所有消费这都可以消费,true表示只有第一次拥有它的消费这才可以使用
参数4:是否自动删除,false表示连接停掉之后不自动删除这个队列
参数5:其他额外参数
*/
channel.queueDeclare(RabbitConstant.QUEUE_HELLOWORD,false,false,false,null);
String message = "李恒666";
/**
* @param exchange the exchange to publish the message to
* @param routingKey the routing key
* @param mandatory true if the 'mandatory' flag is to be set
* @param props other properties for the message - routing headers etc
* @param body the message body
参数1:exchange名称 在这个简单模式下不需要指定
参数2:队列名称
参数3:其他的一些属性
参数4:消息的字节数组
*/
channel.basicPublish("",RabbitConstant.QUEUE_HELLOWORD,null,message.getBytes());
channel.close();
connection.close();
System.out.println("数据发送成功!");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
本文介绍了RabbitMQ的消息队列优势,如应用解耦、异步处理和削峰填谷,同时也指出其可能带来的系统复杂性和可用性降低的问题。文章详细讲解了RabbitMQ基于AMQP协议,并通过一个简单的'Helloworld'实例展示了消费者和生产者的实现,包括队列声明和消息的发送与接收。
1222

被折叠的 条评论
为什么被折叠?



