mq 配置
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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_
|
||||
|
||||
Reference in New Issue
Block a user