代码中有注解可参考
1.引入相关的maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency>
2. 配置文件
# rabbitmq配置信息 # ip spring.rabbitmq.host=192.168.14.200 # 端口 spring.rabbitmq.port=5672 # 用户名 spring.rabbitmq.username=guest # 密码 spring.rabbitmq.password=guest
3.启动类
package com.lh.springboot; import org.springframework.amqp.rabbit.annotation.EnableRabbit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /* 1. 自动配置 RabbitAutoConfiguration 类 2. 又自动配置了 连接工厂 ConnectionFactory 3. RabbitProperties 封装了 Rabbitmq的所有配置 4. RabbitTemplate 给Rabbitmq 发送和接收信息的 5. AmqpAdmin 是rabbitmq 的系统管理功能组件 AmqpAdmin 创建和删除 Queues、Exchanges、 Bindings 6. @EnableRabbit + @RabbitListener 监听消息队列的内容 */ @SpringBootApplication @EnableRabbit //开启基于注解的Rabbitmq模式 public class Springbootweb03Application { public static void main(String[] args) { SpringApplication.run(Springbootweb03Application.class, args); } }
4.config.RabbitmqConfig 类
package com.lh.springboot.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; /** * @program: springbootweb03 * @description: rabbitmq的配置类 * @author: li hui * @create: 2020-12-22 15:25 */ @Configuration public class RabbitmqConfig { @Bean public MessageConverter messageConverter(){ //new Jackson2JsonMessageConverter() 将发送的信息已JSON发送 return new Jackson2JsonMessageConverter(); } }
5.bean.Book类
package com.lh.springboot.bean; /** * @program: springbootweb03 * @description: * @author: li hui * @create: 2020-12-22 15:37 */ 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; } }
6.测试类
package com.lh.springboot;
import com.lh.springboot.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 Springbootweb03ApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private AmqpAdmin amqpAdmin;
//AmqpAdmin 是rabbitmq 的系统管理功能组件
//AmqpAdmin 创建和删除 Queues、Exchanges、 Bindings
@Test
void createExchange(){
//创建DirectExchange
/* 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","amqpAdmin.haha",null));
*/
//删除
amqpAdmin.deleteQueue("huige.news");
}
/*
1.点播 (点对点)
*/
//发送信息
@Test
void contextLoads() {
/**
* @Author: li hui
* @Param: []
* @return: void
* @date: 2020/12/22
* @Description:
*
* 两个方法都可以做到发送消息
* rabbitTemplate.send(exchange,routingKey,message);
* rabbitTemplate.convertAndSend(exchange,routingKey,object);
*
* exchange: 交换器
* routingKey: 路由件
* message: 发送的消息 自己构造的一个;定义消息内容和消息头
* object: 对象 默认当成消息体 只需要传入发送的对象 自动序列化发送给Rabbitmq
*/
Map<String,Object> map = new HashMap<>();
map.put("msg","第一个消息");
map.put("data", Arrays.asList("hello world !!", 123, true));
//对象被默认序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.direct","hui",map);
}
//接收消息,如何将数据自动的转为JSON发送出去
@Test
void receive(){
Object hui = rabbitTemplate.receiveAndConvert("hui");
System.out.println(hui);
}
/*
1.广播
*/
// 交换器 fanout 类型 发送信息
@Test
void senMsg(){
rabbitTemplate.convertAndSend("exchange.fanout","",new Book("红楼梦","曹雪芹"));
}
}
监听 消息对列
BookServicer 类
package com.lh.springboot.service;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* @program: springbootweb03
* @description:
* @author: li hui
* @create: 2020-12-22 15:47
*/
@Service
public class BookService {
@RabbitListener(queues = {"hui","hui.news","hui.emps","lihui.news"}) // queues 监听消息队列 是一个数组的方式
public void receive(Map map){
System.out.println("收到的消息:"+ map.toString());
}
}
启动主程序类 , 利用测试类 发送信息查看 主程序中是否有监听到