RabbitMQ

  RabbitMQ是AMQP(Advanced Message Queuing Protocol,高级消息队列协议)规范的一种实现,用于在分布式系统中实现对消息的存储转发(Store and Forward)。Spring Boot集成RabbitMQ需要添加专门的依赖spring-boot-starter-amqp

源码

  父Maven模块与之前的文章中相同,详情请参阅SpringCloud集成Spring Data Redis

  RabbitMQ的安装可以参考之前的文章:RabbitMQ的安装

spring-rabbitmq

  pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-parent2</artifactId>
        <groupId>com.lyc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-rabbitmq</artifactId>

    <name>spring-rabbitmq</name>

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

</project>

  application.yml

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

  RabbitMQApplication

package com.lyc.springRabbitMQ;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author: zhangzhenyi
 * @date: 2019/3/11 12:35
 * @description: RabbitMQ启动文件
 **/
@SpringBootApplication
public class RabbitMQApplication implements CommandLineRunner {

    @Autowired
    RabbitTemplate rabbitTemplate;

    public static void main(String[] args) {
        SpringApplication.run(RabbitMQApplication.class,args);
    }

    @Override
    public void run(String... strings) throws Exception {
        rabbitTemplate.convertAndSend("rabbitQueue","message from rabbit");
    }

}

  RabbitMQConfig

package com.lyc.springRabbitMQ.conf;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: zhangzhenyi
 * @date: 2019/3/11 12:37
 * @description: 创建RabbitMQ队列
 **/
@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue rabbitQueue(){
        return new Queue("rabbitQueue");
    }

}

  Receiver

package com.lyc.springRabbitMQ.component;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author: zhangzhenyi
 * @date: 2019/3/11 12:45
 * @description: 接收者
 **/
@Component
public class Receiver {

    @RabbitListener(queues = "rabbitQueue")
    public void receiveMessage(String message){
        System.out.println("Received:" + message);
    }

}

运行

  运行结果如下:

2019-03-11 12:55:52.751  INFO 244 --- [cTaskExecutor-1] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#1662cd08:0/[email protected] [delegate=amqp://[email protected]:5672/, localPort= 57575]
2019-03-11 12:55:52.939  INFO 244 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2019-03-11 12:55:52.957  INFO 244 --- [           main] c.l.springRabbitMQ.RabbitMQApplication   : Started RabbitMQApplication in 5.822 seconds (JVM running for 6.508)
Received:message from rabbit

  从Rabbit网页客户端中也可以清楚的看到该创建的队列,如下:

SpringCloud集成RabbitMQ

分析

  在Spring框架中提供了一个RabbitTemplate工具类对通过AMQP与RabbitMQ进行交互的过程进行了封装。与其他Template工具类一样,RabbitTemplate的背后同样是通过ConnectionFactory获取RabbitMQ连接,但在Spring Boot中,这部分工作是通过自动配置机制已经得以实现,一般情况下我们只需要注入RabbitTemplate即可,该注入是在Bootstrap启动类中注入的。

  上面的代码的逻辑是这样的:

  队列的创建是在RabbitMQConfig中进行,创建的队列名称为“rabbitQueue”,然后在项目启动时,直接运行CommandLineRunner接口中的void run(String... var1) throws Exception;方法,由该方法在队列“rabbitQueue”中插入信息。最后通过Receiver来接收信息,通过使用@RabbitListener(queues = "rabbitQueue")注解,我们就能够接收到指定队列中所传递的信息了。

相关文章: