# boot-rabbitmq **Repository Path**: liang-tian-yu/boot-rabbitmq ## Basic Information - **Project Name**: boot-rabbitmq - **Description**: rabbitmq使用 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-09 - **Last Updated**: 2025-07-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## boot-rabbitmq springboot结合rabbitmq使用 ## 入门使用 - 引入依赖 ``` com.rabbitmq amqp-client 5.7.0 ``` - 消费者 ``` package com.lty.mq.demo; import com.rabbitmq.client.*; import java.io.IOException; import java.util.concurrent.TimeoutException; /** * 消费者 */ public class Consumer { private final static String QUEUE_NAME = "hello"; public static void main(String[] args) throws IOException, TimeoutException { // 创建连接 ConnectionFactory factory = new ConnectionFactory(); // 设置 RabbitMQ 的主机名 factory.setHost("localhost"); // factory.setUsername("guest"); // factory.setPassword("guest"); // 创建一个连接 Connection connection = factory.newConnection(); // 创建一个通道 Channel channel = connection.createChannel(); // 指定一个队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 创建队列消费者 com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println("Received Message '" + message + "'"); } }; channel.basicConsume(QUEUE_NAME, true, consumer); } } ``` - 生产者 ``` package com.lty.mq.demo; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; /** * 生产者 */ public class Producer { private final static String QUEUE_NAME = "hello"; public static void main(String[] args) throws IOException, TimeoutException { // 创建连接 ConnectionFactory factory = new ConnectionFactory(); // 设置 RabbitMQ 的主机名 factory.setHost("localhost"); // factory.setUsername("guest"); // factory.setPassword("guest"); // 创建一个连接 Connection connection = factory.newConnection(); // 创建一个通道 Channel channel = connection.createChannel(); // 指定一个队列,不存在的话自动创建 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 发送消息 String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); // 关闭频道和连接 channel.close(); connection.close(); } } ``` ## RabbitMQ实战 - 导入依赖 ``` org.springframework.boot spring-boot-starter-amqp 2.7.0 ``` - application配置 ``` spring: # rabbitmq配置 rabbitmq: host: localhost port: 5672 password: guest username: guest # RabbitMQ队列配置 rabbitmq: queue: name: hello ``` - 消费者 ``` package com.lty.mq.project; import com.rabbitmq.client.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; import java.io.IOException; import java.util.concurrent.TimeoutException; @Configuration @Slf4j public class RabbitMQConsumer { @Value("${spring.rabbitmq.host}") private String host; @Value("${rabbitmq.queue.name:hello}") private String queueName; @PostConstruct public void init() { new Thread(this::startConsuming).start(); } private void startConsuming() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host); try { Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // 声明队列(确保队列存在) channel.queueDeclare(queueName, false, false, false, null); // 创建消费者 com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); log.info("Received Message: '{}'", message); } }; // 开始消费 channel.basicConsume(queueName, true, consumer); } catch (IOException | TimeoutException e) { log.error("Failed to start RabbitMQ consumer", e); } } } ```