From 9c90a513b7f7c4ffbee10a363009df63bf67239f Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 24 Aug 2026 14:16:04 +0800
Subject: [PATCH] fix(gate): 修复心跳调度器中服务器端口获取问题

---
 src/main/java/com/xcong/excoin/modules/gateApi/StatsEventProducer.java |   72 ++++++++++++++++++++++++++++++++++++
 1 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/StatsEventProducer.java b/src/main/java/com/xcong/excoin/modules/gateApi/StatsEventProducer.java
new file mode 100644
index 0000000..6d38665
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/StatsEventProducer.java
@@ -0,0 +1,72 @@
+package com.xcong.excoin.modules.gateApi;
+
+import com.alibaba.fastjson.JSON;
+import com.xcong.excoin.configurations.RabbitMqConfig;
+import com.xcong.excoin.modules.station.model.GateStatsEvent;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.connection.CorrelationData;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.UUID;
+
+/**
+ * JAR 侧 — 统一消息发送器(心跳 / ACK / 策略事件 / Stats)
+ */
+@Slf4j
+@Component
+public class StatsEventProducer {
+
+    private final RabbitTemplate rabbitTemplate;
+
+    /** RabbitTemplate 是 prototype,必须用构造器注入(参考 OrderProducer) */
+    @Autowired
+    public StatsEventProducer(RabbitTemplate rabbitTemplate) {
+        this.rabbitTemplate = rabbitTemplate;
+    }
+
+    /**
+     * 发送心跳 / ACK 到 heartbeat 路由
+     */
+    public void sendHeartbeat(GateStatsEvent event) {
+        send(RabbitMqConfig.EXCHANGE_GATE, RabbitMqConfig.ROUTINGKEY_GATE_HEARTBEAT, event);
+    }
+
+    /**
+     * 发送策略事件到 stats 路由
+     */
+    public void sendStats(GateStatsEvent event) {
+        send(RabbitMqConfig.EXCHANGE_GATE, RabbitMqConfig.ROUTINGKEY_GATE_STATS, event);
+    }
+
+    private void send(String exchange, String routingKey, GateStatsEvent event) {
+        CorrelationData cd = new CorrelationData(event.getEventId());
+        rabbitTemplate.convertAndSend(exchange, routingKey, JSON.toJSONString(event), cd);
+        log.debug("[StatsProducer] 发送: type={}, apiKeyMd5={}", event.getType(), event.getApiKeyMd5());
+    }
+
+    // ==================== 便捷工厂方法 ====================
+
+    public GateStatsEvent newHeartbeat(String apiKeyMd5, Object payload) {
+        return build("HEARTBEAT", apiKeyMd5, payload);
+    }
+
+    public GateStatsEvent newCmdAck(String apiKeyMd5, Object payload) {
+        return build("CMD_ACK", apiKeyMd5, payload);
+    }
+
+    public GateStatsEvent newStats(String type, String apiKeyMd5, Object payload) {
+        return build(type, apiKeyMd5, payload);
+    }
+
+    private GateStatsEvent build(String type, String apiKeyMd5, Object payload) {
+        return GateStatsEvent.builder()
+                .eventId(UUID.randomUUID().toString())
+                .type(type)
+                .apiKeyMd5(apiKeyMd5)
+                .timestamp(System.currentTimeMillis())
+                .payload(JSON.toJSONString(payload))
+                .build();
+    }
+}

--
Gitblit v1.9.1