网站建设捌金手指下拉七免费涨粉工具
目录
- 1.RabbitMQ简介
- 2.添加依赖
- 3.配置RabbitMQ连接
- 4.DirectExchange
- 4.1 消费者
- 4.2 生产者
- 4.3 测试
- 4.4 一个交换机对多个队列
- 4.5 一个队列对多个消费者
- 5.FanoutExchange
- 5.1 消费者
- 5.2 生产者
- 5.3 测试
- 6.TopicExchange
- 6.1 消费者
- 6.2 生产者
1.RabbitMQ简介
RabbitMQ是一个由Erlang语言编写的消息中间件,它遵循AMQP协议,提供了稳定可靠的消息传输服务。RabbitMQ通过其独特的架构和丰富的功能,帮助开发者解决分布式系统中的消息传递问题,提高系统的可扩展性、可靠性和响应速度。
2.添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>
3.配置RabbitMQ连接
在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=/
4.DirectExchange
4.1 消费者
@Configuration
public class DirectConsumer {//注册一个队列@Bean //启动多次为什么不报错?启动的时候,它会根据这个名称Direct_Q01先去查找有没有这个队列,如果有什么都不做,如果没有创建一个新的public Queue directQueue(){return QueueBuilder.durable("Direct_Q01").maxLength(100).build();}//注册交换机@Beanpublic DirectExchange directExchange(){//1.启动的时候,它会根据这个名称Direct_E01先去查找有没有这个交换机,如果有什么都不做,如果没有创建一个新的return ExchangeBuilder.directExchange("Direct_E01").build();}//绑定交换机与队列关系@Beanpublic 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);}
}
4.2 生产者
//放入Ioc容器
@Service
public class DirectProvider {@Resource private RabbitTemplate rabbitTemplate;//发送消息public void send(String message) {rabbitTemplate.convertAndSend("Direct_E01", "RK01", message);}
}
4.3 测试
@SpringBootTest(classes = App.class)
public class TestDirect {@Resourceprivate DirectProvider directProvider;@Testpublic void directSendTest(){for (int i = 0; i < 10; i++) {directProvider.send("我嫩爹");}}
}
4.4 一个交换机对多个队列
4.5 一个队列对多个消费者
5.FanoutExchange
5.1 消费者
@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();}//注册交换机@Beanpublic FanoutExchange fanoutExchange(){return ExchangeBuilder.fanoutExchange("Fanout_E01").build();}//绑定交换机与队列关系@Beanpublic Binding fanoutBinding(Queue fanoutQueue,FanoutExchange fanoutExchange){return BindingBuilder.bind(fanoutQueue).to(fanoutExchange);}@Beanpublic 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);}}
5.2 生产者
@Service
public class FanoutProvider {@Resourceprivate RabbitTemplate rabbitTemplate;public void send(JSONObject message) {rabbitTemplate.convertAndSend("Fanout_E01","",message.get("msg"));}
}
5.3 测试
发送请求进行测试
@RestController
@RequestMapping("/fanout")
public class FanoutController {@Resourceprivate 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>
6.TopicExchange
6.1 消费者
@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();}//注册交换机@Beanpublic TopicExchange topicExchange(){return ExchangeBuilder.topicExchange("Topic_E01").build();}//绑定交换机与队列关系@Beanpublic Binding topicBinding(Queue topicQueue,TopicExchange topicExchange){return BindingBuilder.bind(topicQueue).to(topicExchange).with("#");}@Beanpublic 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);}}
6.2 生产者
@Service
public class TopicProvider {@Resourceprivate RabbitTemplate rabbitTemplate;public void send(JSONObject message) {rabbitTemplate.convertAndSend("Topic_E01",message.get("routingKey").toString(),message.get("msg"));}
}