1. 程式人生 > >RabbitMQ教程之SpringBoot整合RabbitMQ(Java)

RabbitMQ教程之SpringBoot整合RabbitMQ(Java)

SpringBoot整合RabbitMQ

使用SpringBoot整合RabbitMQ非常簡單,它極大程度的簡化了開發成本,使用SpringBoot整合RabbitMQ需匯入如下依賴

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>

<dependencies>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
</dependencies>

1.建立配置檔案 application.yml

spring:
   rabbitmq:
    virtual-host: /
    username: roberto
    password: roberto
    addresses: 192.168.56.128:5672

2.建立訊息生產者配置類

@Configuration
public class RabbitMQProducerConfig {
    @Bean
    public Exchange exchange() {
        return new DirectExchange("roberto.order", true, false, new HashMap<>());
    }
}

3.建立訊息生產者啟動類

@SpringBootApplication
public class ProducerApplication {
    private static RabbitTemplate rabbitTemplate;

    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        ProducerApplication.rabbitTemplate = rabbitTemplate;
    }

    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class);
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        messageProperties.setContentType("UTF-8");
        Message message = new Message("訂單資訊".getBytes(), messageProperties);
        rabbitTemplate.send("roberto.order", "add", message);
    }
}

4.建立訊息消費者配置類

@Configuration
public class RabbitMQConsumerConfig {
    @Bean
    public Exchange exchange() {
        return new DirectExchange("roberto.order", true, false, new HashMap<>());
    }

    @Bean
    public Queue queue() {
        return new Queue("roberto.order.add", true, false, false, new HashMap<>());
    }

    @Bean
    public Binding binding() {
        return new Binding("roberto.order.add", Binding.DestinationType.QUEUE, "roberto.order", "add", new HashMap<>());
    }

    @Bean
    public MessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer messageListenerContainer = new SimpleMessageListenerContainer();
        messageListenerContainer.setConnectionFactory(connectionFactory);
        messageListenerContainer.setQueueNames("roberto.order.add");

        messageListenerContainer.setConcurrentConsumers(5);
        messageListenerContainer.setMaxConcurrentConsumers(10);

        Map<String, Object> argumentMap = new HashMap();
        messageListenerContainer.setConsumerArguments(argumentMap);
        messageListenerContainer.setConsumerTagStrategy(new ConsumerTagStrategy() {
            @Override
            public String createConsumerTag(String s) {
                return "RGP訂單系統ADD處理邏輯消費者";
            }
        });

        messageListenerContainer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                try {
                    System.out.println(new String(message.getBody(), "UTF-8"));
                    System.out.println(message.getMessageProperties());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return messageListenerContainer;
    }
}

5.建立訊息消費者啟動類

@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class);
    }
}

6.依次啟動訊息消費者訊息生產者,訊息消費者控制檯輸出如下

訂單資訊
MessageProperties [headers={}, contentType=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=roberto.order, receivedRoutingKey=add, deliveryTag=1, consumerTag=RGP訂單系統ADD處理邏輯消費者, consumerQueue=roberto.order.add]


我們發現當使用SpringBoot整合RabbitMQ時,我們不再需要管理RabbitAdmin,RabbitTemplate等