如何整合RabbitMQ

1、添加spring-boot-starter-amqp

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-amqp</artifactId>
  4. </dependency>

2、添加配置

  1. spring.rabbitmq.host=localhost
  2. spring.rabbitmq.port=5672
  3. spring.rabbitmq.username=guest
  4. spring.rabbitmq.password=guest
  5. spring.rabbitmq.publisher-confirms=true
  6. spring.rabbitmq.dynamic=true
  7. spring.rabbitmq.cache.connection.mode=channel

3、注入队列

  1. @Configuration
  2. public class RabbitConfig {
  3. @Bean
  4. public Queue Queue() {
  5. return new Queue("hello");
  6. }
  7. }

4、创建操作数据的Repository对象

  1. interface CityRepository extends Repository<City, Long> {
  2. Page<City> findAll(Pageable pageable);
  3. Page<City> findByNameContainingAndCountryContainingAllIgnoringCase(String name,
  4. String country, Pageable pageable);
  5. City findByNameAndCountryAllIgnoringCase(String name, String country);
  6. }

5、创建消费者

  1. @Component
  2. public class RabbitConsumer {
  3. @RabbitHandler
  4. @RabbitListener(queues = "hello")
  5. public void process(@Payload String foo) {
  6. System.out.println(new Date() + ": " + foo);
  7. }
  8. }

6、启动主类

  1. @SpringBootApplication
  2. @EnableScheduling
  3. public class AmqpApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(AmqpApplication.class, args);
  6. }
  7. }

控制台输出:

  1. Sun Sep 30 16:30:35 CST 2018: hello

到此,一个简单的SpringBoot2.0集成RabbitMQ就完成了。
熟悉RabbitMQ的小伙伴们应该知道,RabbitMQ在一般的队列基础上,增加了ExChange的概念。ExChange有四种类型:Direct, Topic, Headers and Fanout。其中Headers实际很少使用,Direct较为简单。接下来将详细介绍如何使用topic和Fanout。

Topic Exchange

1、配置Topic规则

  1. @Configuration
  2. public class TopicRabbitConfig {
  3. @Bean
  4. public Queue queueMessage1() {
  5. return new Queue(MQConst.TOPIC_QUEUENAME1);
  6. }
  7. @Bean
  8. public Queue queueMessage2() {
  9. return new Queue(MQConst.TOPIC_QUEUENAME2);
  10. }
  11. @Bean
  12. TopicExchange exchange() {
  13. return new TopicExchange(MQConst.TOPIC_EXCHANGE);
  14. }
  15. @Bean
  16. Binding bindingExchangeMessage(Queue queueMessage1, TopicExchange exchange) {
  17. // 将队列1绑定到名为topicKey.A的routingKey
  18. return BindingBuilder.bind(queueMessage1).to(exchange).with(MQConst.TOPIC_KEY1);
  19. }
  20. @Bean
  21. Binding bindingExchangeMessages(Queue queueMessage2, TopicExchange exchange) {
  22. // 将队列2绑定到所有topicKey.开头的routingKey
  23. return BindingBuilder.bind(queueMessage2).to(exchange).with(MQConst.TOPIC_KEYS);
  24. }
  25. }

2、配置消费者

  1. @Component
  2. public class TopicConsumer {
  3. @RabbitListener(queues = MQConst.TOPIC_QUEUENAME1)
  4. @RabbitHandler
  5. public void process1(String message) {
  6. System.out.println("queue:topic.message1,message:" + message);
  7. }
  8. @RabbitListener(queues = MQConst.TOPIC_QUEUENAME2)
  9. @RabbitHandler
  10. public void process2(String message) {
  11. System.out.println("queue:topic.message2,message:" + message);
  12. }
  13. }

3、生产消息

在Producer类中添加:

  1. // Topic
  2. rabbitTemplate.convertAndSend(MQConst.TOPIC_EXCHANGE, MQConst.TOPIC_KEYS, "from keys");
  3. rabbitTemplate.convertAndSend(MQConst.TOPIC_EXCHANGE, MQConst.TOPIC_KEY1, "from key1");

再次启动主类,控制台输出:

  1. queue:topic.message2,message:from keys
  2. queue:topic.message1,message:from key1
  3. queue:topic.message2,message:from key1

Fanout Exchange

1、配置Fanout规则

  1. @Configuration
  2. public class FanoutRabbitConfig {
  3. @Bean
  4. public Queue MessageA() {
  5. return new Queue(MQConst.FANOUT_QUEUENAME1);
  6. }
  7. @Bean
  8. public Queue MessageB() {
  9. return new Queue(MQConst.FANOUT_QUEUENAME2);
  10. }
  11. @Bean
  12. FanoutExchange fanoutExchange() {
  13. return new FanoutExchange(MQConst.FANOUT_EXCHANGE);
  14. }
  15. @Bean
  16. Binding bindingExchangeA(Queue MessageA, FanoutExchange fanoutExchange) {
  17. return BindingBuilder.bind(MessageA).to(fanoutExchange);
  18. }
  19. @Bean
  20. Binding bindingExchangeB(Queue MessageB, FanoutExchange fanoutExchange) {
  21. return BindingBuilder.bind(MessageB).to(fanoutExchange);
  22. }
  23. }

2.配置消费者

  1. @Component
  2. public class FanoutConsumer {
  3. @RabbitListener(queues = MQConst.FANOUT_QUEUENAME1)
  4. @RabbitHandler
  5. public void process1(String message) {
  6. System.out.println("queue:fanout.message1,message:" + message);
  7. }
  8. @RabbitListener(queues = MQConst.FANOUT_QUEUENAME2)
  9. @RabbitHandler
  10. public void process2(String message) {
  11. System.out.println("queue:fanout.message2,message:" + message);
  12. }
  13. }

3、生产消息

在Producer类中添加:

  1. // FanOut
  2. rabbitTemplate.convertAndSend(MQConst.FANOUT_EXCHANGE, "", "fanout");

再次启动主类,控制台输出:

  1. queue:fanout.message2,message:fanout
  2. queue:fanout.message1,message:fanout

源码地址:GitHub