一、RabbitMQ的配置搭建
请参考 消息队列——RabbitMQ在Centos的基本搭建
二、测试demo使用
1.创建一个Springboot项目 hello-rabbit,并加上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 https://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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.basic</groupId>
<artifactId>hello-rabbit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello-rabbit</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.builde.sourceEncoding>UTF-8</project.builde.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.新建一个包名config,并新增配置类RabbitConfiguration,配置队列名:TestRabbitMQ
package com.basic.hellorabbit.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description: 队列配置
* @Author: Joe
* @CreateDate: 2020/3/11 23:49
*/
@Configuration
public class RabbitConfiguration {
//队列名称:TestRabbitMQ
@Bean
public Queue queue(){
return new Queue("TestRabbitMQ");
}
}
3.主目录下新建一个消息生产者的类RabbitProvider,且将消息发送到指定的队列:TestRabbitMQ
package com.basic.hellorabbit;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @Description: 消息生产者/提供者
* @Author: Joe
* @CreateDate: 2020/3/11 23:50
*/
@Component
public class RabbitProvider {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(){
String content = "你好!"+ new Date();
System.out.println("生产者:" + content);
amqpTemplate.convertAndSend("TestRabbitMQ",content);
}
}
4.修改application.yml里对RabbitMQ的服务配置,注意:yml文件的冒号后空格,且此处的RabbitMQ使用的默认client端通信口5672
spring:
rabbitmq:
host: ip地址
port: 5672
username: guest
password: guest
5.在test之下写一个测试类
package com.basic.hellorabbit;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = HelloRabbitApplication.class)
class HelloRabbitApplicationTests {
@Autowired
private RabbitProvider rabbitProvider;
/**
* 测试发送20条数据
*/
@Test
void contextLoads() {
for (int i = 0; i < 20; i++) {
rabbitProvider.send();
}
}
}
6.测试前队列界面如图
7.运行完测试类控制台信息及队列界面如图,可见消息已经加入到队列TestRabbitMQ,且为20条
8.主目录下新建一个消息消费者的类RabbitConsumer,指定消费TestRabbitMQ队列消息
package com.basic.hellorabbit;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @Description: 消费者
* @Author: Joe
* @CreateDate: 2020/3/12 17:08
*/
@Component
@RabbitListener(queues = "TestRabbitMQ") //监听、订阅、消费该队列
public class RabbitConsumer {
@RabbitHandler
public void subscribe(String content){
System.out.println("消费者:"+content);
}
}
9.启动类HelloRabbitApplication启动,控制台信息及界面如图,队列TestRabbitMQ中的20条消息已经订阅完
10.熟悉这些可以方便我们尽快入手使用,在项目中尽快用起来
1.日志统计:异步统计网站的流量uv,pv,处理数据,日志收集等
2.异步处理:网站用户注册信息成功,购买成功等发送提示短信或者邮件等
3.应用解耦:分布式系统中的订单系统,订单和库存交给队列等
4.流量削峰:秒杀系统等
……