springboot学习笔记(十)—— springboot整合RabbitMQ

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41622183/article/details/82825441

前言:

      MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。其中较为成熟的MQ产品有IBM WEBSPHERE MQ等等。

                                                                       (以上来自百度百科)

       RabbitMQ是什么?RabbitMQ是流行的开源消息队列系统,用erlang语言开发。RabbitMQ是AMQP(高级消息队列协议)的标准实现。在我们开发中,也会听到过一些中间件。RabbitMQ便是中间件之一,它可以做的事情很多,例如:低耦合可靠投递广播流量控制最终一致性。在互联网项目中,rabbitmq也是很常见的(kafka也是),秒杀类项目更是少不了这类中间件。下面来介绍springboot整合RabbitMQ吧~!

开发环境:

         win10+IntelliJ IDEA +JDK1.8+RabbitMQ

         springboot版本:springboot 1.5.14 ——2.0后的springboot增加了挺多新特性,暂时先不做了解

RabbitMQ环境的安装和配置请参考:

               随笔(五) 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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.test</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.13.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>

		<dependency>
			<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-amqp</artifactId>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

          主要是添加这个依赖:

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

     application.properties:


spring.application.name=hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=Gentle
spring.rabbitmq.password=123456

代码编写:

      Config:配置队列的名字,并创建这个队列的bean

package com.test.demo.rabbitmq;

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

/**
 * 配置类
 * @author Gentle
 */
@EnableRabbit
@Configuration
public class RabbitMQConfig {
	//队列的名字,需要配置自己的队列
	public static final String QUEUE_NAME = "hello";
	@Bean
	public Queue queue() {
		return new Queue(QUEUE_NAME);
	}
}

       发送者:由此类向队列发送消息

package com.test.demo.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息发送者
 * @author Gentle
 */
@Service
public class Sender {
	@Autowired
	private AmqpTemplate rabbitTemplate;
	public void send() {
		//发送消息
		rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, "哈哈(*´▽`)ノノ,你好~!Gentle");
	}
}

      接收者:发送者发送消息后,rabbitMQ服务器发送过来,此类用于接收

package com.test.demo.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
/**
 * 消息接收者
 * @author Gentle
 */
@Service
public class Receiver {
	@RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
	public void receiveMessage(String message) {

		System.out.println("接收的消息是:"+message);
	}
}

      测试类:

package com.test.demo;

import com.test.demo.rabbitmq.Sender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

	@Autowired
	Sender sender;
	@Test
	public void contextLoads() {

		try {
			sender.send();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

测试结果:

          

总结:

        简单入门教程没多大难度的,基本看上几遍就能学会如何使用,但是真正要符合业务需求的话也是要做很多准备。这里仅做了简单入门。最后,祝大家学习进步,工作顺利。

                                                                                                                                                                         --谢谢~!

程序人生,与君共勉~!

猜你喜欢

转载自blog.csdn.net/weixin_41622183/article/details/82825441