Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ

本文,讲解 Spring Boot 如何集成 RabbitMQ,实现消息队列。javascript

博客地址:blog.720ui.com/java

什么是 RabitMQ

RabbitMQ 是一个在 AMQP 基础上完整的,可复用的企业消息系统。git

关于 RabbitMQ 的使用,能够阅读以前的 RabbitMQ 实战教程。github

Spring Boot 整合 RabbitMQ

Spring Boot 整合 RabbitMQ 是很是容易,只须要两个步骤。spring

首先,在 pom.xml 中增长 RabbitMQ 依赖。springboot

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>复制代码

第二步,在 src/main/resources/application.properties 中配置信息。微信

#rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest复制代码

实战演练

一个简单的实战开始

咱们来实现一个简单的发送、接收消息。app

Configuration

在 Spring Boot 中使用 @Bean 注册一个队列。spring-boot

@Configuration
public class RabbitMQConfig {
    public static final String QUEUE_NAME = "spring-boot-simple";
    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME);
    }
}复制代码

消息生产者

建立消息生产者 Sender。经过注入 AmqpTemplate 接口的实例来实现消息的发送。单元测试

@Service
public class Sender {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    public void send() {
        System.out.println("梁桂钊 发送消息...");
        rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, "你好, 梁桂钊!");
    }
}复制代码

消息消费者

建立消息消费者 Receiver。经过 @RabbitListener 注解定义对队列的监听。

@Service
public class Receiver {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    @RabbitListener(queues = "spring-boot-simple")
    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
    }
}复制代码

运行

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.lianggzone.springboot" })
public class RunMain {
    public static void main(String[] args) {
        SpringApplication.run(RunMain.class, args);
    }
}复制代码

单元测试

建立单元测试用例

public class RabbitMQTest {
    @Autowired
    private Sender sender;
    @Test
    public void send() throws Exception {
        sender.send();
    }
}复制代码

路由的实战演练

通过上面的实战案例,咱们对 Spring Boot 整合 RabbitMQ 有了必定的了解。如今,咱们再来看下 RabbitMQ 路由场景。

Configuration

在 RabbitMQConfig 中,咱们注册 队列,转发器,监听等。

@Configuration
public class RabbitMQConfig2 {

    public static final String QUEUE_NAME = "spring-boot";
    public static final String QUEUE_EXCHANGE_NAME = "spring-boot-exchange";

    @Bean
    public Queue queue() {
        // 是否持久化
        boolean durable = true; 
        // 仅建立者能够使用的私有队列,断开后自动删除
        boolean exclusive = false; 
        // 当全部消费客户端链接断开后,是否自动删除队列
        boolean autoDelete = false; 
        return new Queue(QUEUE_NAME, durable, exclusive, autoDelete);
    }

    @Bean
    public TopicExchange exchange() {
        // 是否持久化
        boolean durable = true;
        // 当全部消费客户端链接断开后,是否自动删除队列
        boolean autoDelete = false;
        return new TopicExchange(QUEUE_EXCHANGE_NAME, durable, autoDelete);
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(QUEUE_NAME);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }
}复制代码

消息生产者

建立消息生产者 Sender。经过注入 AmqpTemplate 接口的实例来实现消息的发送。

@Service
public class Sender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        System.out.println("梁桂钊 发送消息...");
        rabbitTemplate.convertAndSend(RabbitMQConfig2.QUEUE_NAME, "你好, 梁桂钊!");
    } 
}复制代码

消息消费者

建立消息消费者 Receiver。经过 @RabbitListener 注解定义对队列的监听。

@Service
public class Receiver {

    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
    }
}复制代码

运行

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.lianggzone.springboot" })
public class RunMain {
    public static void main(String[] args) {
        SpringApplication.run(RunMain.class, args);
    }
}复制代码

单元测试

建立单元测试用例

public class RabbitMQTest {
    @Autowired
    private Sender sender;
    @Test
    public void send() throws Exception {
        sender.send();
    }
}复制代码

源代码

相关示例完整代码: springboot-action

(完)

更多精彩文章,尽在「服务端铺子」微信公众号!

相关文章
相关标签/搜索