spring boot 1.5.4 整合rabbitMQ(十七)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 1.5.4 整合rabbitMQ(十七)相关的知识,希望对你有一定的参考价值。

上一篇:spring boot 1.5.4 整合redis、拦截器、过滤器、监听器、静态资源配置(十六)

 

关于rabbitMQ原理,请参阅博客:rabbitMQ消息队列原理

 

1.2.2     创建spring-boot-MQ工程

spring-boot-rabbitMQ

项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址https://github.com/wyait/spring-boot-1.5.4.git

 技术分享

pom.xml文件:

<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <parent>

      <!-- spring boot项目的parent -->

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

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

      <version>1.5.4.RELEASE</version>

   </parent>

   <groupId>com.wyait.mq</groupId>

   <artifactId>spring-boot-mq</artifactId>

   <version>1.0.0-SNAPSHOT</version>

   <dependencies>

      <dependency>

        <!-- spring boot 引入Web模块。自动配置:tomcatspringmvcjackson -->

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

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

        <exclusions>

           <exclusion>

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

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

           </exclusion>

        </exclusions>

      </dependency>

      <dependency>

        <!-- test使用 -->

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

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

        <scope>test</scope>

      </dependency>

      <dependency>

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

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

        <version>1.3.2.RELEASE</version>

      </dependency>

      <dependency>

        <!-- 整合rabbitmq,添加amqp依赖 -->

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

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

      </dependency>

   </dependencies>

<build>

      <plugins>

        <plugin>

           <!-- 配置spring bootmaven插件 -->

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

           <artifactId>spring-boot-maven-plugin</artifactId>

        </plugin>

      </plugins>

   </build>

</project>

 

Application启动类:

@Configuration

@SpringBootApplication

public class MqApplication {

   public static void main(String[] args) {

      SpringApplication.run(MqApplication.class, args);

   }

}

 

添加log4j.propertiesapplication.propertiesapplication-dev.properties配置文件。

 

application-dev.properties配置文件中添加rabbitmq配置:

spring.application.name=springboot-rabbitmq

spring.rabbitmq.host=127.0.0.1

spring.rabbitmq.port=5672

spring.rabbitmq.username=wyait

spring.rabbitmq.password=wyait

spring.rabbitmq.virtual-host=/

# p端收到回调,确认消息发送结果

spring.rabbitmq.publisher-confirms=true

spring.rabbitmq.publisher-returns=true

spring.rabbitmq.template.mandatory=true

 

编写RabbitMqConfig配置类:

创建RabbitMQ的配置类RabbitMqConfig,用来配置队列、交换器、路由等高级信息。这里我们以入门为主,先以最小化的配置来定义,以完成一个基本的生产和消费过程。

@Configuration

public class RabbitMqConfig {

   @Bean

   public Queue helloQueue() {

      return new Queue("hello");

   }

 

}

 

编写Sender类:

创建消息生产者Sender。通过注入AmqpTemplate接口的实例来实现消息的发送,AmqpTemplate接口定义了一套针对AMQP协议的基础操作。在Spring Boot中会根据配置来注入其具体实现。在该生产者,我们会产生一个字符串,并发送到名为hello的队列中。

@Component

public class Sender {

   @Autowired

   private AmqpTemplate rabbitMQTemplate;

 

   public void send() {

      String context = "hello :" + new Date();

      System.out.println("Sender : " + context);

      this.rabbitMQTemplate.convertAndSend("hello",context);

   }

}

 

编写Recevier类:

创建消息消费者Receiver。通过@RabbitListener注解定义该类对hello队列的监听,并用@RabbitHandler注解来指定对消息的处理方法。所以,该消费者实现了对hello队列的消费,消费操作为输出消息的字符串内容。

@Component

// 监听“hello”队列

@RabbitListener(queues ="hello")

public class Receiver {

 

   @RabbitHandler

   // handler注解来指定对消息的处理方法

   public void process(String hello) {

      System.out.println("Receiver:" + hello);

   }

}

 

编写test类:

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest(classes =MqApplication.class)

public class MqApplicationTest{

   @Autowired

   private Sender send;

 

   @Test

   public void test() {

      System.out.println("==========发送消息!");

      send.send();

   }

 

}

 

完成程序编写之后,下面开始尝试运行。首先确保RabbitMQ Server已经开始,然后进行下面的操作:

 

  • 启动应用主类,从控制台中,我们看到如下内容,程序创建了一个访问127.0.0.1:5672wyait的连接。

 

Created newconnection: rabbitConnectionFactory#1e456bc3:0/[email protected] [delegate=amqp://[email protected]:5672/,localPort= 2801]

 

同时,我们通过RabbitMQ的控制面板,可以看到ConnectionChannels中包含当前连接的条目,Queues中查看队列概况。

技术分享

 

  • 运行单元测试类,我们可以看到控制台中输出下面的内容,消息被发送到了RabbitMQ Serverhello队列中。

 

==========发送消息!

Sender : hello:Thu Sep 14 16:06:54 CST 2017

技术分享

 

  • 切换到应用主类的控制台,我们可以看到类似如下输出,消费者对hello队列的监听程序执行了,并输出了接受到的消息信息。

 

技术分享

 

通过上面的示例,我们在Spring Boot应用中引入spring-boot-starter-amqp模块,进行简单配置就完成了对RabbitMQ的消息生产和消费的开发内容。然而在实际应用中,还有很多内容没有演示,下面继续研究其他消息队列模式,大家也可以查阅RabbitMQ的官方教程,有更全面的了解。


 

项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址https://github.com/wyait/spring-boot-1.5.4.git


spring boot系列文章:

spring boot 1.5.4 概述(一)

spring boot 1.5.4入门和原理(二)

spring boot 1.5.4 之web开发(三)

spring boot 1.5.4 整合JSP(四)

spring boot 1.5.4 集成devTools(五)

spring boot 1.5.4 集成JdbcTemplate(六)

spring boot 1.5.4 集成spring-Data-JPA(七)

spring boot 1.5.4 配置文件详解(八)

spring boot 1.5.4 统一异常处理(九)

spring boot 1.5.4 定时任务和异步调用(十)

spring boot 1.5.4 整合log4j2(十一)

spring boot 1.5.4 整合 mybatis(十二)

spring boot 1.5.4 整合 druid(十三)

spring boot 1.5.4 之监控Actuator(十四)

spring boot 1.5.4 整合webService(十五)

spring boot 1.5.4 整合redis、拦截器、过滤器、监听器、静态资源配置(十六)

spring boot 1.5.4 整合rabbitMQ(十七)


本文出自 “架构的路上” 博客,请务必保留此出处http://wyait.blog.51cto.com/12674066/1977527

以上是关于spring boot 1.5.4 整合rabbitMQ(十七)的主要内容,如果未能解决你的问题,请参考以下文章

spring boot 1.5.4 整合rabbitMQ(十七)

spring boot 1.5.4 整合log4j2

spring boot 1.5.4 整合redis拦截器过滤器监听器静态资源配置(十六)

spring boot 1.5.4 集成devTools

spring boot 1.5.4 集成JdbcTemplate

spring boot 1.5.4 配置文件详解