自动配置:
1、RabbitAutoConfiguration
2、自动配置了连接工厂CachingConnectionFactory
3、RabbitProperties封装了RabbitMQ的配置
4、RabbitTemplate:给RabbitMQ发送和接收消息
5、AmqpAdmin:系统管理功能组件。AmqpAdmin用来创建和删除queue、exchange、Binding
6、@EnableRabbit+@RabbitListener来监听消息队列里的内容。
示例:
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>
<groupId>com.qublog</groupId>
<artifactId>springboot10_amqp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot10_amqp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.rabbitmq.host=192.168.30.128
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
Springboot10AmqpApplicationTests类:
package com.qublog.springboot10_amqp;
import com.qublog.springboot10_amqp.bean.Book;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class Springboot10AmqpApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void create() {
// amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
// System.out.println("创建完成");
// amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
//创建绑定
amqpAdmin.declareBinding(new Binding("amqpadmin.queue",Binding.DestinationType.QUEUE,
"amqpadmin.exchange","amqp.haha",null));
}
/*
* 1、单播(点对点)
* 2、
* */
@Test
void contextLoads() {
//Message需要自己构造,可以定制消息体内容和消息头
//rabbitTemplate.send(exchange,routeKey,message);
//object默认当成消息体,只需要传入要发送的对象,自动序列化,保存到rabbitmq
//rabbitTemplate.convertAndSend(exchange,routeKey,object);
Map<String,Object> map = new HashMap<String, Object>();
map.put("msg","这是第一个消息");
map.put("data", Arrays.asList("hello",123,true));
//对象默认序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.direct","qublog.news",map);
}
//接收数据
@Test
public void receive() {
Object o = rabbitTemplate.receiveAndConvert("qublog.news");
System.out.println(o.getClass());
System.out.println(o);
}
@Test
void send() {
rabbitTemplate.convertAndSend("exchange.direct","qublog.news",new Book("西游记","吴承恩"));
}
//广播
@Test
void sendMsg() {
rabbitTemplate.convertAndSend("exchange.fanout","",new Book("三国演义","罗贯中"));
}
}
Book类:
package com.qublog.springboot10_amqp.bean;
public class Book {
private String bookName;
private String author;
public Book() {
}
public Book(String bookName, String author) {
this.bookName = bookName;
this.author = author;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"bookName='" + bookName + '\'' +
", author='" + author + '\'' +
'}';
}
}
MyAMQPConfig类:
package com.qublog.springboot10_amqp.config;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAMQPConfig {
//将消息转换为json格式发送出去
@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
}
BookService类:
package com.qublog.springboot10_amqp.service;
import com.qublog.springboot10_amqp.bean.Book;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class BookService {
@RabbitListener(queues = {"qublog.news"})
public void receive(Book book) {
System.out.println("收到消息"+book.toString());
}
@RabbitListener(queues = {"qublog"})
public void receive2(Message message) {
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}
Springboot10AmqpApplication类:
package com.qublog.springboot10_amqp;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//开启基于注解的rabbitmq
@EnableRabbit
@SpringBootApplication
public class Springboot10AmqpApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot10AmqpApplication.class, args);
}
}