1. 程式人生 > >spring boot 1.5.4 整合rabbitMQ(十七)

spring boot 1.5.4 整合rabbitMQ(十七)

rabbitmq springboot springboot1.5.4 springboot整合jsp springboot整合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/SimpleConnection@2d428d9b [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(十七)