赞
踩
RabbitMQ是一个由Erlang语言编写的消息中间件,它遵循AMQP协议,提供了稳定可靠的消息传输服务。RabbitMQ通过其独特的架构和丰富的功能,帮助开发者解决分布式系统中的消息传递问题,提高系统的可扩展性、可靠性和响应速度。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
在application.properties
或application.yml
中配置RabbitMQ服务器的连接参数:
# 定义RabbitMQ的主机地址,这里使用的是局域网内的一个IP地址
spring.rabbitmq.host=192.168.131.130
# 指定RabbitMQ的端口号,默认情况下RabbitMQ使用5672端口
spring.rabbitmq.port=5672
# 设置RabbitMQ的用户名,这里使用的是默认的用户名guest
spring.rabbitmq.username=guest
# 设置RabbitMQ的密码,这里使用的是默认的密码guest
spring.rabbitmq.password=guest
# 配置RabbitMQ的虚拟主机,这里使用的是默认的虚拟主机"/"
spring.rabbitmq.virtual-host=/
@Configuration public class DirectConsumer { //注册一个队列 @Bean //启动多次为什么不报错?启动的时候,它会根据这个名称Direct_Q01先去查找有没有这个队列,如果有什么都不做,如果没有创建一个新的 public Queue directQueue(){ return QueueBuilder.durable("Direct_Q01").maxLength(100).build(); } //注册交换机 @Bean public DirectExchange directExchange(){ //1.启动的时候,它会根据这个名称Direct_E01先去查找有没有这个交换机,如果有什么都不做,如果没有创建一个新的 return ExchangeBuilder.directExchange("Direct_E01").build(); } //绑定交换机与队列关系 @Bean public Binding directBinding(Queue directQueue,DirectExchange directExchange){ return BindingBuilder.bind(directQueue).to(directExchange).with("RK01"); } //启动一个消费者 @RabbitListener(queues = "Direct_Q01") public void receiveMessage(String msg){ System.out.println("Direct_Q01收到消息:"+msg); } }
//放入Ioc容器
@Service
public class DirectProvider {
@Resource
private RabbitTemplate rabbitTemplate;
//发送消息
public void send(String message) {
rabbitTemplate.convertAndSend("Direct_E01", "RK01", message);
}
}
@SpringBootTest(classes = App.class)
public class TestDirect {
@Resource
private DirectProvider directProvider;
@Test
public void directSendTest(){
for (int i = 0; i < 10; i++) {
directProvider.send("我嫩爹");
}
}
}
@Configuration public class FanoutConsumer { //注册一个队列 @Bean public Queue fanoutQueue(){ return QueueBuilder.durable("Fanout_Q01").maxLength(100).build(); } @Bean public Queue fanoutQueue2(){ return QueueBuilder.durable("Fanout_Q02").maxLength(100).build(); } //注册交换机 @Bean public FanoutExchange fanoutExchange(){ return ExchangeBuilder.fanoutExchange("Fanout_E01").build(); } //绑定交换机与队列关系 @Bean public Binding fanoutBinding(Queue fanoutQueue,FanoutExchange fanoutExchange){ return BindingBuilder.bind(fanoutQueue).to(fanoutExchange); } @Bean public Binding fanoutBinding2(Queue fanoutQueue2,FanoutExchange fanoutExchange){ return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange); } //启动一个消费者 @RabbitListener(queues = "Fanout_Q01") public void receiveMessage(String msg){ System.out.println("Fanout_Q01收到消息:"+msg); } //启动一个消费者 @RabbitListener(queues = "Fanout_Q02") public void receiveMessage2(String msg){ System.out.println("Fanout_Q02收到消息:"+msg); } }
@Service
public class FanoutProvider {
@Resource
private RabbitTemplate rabbitTemplate;
public void send(JSONObject message) {
rabbitTemplate.convertAndSend("Fanout_E01","",message.get("msg"));
}
}
发送请求进行测试
@RestController
@RequestMapping("/fanout")
public class FanoutController {
@Resource
private FanoutProvider fanoutProvider;
@PostMapping("/send")
public void send(@RequestBody JSONObject message) {
fanoutProvider.send(message);
}
}
额外涉及到的一些依赖:
<!-- 封装了一些工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!-- 之前web请求相关注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@Configuration public class TopicConsumer { //注册一个队列 @Bean public Queue topicQueue(){ return QueueBuilder.durable("Topic_Q01").maxLength(100).build(); } @Bean public Queue topicQueue2(){ return QueueBuilder.durable("Topic_Q02").maxLength(100).build(); } //注册交换机 @Bean public TopicExchange topicExchange(){ return ExchangeBuilder.topicExchange("Topic_E01").build(); } //绑定交换机与队列关系 @Bean public Binding topicBinding(Queue topicQueue,TopicExchange topicExchange){ return BindingBuilder.bind(topicQueue).to(topicExchange).with("#"); } @Bean public Binding topicBinding2(Queue topicQueue2,TopicExchange topicExchange){ return BindingBuilder.bind(topicQueue2).to(topicExchange).with("1.8.*"); } //启动一个消费者 @RabbitListener(queues = "Topic_Q01") public void receiveMessage(String msg){ System.out.println("Topic_Q01收到消息:"+msg); } //启动一个消费者 @RabbitListener(queues = "Topic_Q02") public void receiveMessage2(String msg){ System.out.println("Topic_Q02收到消息:"+msg); } }
@Service
public class TopicProvider {
@Resource
private RabbitTemplate rabbitTemplate;
public void send(JSONObject message) {
rabbitTemplate.convertAndSend("Topic_E01",message.get("routingKey").toString(),message.get("msg"));
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。