安装
使用docker安装
镜像下载后安装
docker run -d --name rabbitmq -p 15672:15672 -p 5672:5672 1482b87815ec
这里15672是Web端口使用。
从Web端进入15672端口,默认账号密码都是guest。
start
搭建简单的生产者和消费者。
导入依赖
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.6.0</version>
</dependency>
生产者
package com.meng.simple;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Provider {
public static final String QUEUE_NAME = "simple_queue";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
//参数解释:队列名称、是否持久化、该频道是否独占连接、是否不使用时自动删除队列、队列其他参数
channel.queueDeclare(QUEUE_NAME,true,false,false,null);
String mess = "RabbitMq hello!";
channel.basicPublish("",QUEUE_NAME,null,mess.getBytes());
channel.close();
connection.close();
}
}
消费者
package com.meng.simple;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
/**
* @author liubin
* @create 2020-12-30
*/
public class Consumer {
public static final String QUEUE_NAME = "simple_queue";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
//参数解释:队列名称、是否持久化、该频道是否独占连接、是否不使用时自动删除队列、队列其他参数
channel.queueDeclare(QUEUE_NAME,true,false,false,null);
DeliverCallback deliverCallback = (consumerTag,delivery)->{
String mes = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(mes);
};
channel.basicConsume(QUEUE_NAME,true,deliverCallback,consumerTag->{});
}
}
先启动生产者,再启动消费者。
消费者会收到生产者发送的消息。
集成SpringBoot
先来看生成者。
新建一个maven工程,导入需要的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置yml文件(习惯使用yml)
server:
port: 8000
spring:
rabbitmq:
port: 5672
host: 127.0.0.1
username: guest
password: guest
virtual-host: /springmq
编写配置类
package com.meng.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SendConfig {
@Bean
public Queue queue(){
return new Queue("queue_spring");
}
}
发送消息的业务类,这里使用的是topic类型
package com.meng.service;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SendService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendTopic(String msg){
rabbitTemplate.convertAndSend("topics","product.save.add",msg);
System.out.println("topic,信息已发送");
}
}
然后看消费者。
另建maven项目,消费者的依赖、yml配置、配置类都一样,来看主要业务
package com.meng.listener;
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 ReceiveListener {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(autoDelete = "false",name = "topic_1"),
exchange = @Exchange(type = "topic",name = "topics"),
key = {"user.#"}
)
})
public void receiveTopicMsg1(String msg){
System.out.println("1:"+msg);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(autoDelete = "false",name = "topic_2"),
exchange = @Exchange(type = "topic",name = "topics"),
key = {"product.#"}
)
})
public void receiveTopicMsg2(String msg){
System.out.println("2:"+msg);
}
}
这里有两个监听,后者会监听到以product为前缀队列的消息。