RabbitMQ入门(二)RabbitMQ+SpringBoot的基本使用

前言

       本章讲解RabbitMQ整合SpringBoot时的基本使用

方法

1.环境搭建

1)创建相应springboot工程

2)修改pom文件,添加相关的座标

<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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
	</parent>
	<groupId>cn.edu.ccut</groupId>
	<artifactId>springboot-rabbitmq</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<properties>
		<!-- 设定Java的版本 -->
		<java.version>1.8</java.version>
		<!-- 解决pom.xml首行报错 -->
		<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
	</properties>
	
	<dependencies>
		<!-- 配置springBoot的web启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 配置rabbitmq的启动器 -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<!-- 配置devtools  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<!-- 配置springBoot的test启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
	</dependencies>
	
	 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>cn.edu.ccut.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
</project>

3)修改application.properties文件,添加rabbitmq的相关配置

spring.application.name=springboot-rabbitmq
#rabbitmq config
spring.rabbitmq.host=192.168.1.108
spring.rabbitmq.port=5672
spring.rabbitmq.username=jwang
spring.rabbitmq.password=123456

2.Provider和Consumer的编写

1)创建配置类,用于队列的生成

package cn.edu.ccut.config;

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

@Configuration
public class QueueConfig {

	@Bean
	public Queue getQueue(){
		return new Queue("hello-queue");
	}
}

2)编写生产者Provider代码

package cn.edu.ccut;

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

@Component
public class Provider {

	@Autowired
	private AmqpTemplate rabbitTemplate;
	
	public void sendMsg(String msg){
		rabbitTemplate.convertAndSend("hello-queue", msg);
	}
}

convertAndxiaoxiSend方法的两个参数分别为队列名称和要发送的消息

3)编写消费者代码

package cn.edu.ccut;

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

@Component
public class Consumer {

	@RabbitListener(queues="hello-queue")
	public void receiveMsg(String msg){
		System.out.println("this message is "+msg);
	}
}

消费者使用@RabbitListener注解监听指定的队列发送的消息进行消费!

3.编写测试类,测试运行

1)编写测试代码

package cn.edu.ccut;

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(classes=App.class)
public class RabbitMQTest {
	
	@Autowired
	private Provider provider;
	
	@Test
	public void testSendMsg(){
		provider.sendMsg("hello rabbitmq !");
	}
}

2)运行改代码,观察控制台效果

由此可见,我们的消费者端成功的捕获了这个消息

3)观察rabbitmq的控制端效果

该面板记录了我们所创建的队列的一些相关的属性

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章