rabbitmq五种模式的实现——springboot
基础知识和javase的实现形式可以看我之前的博客
代码地址:https://github.com/9lucifer/rabbitmq4j-learning
一、进行集成
(一)Spring Boot 集成 RabbitMQ 概述
Spring Boot 提供了对 RabbitMQ 的自动配置支持,通过 RabbitTemplate 和 @RabbitListener 可以方便地实现消息的生产和消费。以下是基于 Spring Boot 的 RabbitMQ 集成示例。
(二)生产者代码解析
生产者负责创建消息并将其发送到指定的队列中。
1. 配置文件(application.yml)
spring:
rabbitmq:
host: 自己服务器的ip # RabbitMQ 服务器地址
port: 5672 # RabbitMQ 端口号
username: admin # RabbitMQ 用户名
password: admin # RabbitMQ 密码
virtual-host: / # 虚拟主机(默认是 /)
server:
port: 8081 # 应用端口
2. 生产者代码(Controller)
package top.miqiu.controller;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SendController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendMsg")
public String sendMsg(@RequestParam String msg) {
// 发送消息到队列 boot_queue
rabbitTemplate.convertAndSend("", "boot_queue", msg);
r

最低0.47元/天 解锁文章
236

被折叠的 条评论
为什么被折叠?



