package com.xcong.excoin.modules.station.consumer; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.xcong.excoin.configurations.RabbitMqConfig; import com.xcong.excoin.modules.station.model.CmdAckMsg; import com.xcong.excoin.modules.station.model.GateStatsEvent; import com.xcong.excoin.modules.station.model.HeartbeatMsg; import com.xcong.excoin.modules.station.registry.InstanceRegistry; import com.xcong.excoin.modules.station.service.StationService; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import javax.annotation.Resource; /** * Station 消费心跳 + ACK + 策略事件 — 均复用到 QUEUE_GATE_HEARTBEAT 和 QUEUE_GATE_STATS */ @Slf4j @Component public class GateMessageConsumer { @Resource private InstanceRegistry instanceRegistry; @Resource private StationService stationService; /** * 消费心跳 + ACK(都路由到 heartbeat 队列) */ @RabbitListener(queues = RabbitMqConfig.QUEUE_GATE_HEARTBEAT) public void onHeartbeat(String msg) { try { JSONObject json = JSON.parseObject(msg); String type = json.getString("type"); String apiKeyMd5 = json.getString("apiKeyMd5"); if ("HEARTBEAT".equals(type)) { HeartbeatMsg hb = JSON.parseObject(json.getString("payload"), HeartbeatMsg.class); instanceRegistry.update(apiKeyMd5, hb); log.debug("[Station] 心跳: {}, state={}", apiKeyMd5, hb.getState()); } else if ("CMD_ACK".equals(type)) { CmdAckMsg ack = JSON.parseObject(json.getString("payload"), CmdAckMsg.class); instanceRegistry.updateState(apiKeyMd5, ack.getNewState()); log.info("[Station] 指令确认: cmdId={}, success={}, newState={}", ack.getCommandId(), ack.isSuccess(), ack.getNewState()); } } catch (Exception e) { log.error("[Station] 心跳消费异常", e); } } /** * 消费策略事件(stats 队列,落库) */ @RabbitListener(queues = RabbitMqConfig.QUEUE_GATE_STATS) public void onStats(String msg) { try { GateStatsEvent event = JSON.parseObject(msg, GateStatsEvent.class); stationService.saveEvent(event); } catch (Exception e) { log.error("[Station] 策略事件消费异常", e); } } }