一、前言
本篇主要是围绕着 Spring Boot 3.x 与 RabbitMQ 的集成,这边文章比较简单,RabbitMQ 的集成没有太大的变化,这篇文章主要是为了后续的 RabbitMQ 的动态配置做铺垫。
1、Docker 安装 RabbitMQ
2、Spring Boot 3.x 集成 RabbitMQ
二、Docker 安装 RabbitMQ
1、创建docker-network
docker network create local-net
2、拉取镜像并启动容器
docker search rabbitmq
docker pull rabbitmq:3-management
docker images
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 -v /Users/kenny/docker/rabbitmq/data:/var/lib/rabbitmq/mnesia --network=local-net rabbitmq:3-management
docker ps
docker logs -f rabbitmq
三、Spring Boot 3.x 集成 RabbitMQ
1、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2、application.yml
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
listener:
simple:
acknowledge-mode: manual
retry:
enabled: true
max-attempts: 5
initial-interval: 1000
max-interval: 10000
3、初始化Exchange、Queue
RabbitFountConfig.java
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitFountConfig {
public final static String DEFAULT_EXCHANGE = "exchange.fount";
public final static String DEFAULT_QUEUE = "queue.fount";
public final static String DEFAULT_ROUTING_KEY = "routing.key.fount";
@Bean
public FanoutExchange defalutFanoutExchange() {
return new FanoutExchange(DEFAULT_EXCHANGE, true, false);
}
@Bean
public Queue defaultQueue() {
return new Queue(DEFAULT_QUEUE, true);
}
@Bean
public Binding defaultBinding() {
return BindingBuilder.bind(defaultQueue()).to(defalutFanoutExchange());
}
}
4、接收者
DefaultDirectReceiveQueueService
import com.chain.air.rpp.exchange.config.rabbit.RabbitDirectConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@RabbitListener(queues = RabbitDirectConfig.DEFAULT_QUEUE)
public class DefaultDirectReceiveQueueService {
@RabbitHandler
public void messageReceive(String message) {
log.info("默认 direct 队列接收消息:{}", message);
}
}
5、发送者
RabbitProducerService
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class RabbitProducerService {
@Resource
private RabbitTemplate rabbitTemplate;
public void sendMessageToQueue(String queueName, String message) {
log.info("向队列:{},发送消息:{}", queueName, message);
rabbitTemplate.convertAndSend(queueName, message);
}
public void sendMessageToExchange(String exchangeName, String routingKey, String message) {
log.info("向交换机:{},路由键:{},发送消息:{}", exchangeName, routingKey, message);
rabbitTemplate.convertAndSend(exchangeName, routingKey, message);
}
}
6、Controller
RabbitController.java
import com.chain.air.rpp.exchange.rabbit.RabbitProducerService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/rabbit")
public class RabbitController {
@Resource
private RabbitProducerService rabbitProducerService;
@RequestMapping("/send")
public String sendMessage(@RequestParam String queueName, @RequestParam String message) {
rabbitProducerService.sendMessageToQueue(queueName, message);
return "向队列:" + queueName + ",发送消息:" + message;
}
@RequestMapping("/send/exchange")
public String sendMessageToExchange(@RequestParam String exchangeName, @RequestParam String routingKey, @RequestParam String message) {
rabbitProducerService.sendMessageToExchange(exchangeName, routingKey, message);
return "向交换机:" + exchangeName + ",发送消息:" + message;
}
}
四、测试
1、启动服务
2、查看RabbitMQ控制台,Exchange、Queue是否完成创建
3、通过接口发送消息
4、通过RabbitMQ控制台,往Exchange、Queue分别发送消息,服务是否接收到
下一篇:6、Spring Boot 3.x集成RabbitMQ动态交换机、队列