在SpringBoot中配置RabbitMQ
添加maven坐标:
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
配置application.properties,设置连接地址,端口,用户名,密码:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
实现发送和接收对象
定义一个简单的对象,要实现Serializable:
public class User implements Serializable {
private String id;
private String name;
private String address;
..省略构造函数 get set
}
在配置类中定义队列:
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitmqConfig {
//定义一个名为queue.a,持久化,非排他,非自动删除的队列
@Bean
public Queue queueA()
{
return new Queue("queue.a",true,false,false);
}
}
发送对象,使用RabbitTemplate:
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SenderService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendByRabbitTemplate()
{ //queue.a为之前定义的队列
rabbitTemplate.convertAndSend("queue.a",new User("swe111","jack","福建省"));
}
}
接收对象:
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
//接收来自队列queue.a
@Service
@RabbitListener(queues = {"queue.a"})
public class ReceiverService {
//接收消息处理函数
@RabbitHandler
public void receiver(User user)
{
System.out.println(user);
}
}
实现发送接收多个主题(topic)
topic交换器模式下根据routing key来确定要投递的队列,
routing key 为一个“.”号分隔的字符串,如com.rabbit.client。
routing key 中也会出现“*”和“#”,“*”匹配一个字母,“#”匹配多个字母
在配置类中配置队列和交换器:
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitmqConfig {
@Bean
public Queue queueA()
{
return new Queue("queue.a",true,false,false);
}
@Bean
public Queue queueB()
{
return new Queue("queue.b",true,false,false);
}
//定义topic交换器
@Bean
public TopicExchange topicExchange()
{
return new TopicExchange("topicExchange",true,false,null);
}
@Bean
public Binding queueBindingA(Queue queueA,TopicExchange topicExchange)
{ //将队列绑定至交换器,并指定routing key
return BindingBuilder.bind(queueA).to(topicExchange).with("queue.a");
}
@Bean
public Binding queueBindingB(Queue queueB,TopicExchange topicExchange)
{ //将队列绑定至交换器,并指定routing key
return BindingBuilder.bind(queueB).to(topicExchange).with("queue.#");
}
}
发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SenderService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendByRabbitTemplate()
{ //发送时要指定交换器和routing key,否则会将消息发送至默认交换器,routing key将不起作用
rabbitTemplate.convertAndSend("topicExchange","queue.a",
new User("swe111","zxy","福建省"));
}
}