在讲这篇文章之前,是默认大家对Springboot已经有一定的了解了。
上篇文章我们讲的是RabbitMQ的手动实现,比较繁琐冗余。所以Spring帮我们做了封装,SpringAMQP。
步骤:
- 导依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
- 在配置文件中进行基本的配置
spring:
rabbitmq:
username: songdiao
password: sd460429
host:
port: 5672
virtual-host: /
- 在SpringBoot的主类上面添加@EnableRabbit注解来开启RabbitMQ
- 根据各种消息队列模型来编写Publisher和Consumer
简单队列模型
@SpringBootTest
public class Publisher {
//注入操作RabbitMq的对象
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
//调用convertAndSend方法来发送消息
rabbitTemplate.convertAndSend("queueTest4","Running...");
}
}
@Component("test1")
//该注解表示队列
@RabbitListener(queuesToDeclare = {@Queue(value = "queueTest4")})//默认为持久化,非独占,非自动删除
public class Consumer {
@RabbitHandler//该注解表示消费者
public void test(String msg){//该方法返回值必须为void,且参数类型需要和传过来的消息类型保持一致
System.out.println("收到消息:"+msg);
}
}
Work模型(平均分配)
@SpringBootTest
public class Publisher {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("workAnnotation","work is running...");
}
}
}
@Component("test2")
public class Consumer {
@RabbitListener(queuesToDeclare = {@Queue("workAnnotation")})
public void test1(String msg)throws Exception{
Thread.sleep(1000);
System.out.println("消费者1消费消息:"+msg);
}
@RabbitListener(queuesToDeclare = {@Queue("workAnnotation")})
public void test2(String msg){
System.out.println("消费者2消费消息:"+msg);
}
}
Fanout广播模型
@SpringBootTest
public class Publisher {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
rabbitTemplate.convertAndSend("fanoutExchange","","fanout is running...");//交换机名称,routingKey,具体消息内容
}
}
@Component("test3")
public class Consumer {
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,//创建队列,如果不传队列名称,就是创建一个临时队列
exchange = @Exchange(value = "fanoutExchange",type = "fanout"))//指定交换机名称和类型
})
public void test1(String msg){
System.out.println("消费者1消费消息:"+msg);
}
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,//创建队列,如果不传队列名称,就是创建一个临时队列
exchange = @Exchange(value = "fanoutExchange",type = "fanout"))//指定交换机名称和类型
})
public void test2(String msg){
System.out.println("消费者2消费消息:"+msg);
}
}
Direct路由模型
@SpringBootTest
public class Publisher {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
rabbitTemplate.convertAndSend("directExchange","test1","direct is running...");
}
}
@Component("test4")
public class Consumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "directExchange",type = "direct"),//这儿类型可以不写,因为默认就是direct模式
key = {"test1","test2","test3"})})//指定routingKey
public void test1(String msg){
System.out.println("消费者1消费消息:"+msg);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "directExchange",type = "direct"),
key = {"test1"})})
public void test2(String msg){
System.out.println("消费者2消费消息:"+msg);
}
}
Topic动态路由模型
@SpringBootTest
public class Publisher {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
rabbitTemplate.convertAndSend("topicExchange","test.a","topic is running...");
}
}
@Component
public class Consumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "topicExchange",type = "topic"),
key = {"test.*","*.test.#"}
)
})
public void test1(String msg){
System.out.println("消费者1消费消息:"+msg);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "topicExchange",type = "topic"),
key = {"test.user.#","*.test.#"}
)
})
public void test2(String msg){
System.out.println("消费者2消费消息:"+msg);
}
}
实战:
上面我们传递的消息都是String类型,那如果是Object类型呢,也就是说我们可以发送任意对象类型的消息,SpringAMQP会帮我们序列化为字节后发送。但是如果我们直接接收的话就会报错SimpleMessageConverter only supports String, byte[] and Serializable payloads。
解决办法为定义一个消息转换器,Spring的对消息对象的处理是由org.springframework.amqp.support.converter.MessageConverter来处理的。而默认实现是SimpleMessageConverter,基于JDK的ObjectOutputStream完成序列化。如果要修改只需要定义一个MessageConverter 类型的Bean即可。推荐用JSON方式序列化。
@Data
public class User {
private int id;
private String username;
private String password;
}
@Configuration
public class Converter {
@Bean
public MessageConverter messageConverter(){//MessageConverter注意是amqp包下的
return new Jackson2JsonMessageConverter();
}
}
@SpringBootTest
public class Publisher {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
User user = new User();
user.setId(1);
user.setUsername("test");
user.setPassword("test");
rabbitTemplate.convertAndSend("userExchange","",user);
}
}
@Component
public class Consumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "userExchange",type = "fanout")
)
})
public void test1(User user){
System.out.println("消费者1拿到消息:"+user);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "userExchange",type = "fanout")
)
})
public void test2(User user){
System.out.println("消费者2拿到消息:"+user);
}
}