mq 配置

This commit is contained in:
GYJ
2025-02-14 13:36:59 +08:00
parent c1b4f06670
commit 4c6d521118
11 changed files with 219 additions and 2 deletions

View File

@@ -0,0 +1,29 @@
package com.czg.mq;
import com.czg.config.RabbitPublisher;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author GYJoker
*/
@RestController
public class RabbitMqController {
@Resource
private RabbitPublisher rabbitPublisher;
@GetMapping("/sendOrderPrintMsg")
public String sendOrderPrintMsg(String msg) {
rabbitPublisher.sendOrderPrintMsg(msg);
return "success";
}
@GetMapping("/sendOrderStockMsg")
public String sendOrderStockPrintMsg(String msg) {
rabbitPublisher.sendOrderStockMsg(msg);
return "success";
}
}

View File

@@ -0,0 +1,45 @@
package com.czg.mq;
import com.czg.config.RabbitConstants;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* @author GYJoker
*/
@Slf4j
@Component
public class RabbitmqReceiver {
/**
* 消费者监听,绑定队列
*/
@RabbitListener(
bindings = @QueueBinding(value = @Queue(value = "#{orderPrintQueue.name}", durable = "true",
arguments = {@Argument(name = "x-message-ttl", value = "180000", type = "java.lang.Long")}),
exchange = @Exchange(value = RabbitConstants.Exchange.CASH_EXCHANGE)),
concurrency = "10"
)
@RabbitHandler
public void receiveOrderPrintQueue(Channel channel, String payload, Message message) throws IOException {
try {
System.out.println("Topic模式(orderPrintQueue)消费者收到消息: " + message);
System.out.println(payload);
// 手动确认消息multiple 参数表示是否批量确认
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
log.error(e.getMessage(), e);
// 判断是否需要重新入队
boolean requeue = false;
// 拒绝消息requeue 为 true 表示将消息重新放回队列
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, requeue);
}
}
}

View File

@@ -20,6 +20,12 @@ spring:
server-addr: 121.40.109.122:8848
namespace: 237e1905-0a66-4375-9bb6-a51c3c034aca
rabbitmq:
host: 121.40.109.122
port: 5672
username: chaozg
password: chaozg123
dubbo:
application:
name: order-server
@@ -34,3 +40,5 @@ dubbo:
threads: 20
threadpool: fixed
rabbitmq:
prefix: dev_