`

85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】

阅读更多

 à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

 

 

       这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用RabbitMQ,那么本节分如下几个步骤:

(1) 新建Maven Java Project;

(2) pom.xml添加相关依赖;

(3) 编程+测试

(4) 配置信息

       接下来看看每个步骤是怎么操作的。

(1) 新建Maven Java Project;

       新建一个Maven Java Project项目,取名为spring-boot-rabbitmq

(2) pom.xml添加相关依赖;

       这里需要加入基本的依赖以及rabbitmq相关的依赖,具体如下:

       <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">

  <modelVersion>4.0.0</modelVersion>

 

  <groupId>com.kfit</groupId>

  <artifactId>spring-boot-rabbitmq</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>spring-boot-rabbitmq</name>

  <url>http://maven.apache.org</url>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- jdk版本号,Angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->

    <java.version>1.8</java.version>

  </properties>

 

 

    <!--

       spring boot 父节点依赖,

       引入这个之后相关的引入就不需要添加version配置,

       spring boot会自动选择最合适的版本进行添加。

     -->

    <parent>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-parent</artifactId>

       <version>1.3.3.RELEASE</version>

    </parent>

 

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <scope>test</scope>

    </dependency>

   

    <!-- spring boot web支持:mvc,aop... -->

    <dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

   

    <dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-amqp</artifactId>

    </dependency>

   

  </dependencies>

</project>

 

(3) 编程+测试

       在这里我们为了方便直接在启动类App.java进行编码,先提供代码:

package com.kfit;

 

 

import java.util.Date;

 

import org.springframework.amqp.core.Queue;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;

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

import org.springframework.amqp.rabbit.core.RabbitTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

import org.springframework.messaging.handler.annotation.Payload;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;

 

/**

 *

 * @author Angel --守护天使

 * @version v.0.1

 * @date 2016823

 */

@SpringBootApplication

@EnableScheduling//启用任务调度.

@RabbitListener(queues="foo")//启用Rabbit队列监听foo key.

public class App {

   

   

    //rabbit操作类;

    @Autowired

    private RabbitTemplate rabbitTemplate;

      

    @Scheduled(fixedDelay=3000)//3s执行1次此方法;

    public void send(){

       rabbitTemplate.convertAndSend("foo","zhang");

    }

   

    @Bean

    public Queue fooQueue(){

       returnnew  Queue("foo");

    }

       

    //接收到消息处理.

    @RabbitHandler

    public void onMessage(@Payload String foo){

       System.out.println(" >>> "+new Date() + ": " + foo);

    }

   

   

    public static void main(String[] args) {

       SpringApplication.run(App.class, args);

    }

}

       好了,到这里就可以运行测试了,观察控制台的打印信息:

 >>> Tue Aug 23 15:06:54 CST 2016: zhang

 >>> Tue Aug 23 15:06:57 CST 2016: zhang

 >>> Tue Aug 23 15:07:00 CST 2016: zhang

            看到如上信息,恭喜你简单的例子编写成功了。好了,上面的源代码还是有必要进行简单的讲解下。

            首先我们需要清除RabbitMQ的话有这么几个角色,消息生产者或者说消息提供者(sender);其次就是消息队列提供存放消息的对方(queue);消息消费者或者说消息接收者(receiver)。知道这些概念之后在看代码就好理解很多了。

            我们先看消息提供者:send()方法,这里使用Spring 提供的RabbitTemplate 进行操作消息,调用convertAndSend方法将消息发布到对应的消息频道上,这里也就是使用Queue进行存储;然后我们注意到这个方法不是我们人为执行的方法而是采用定时执行的方式进行3s一次进行发布消息,所以你会看到send方法上有一个注解@Scheduled,那么使用该注解的话,相应的类上上面就需要@EnableScheduling启用注解。

       在上面的分析中我们说消息被发布到队列中,所以我们需要创建一个Queue进行存放,这里需要注意Queue的包路径是org.springframework.amqp.core.Queue

这里我们注入了fooQueue 方法指定队列的名称是foo,那么此队列只会处理keyfoo的消息,其它的key不会处理。

       最后就是消息的接收者了:这里我们使用了@RabbitHandler定义了一个消息接收者onMessage,其中这个方法名可以随意取名,再者我们需要启用Rabbit监听,所以需要在类上添加@RabbitListener,这样编写完就能按照我们的想法执行了。那么如果我们编写了两个消息接收者的话,可以正常运行嘛,两个接收者都能接收到消息嘛,答案是肯定的,因为我们以上的编码是发布(Pub)/订阅(Sub)消息模型。

       很有意思的是,这个方法还可以在简化下,将类上的@RabbitListener直接转移到方法上,去掉@RabbitHandler注解,直接为如下:

//接收到消息处理.

    @RabbitListener(queues = "foo")

    public void onMessage(@Payload String foo){

       System.out.println(" >new>> "+new Date() + ": " + foo);

    }

 

视频&交流平台

à SpringBoot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

 

 

 

 

(4) 配置信息

       在以上的代码你会发现我们根本没有添加什么配置信息,所以spring boot提供了默认的配置,那么我们应该怎么修改配置呢,只需要在application.properties进行配置即可:

# RABBIT (RabbitProperties) 

spring.rabbitmq.host= # connection host 

spring.rabbitmq.port=# connection port 

spring.rabbitmq.addresses= # connection addresses (e.g. myhost:9999,otherhost:1111) 

spring.rabbitmq.username= # login user 

spring.rabbitmq.password= # login password 

spring.rabbitmq.virtualhost= 

spring.rabbitmq.dynamic= 

 

 

       这里可以配置主机地址,端口号,用户名密码(默认使用guest/guest)。

 

 

 

分享到:
评论
4 楼 林祥纤 2017-07-21  
PanlJlove 写道
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect  无法连接



连接问题,检查下AMQP地址,端口等。
3 楼 PanlJlove 2017-07-11  
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect  无法连接
2 楼 justimkiss 2017-06-20  
XW65432 写道
请问,spring.rabbitmq.addresses这个配置是存放什么的


RabbitProperties
1 楼 XW65432 2016-12-12  
请问,spring.rabbitmq.addresses这个配置是存放什么的

相关推荐

Global site tag (gtag.js) - Google Analytics