Springboot整合RabbitMq学习记录(windows)
1,安装rabbitMq和ErLang
链接: win10 安装RabbitMQ时 执行:rabbitmq-plugins enable rabbitmq_management命令报 Plugin configuration unchanged
2,pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
3,springboot项目结构
分为服务端和消费端两个项目
服务端:
消费端:
4,yml文件
服务端和消费端在rabbitmq部分的配置相同
server:
port: 8040
spring:
application:
name: myProjectConsumer
#配置 rabbitMq 服务器
rabbitmq:
# 虚拟主机
virtual-host: /
#设置RabbitMQ的IP地址
host: localhost
#设置rabbitmq服务器连接端口(应用访问端口号是5672,不是控制台端口号15672)
port: 5672
#设置rabbitmq服务器用户名 本地搭建对应的账户密码都是 guest
username: guest
#设置rabbitmq服务器密码
password: guest
5,RabbitMqConfig
服务端:
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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 RabbitMqConfig {
/**
* RabbitTemplate是RabbitMQ在与SpringAMQP整合的时,Spring提供的即时消息模板
* RabbitTemplate提供了可靠性消息投递方法、回调监听消息接口ConfirmCallback、返回值确认接口ReturnCallback等等
*/
@Bean
public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter());
return rabbitTemplate;
}
/***序列化方式使用Jackson2JsonMessageConverter序列化器*/
@Bean
public MessageConverter jackson2JsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
/**
* 创建一个名称为 hello 的队列
*/
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
final static String red="topic.red";
final static String blue ="topic.blue";
final static String all ="topic.all";
@Bean
public Queue queueRed(){
return new Queue(red);
}
@Bean
public Queue queueBlue(){
return new Queue(blue);
}
@Bean
public Queue queueAll(){
return new Queue(all);
}
@Bean
TopicExchange exchange(){
return new TopicExchange("exchange");
}
@Bean
Binding bindingExchangeRed(Queue queueRed, TopicExchange exchange){
return BindingBuilder.bind(queueRed).to(exchange).with("topic.red");
}
@Bean
Binding bindingExchangeBlue( Queue queueBlue, TopicExchange exchange){
return BindingBuilder.bind(queueBlue).to(exchange).with("topic.blue");
}
@Bean
Binding bindingExchangeAll( Queue queueAll, TopicExchange exchange){
return BindingBuilder.bind(queueAll).to(exchange).with("topic.#");
}
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("myFanout");
}
@Bean
public Binding bindingFanoutExchageRed(Queue queueRed, FanoutExchange fanoutExchange){
return BindingBuilder.bind(queueRed).to(fanoutExchange);
}
@Bean
public Binding bindingFanoutExchageBlue(Queue queueBlue, FanoutExchange fanoutExchange){
return BindingBuilder.bind(queueBlue).to(fanoutExchange);
}
@Bean
public Binding bindingFanoutExchageAll(Queue queueAll, FanoutExchange fanoutExchange){
return BindingBuilder.bind(queueAll).to(fanoutExchange);
}
}
消费端:需要配置RabbitMqTemlate和具体使用的队列:
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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 RabbitMqConfig {
/**
* 创建一个名称为 hello 的队列
*/
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
@Bean
public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter());
return rabbitTemplate;
}
/***默认RabbitMQ序列化方式是SerializerMessageConverter序列化器,这么我们使用Jackson2JsonMessageConverter序列化器。我们需要设置下,内容如下:*/
@Bean
public MessageConverter jackson2JsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
final static String red="topic.red";
final static String blue ="topic.blue";
final static String all ="topic.all";
@Bean
public Queue queueRed(){
return new Queue(red);
}
@Bean
public Queue queueBlue(){
return new Queue(blue);
}
@Bean
public Queue queueAll(){
return new Queue(all);
}
}
6,服务端Sender
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@Slf4j
public class Sender {
@Autowired
private AmqpTemplate rabbitTemplate;
/**
* 简易发送消息
*/
public void send(){
String context="hello "+ new Date();
log.info("sender:发送消息"+context);
this.rabbitTemplate.convertAndSend("hello",context);
}
/**
* 通过交换机exchange使用topic匹配发送消息
*/
public void send1(){
String context="topic.red";
rabbitTemplate.convertAndSend("exchange","topic.red",context);
}
public void send2(){
String context="topic.blue";
rabbitTemplate.convertAndSend("exchange","topic.blue",context);
}
public void sendAll(){
String context="topic.#";
rabbitTemplate.convertAndSend("exchange","topic",context);
}
/**
* 通过myFanout广播向所有绑定的通道发送消息
*/
public void sendF(){
String context="topic.fanout";
rabbitTemplate.convertAndSend("myFanout","",context);
}
}
7,服务端测试用TestController
import com.myProjectRabbitMQLearn.rabbitMq.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private Sender sender;
@GetMapping("/send")
public void testSend(){
sender.send();
}
@GetMapping("/send1")
public void testSend1(){
sender.send1();
}
@GetMapping("/send2")
public void testSend2(){
sender.send2();
}
@GetMapping("/sendAll")
public void testSendAll(){
sender.sendAll();
}
@GetMapping("/sendf")
public void testSendF(){
sender.sendF();
}
}
8,消费端监听通道的方法
Receiver1,2,3:
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@RabbitListener(queues = "topic.red")
public class Receiver1 {
@RabbitHandler
public void process(String s){
log.info("topic.red通道: 消费成功"+s);
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@RabbitListener(queues = "topic.blue")
public class Receiver2 {
@RabbitHandler
public void process(String s){
log.info("topic.blue通道: 消费成功"+s);
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@RabbitListener(queues = "topic.all")
public class Receiver3 {
@RabbitHandler
public void process(String s){
log.info("topic.all通道: 消费成功"+s);
}
}
MesssageConsumeService:
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
@RabbitListener(queuesToDeclare = @Queue("hello"))
@Slf4j
public class MesssageConsumeService {
@RabbitHandler
public void process(String s){
log.info("receiver1: 消费成功"+s);
}
}
MessageReveiver2:
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@RabbitListener(queues = "hello")
public class MessageReveiver2 {
@RabbitHandler
public void process(String s){
log.info("reveiver2: 消费成功"+s);
}
}
9,测试
调用服务端TestController里的接口,在消费端查看接收的消息:
参考文章
感谢以下大佬的文章:
链接: SpringBoot RabbitMQ 入门学习(详细)
链接: Spring Boot:RabbitMQ详解