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_

View File

@ -37,4 +37,4 @@
</dependency>
</dependencies>
</project>
</project>

View File

@ -25,6 +25,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: product-server

View File

@ -0,0 +1,24 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.czg</groupId>
<artifactId>cash-common</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>cash-common-mq</artifactId>
<name>cash-common-mq</name>
<url>https://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.czg</groupId>
<artifactId>cash-common-tools</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,51 @@
package com.czg.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.util.HashMap;
import java.util.Map;
/**
* @author GYJoker
*/
@Configuration
public class RabbitConfig {
@Value("${spring.profiles.active}")
private String activeProfile;
@Bean
public Queue orderPrintQueue() {
// 创建一个用于存储队列参数的 Map
Map<String, Object> args = new HashMap<>();
// 设置消息过期时间为 180000 毫秒 180
args.put("x-message-ttl", 180000);
return new Queue(activeProfile + "-" + RabbitConstants.Queue.ORDER_PRINT_QUEUE, true, false, false, args);
}
@Bean
public Queue orderStockQueue() {
return new Queue(activeProfile + "-" + RabbitConstants.Queue.ORDER_STOCK_QUEUE, true);
}
@Bean
@Primary
public DirectExchange directExchange() {
return new DirectExchange(activeProfile + "-" + RabbitConstants.Exchange.CASH_EXCHANGE);
}
@Bean
public Binding bindingOrderPrintExchange(Queue orderPrintQueue, DirectExchange exchange) {
return BindingBuilder.bind(orderPrintQueue).to(exchange).with(activeProfile + "-" + RabbitConstants.Queue.ORDER_PRINT_QUEUE);
}
@Bean
public Binding bindingOrderStockExchange(Queue orderStockQueue, DirectExchange exchange) {
return BindingBuilder.bind(orderStockQueue).to(exchange).with(activeProfile + "-" + RabbitConstants.Queue.ORDER_STOCK_QUEUE);
}
}

View File

@ -0,0 +1,16 @@
package com.czg.config;
/**
* @author GYJoker
*/
public interface RabbitConstants {
class Exchange {
public static final String CASH_EXCHANGE = "czg-cashier";
}
class Queue {
public static final String ORDER_STOCK_QUEUE = "order.stock.queue";
public static final String ORDER_PRINT_QUEUE = "order.print.queue";
}
}

View File

@ -0,0 +1,32 @@
package com.czg.config;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* @author GYJoker
*/
@Service
public class RabbitPublisher {
@Value("${spring.profiles.active}")
private String activeProfile;
@Resource
private RabbitTemplate rabbitTemplate;
public void sendOrderStockMsg(String msg) {
sendMsg(RabbitConstants.Exchange.CASH_EXCHANGE, RabbitConstants.Queue.ORDER_STOCK_QUEUE, msg);
}
public void sendOrderPrintMsg(String msg) {
sendMsg(RabbitConstants.Exchange.CASH_EXCHANGE, RabbitConstants.Queue.ORDER_PRINT_QUEUE, msg);
}
private void sendMsg(String exchange, String queue, String msg) {
rabbitTemplate.convertAndSend(activeProfile + "-" + exchange, activeProfile + "-" + queue, msg);
}
}

View File

@ -17,6 +17,7 @@
<module>cash-common-api-config</module>
<module>cash-common-service</module>
<module>cash-common-log</module>
<module>cash-common-mq</module>
</modules>
</project>

View File

@ -23,5 +23,10 @@
<artifactId>czg-pay</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.czg</groupId>
<artifactId>cash-common-mq</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
</project>