1. 程式人生 > >(九) RabbitMQ實戰教程(面向Java開發人員)之SpringBoot整合RabbitMQ

(九) RabbitMQ實戰教程(面向Java開發人員)之SpringBoot整合RabbitMQ

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等

感言

至此本系列部落格完結,由於部落格篇幅有限所以無法面面俱到,也由於博主個人水平有限所以無法保證文章質量符合所有人要求在此希望大家多多包涵。本系列的每一篇文章都傾注了我的心血,每一個Demo我都用心測試,從0到1覆蓋了大部分RabbitMQ常見案例,如果您覺得本系列部落格幫助到了您,那就點個讚唄~,又或者您覺得這系列部落格寫的像屎一樣,我也希望您能留下寶貴的建議來幫助我成長,因為我認為就算是一坨屎它也會有進步的空間