1.rabbitMq的简介
rabbitmq 是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。
2.rabbitMq的特点
可靠性,高可用,多语言客户端,灵活的路由,插件机制,管理页面,消息集群,多种协议,跟踪机制
3.rabbitMq的安装
安装方式有很多种,本文采取的是使用docker的方式进行安装rabbitMQ,rabbitmq官网也有对应的docker安装,详细操作说明请查阅rabbitmq官网:https://www.rabbitmq.com/download.html
本文拉取的是3.11的版本,因此执行的docker命令应该更改为
docker run --privileged=true -it --name rabbitmq -p 5672:5672 -p 15672:15672 -d rabbitmq:3.11-management
注意:执行官网给的docker命令会报Failed to create thread: Operation not permitted (1),出现此错误加上--privileged=true即可
执行完之后,如果一切顺利的话,我们在浏览器访问IP:15672就会看到rabbitmq的管理页面,默认的账号密码是guest/guest
4.java整合rabbitMq
1.创建一个maven项目
2.在pom.xml引入对应的依赖
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.16.0</version> </dependency>
3.创建productService生产者的示例代码
public class ProductService {
/**
* 生产者发送消息
* @throws Exception
*/
public void sendMessage(String message) throws Exception{
//创建rabbit的连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setPassword("guest");
connectionFactory.setUsername("guest");
connectionFactory.setHost("192.168.79.129");
connectionFactory.setPort(5672);
//获取connection
Connection connection = connectionFactory.newConnection();
//获取channel
Channel channel = connection.createChannel();
//创建交换器
String exchanelName="my-test-rabbitMq";
// 参数1:交换机的名称,参数2:交换机的类型, 参数3:是否持久化
channel.exchangeDeclare(exchanelName,"direct",true);
String key="hello";
//发送消息
System.out.println("生产者发送的消息为:"+message);
channel.basicPublish(exchanelName,key,null,message.getBytes());
}
public static void main(String[] args) throws Exception{
ProductService productService=new ProductService();
System.out.println("生产者启动~~~~~");
productService.sendMessage("hello,RabbitMq");
}
}
4.创建consumerService消费者的示例代码
public class ConsumerService {
/**
* 消费者消费消息
*/
public void consumerMessage() throws Exception{
System.out.println("消费者启动~~~~~");
//创建rabbit的连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setPassword("guest");
connectionFactory.setUsername("guest");
connectionFactory.setHost("192.168.79.129");
connectionFactory.setPort(5672);
//获取connection
Connection connection = connectionFactory.newConnection();
//获取channel
final Channel channel = connection.createChannel();
//创建交换器
String exchanelName="my-test-rabbitMq";
// 参数1:交换机的名称,参数2:交换机的类型, 参数3:是否持久化
channel.exchangeDeclare(exchanelName,"direct",true);
String key="hello";
//声明队列
String queueName = channel.queueDeclare().getQueue();
//绑定队列
channel.queueBind(queueName,exchanelName,key);
while(true){
boolean autoAck = false;
String consumerTag = "";
channel.basicConsume(queueName, autoAck, consumerTag,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String routingKey = envelope.getRoutingKey();
String contentType = properties.getContentType();
System.out.println("消费的路由键:" + routingKey);
System.out.println("交换机类型:" + contentType);
long deliveryTag = envelope.getDeliveryTag();
channel.basicAck(deliveryTag, false);
System.out.println("消息内容:");
String bodyStr = new String(body, "UTF-8");
System.out.println(bodyStr);
}
});
}
}
public static void main(String[] args) throws Exception{
ConsumerService consumerService=new ConsumerService();
consumerService.consumerMessage();
}
}
1.先启动消费者程序,监听对应的队列是否有消息投递
2.生产者启动往队列投递消息,
3.消费者查看控制台是否消费到消息
5.总结
本文只是简单的介绍一下rabbitMq的特点,简介,安装以及如何使用java整合rabbitmq的api,简单入个门,从下篇文章开始将隆重介绍一下rabbitmq的交换机类型,消息模型,以及它的基本概念。