1.1 什么是消息中间件?
消息队列中间件 (Message Queue Middleware ,简称为 MQ) 是指利用高效可靠的消息传递 机制进行与平台无关的数据交流,并基于数据通信来进行分布式系统的集成。通过提供消息传递和消息排队模型,它可以在分布式环境下扩展进程间的通信。
个人理解:就是将消息放在第三方服务器,然后再处理
1.2消息中间件的作用?
异步解耦:适用于不想立刻处理的任务可以放在消息队列里慢慢处理,比如注册需要绑定邮箱,可以放队列里慢慢处理
顺序保证:可以保证任务执行有序
削峰:某些情况下流量剧增,加资源太浪费,可以放入队列,防止请求过大,而崩溃
可恢复性:平台崩溃,消息不会丢失
冗余〈存储):有些情况数据处理过程失败,可以使用消息中间件保证数据持久化完全被处理,防止数据丢失
1.3RabbitMq的定义
RabbitMQ 是采用Erlang 语言实现 AMQP (Advanced Message Queuing Protocol ,高级消息 队列协议)的消息中间件
1.4RabbitMq的安装
首先安装erlang,然后再安装RabbitMq
注意:默认安装会有一个guest账号,这个只能本地使用,测试需要再创建新用户,配置权限等
1.5RabbitMq小demo
构建一个maven项目
导入依赖
<! -- https : //mvnrepository . com/artifact/com. rabbitmq/amqp-client -->
<dependency>
<groupld>com.rabbitmq</groupld>
<artifactld>amqp-client</artifactld>
<version>4.2.1</version>
</dependency>
package rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import java.util.concurrent.TimeUnit;
//生产者代码
public class RabbitProducer {
public static final String EXCHANGE_NAME = "exchange_demo";//交换机名称
public static final String ROUTING_KEY = "routingkey_demo";//路由名称
public static final String QUEUE_NAME = "queue_demo";//队列名称
public static final String IP_ADDRESS = "127.0.0.1";
public static final int PORT = 5672;
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
//factory.setVirtualHost("/");
factory.setHost(IP_ADDRESS);
factory.setPort(PORT);
factory.setUsername("admin");
factory.setPassword("admin");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 创建一个 type="direct" 、持久化的、非自动删除的交换器
channel.exchangeDeclare(EXCHANGE_NAME,"direct",true,false,null);
//创建一个持久化、非排他的、非自动删除的队列
channel.queueDeclare(QUEUE_NAME,true,false,false,null);
//将交换机与队列通过路由键绑定
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,ROUTING_KEY);
//发送一条持久化消息hello world
String message = "hello world";
channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY,
MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());
channel.close();
connection.close();
}
}
package rabbitmq;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Address;
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 java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
//消费者代码
public class RabbitConsumer {
public static final String QUEUE_NAME = "queue_demo";
public static final String IP_ADDRESS = "127.0.0.1";
public static final int PORT = 5672;
public static void main(String[] args) throws Exception {
Address[] addresses = new Address[]{new Address(IP_ADDRESS,PORT)};
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("admin");
factory.setPassword("admin");
Connection connection = factory.newConnection(addresses);//创建连接
Channel channel = connection.createChannel();//创建信道
channel.basicQos(64);// 设置客户端最多接收未被 ack 的消息的个数
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try{
TimeUnit.SECONDS.sleep(1);
}catch(Exception e) {
e.printStackTrace();
}
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(QUEUE_NAME,consumer);
//等待回调函数执行完毕之后 关闭资源
TimeUnit.SECONDS.sleep(5);
channel.close();
connection.close();
}
}