|
|
@@ -1,112 +0,0 @@
|
|
|
-package cn.reghao.autodop.dagent.machine;
|
|
|
-
|
|
|
-import cn.reghao.autodop.common.http.DefaultWebRequest;
|
|
|
-import cn.reghao.autodop.common.http.WebRequest;
|
|
|
-import cn.reghao.autodop.common.http.WebResponse;
|
|
|
-import cn.reghao.autodop.common.mqtt.DefaultMqttClient;
|
|
|
-import cn.reghao.autodop.common.msg.Message;
|
|
|
-import cn.reghao.autodop.common.msg.MsgQueue;
|
|
|
-import cn.reghao.autodop.common.msg.pub.PubMsg;
|
|
|
-import cn.reghao.autodop.common.msg.pub.clazz.NodeClazz;
|
|
|
-import cn.reghao.autodop.common.msg.pub.dto.node.NodeDTO;
|
|
|
-import cn.reghao.autodop.common.msg.pub.dto.node.constant.AppId;
|
|
|
-import cn.reghao.autodop.common.msg.pub.dto.node.constant.AppStatus;
|
|
|
-import cn.reghao.autodop.common.util.thread.ThreadPoolWrapper;
|
|
|
-import cn.reghao.jdkutil.MachineId;
|
|
|
-import cn.reghao.jdkutil.serializer.JsonConverter;
|
|
|
-import com.google.gson.JsonObject;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.concurrent.ScheduledExecutorService;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author reghao
|
|
|
- * @date 2021-09-03 09:22:42
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Component
|
|
|
-public class NodeOpsImpl {
|
|
|
- private final String nodeId;
|
|
|
- private final AppId appId;
|
|
|
- private final String topic;
|
|
|
- private final ScheduledExecutorService heartbeatScheduler;
|
|
|
- private final WebRequest webRequest;
|
|
|
- private final DefaultMqttClient mqttClient;
|
|
|
-
|
|
|
- public NodeOpsImpl(DefaultMqttClient mqttClient) throws IOException {
|
|
|
- this.nodeId = MachineId.id();
|
|
|
- this.appId = AppId.dagent;
|
|
|
- this.topic = MsgQueue.dmasterTopic();
|
|
|
- this.heartbeatScheduler = ThreadPoolWrapper.scheduledThreadPool("heartbeat", 1);
|
|
|
- this.webRequest = new DefaultWebRequest();
|
|
|
- this.mqttClient = mqttClient;
|
|
|
- }
|
|
|
-
|
|
|
- public void nodeStart() {
|
|
|
- NodeDTO nodeDTO = nodeAppDTO(AppStatus.online);
|
|
|
- String jsonPayload = JsonConverter.objectToJson(nodeDTO);
|
|
|
- PubMsg pubMsg = PubMsg.pubMsg(NodeClazz.class.getSimpleName(), NodeClazz.start.name(), jsonPayload);
|
|
|
- Message message = Message.pubMessage("", "", pubMsg);
|
|
|
- try {
|
|
|
- mqttClient.pub(topic, 1, JsonConverter.objectToJson(message));
|
|
|
- } catch (MqttException e) {
|
|
|
- log.error("{}", e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void nodeShutdown() {
|
|
|
- ThreadPoolWrapper.shutdownScheduler(heartbeatScheduler);
|
|
|
- NodeDTO nodeDTO = nodeAppDTO(AppStatus.offline);
|
|
|
- String jsonPayload = JsonConverter.objectToJson(nodeDTO);
|
|
|
- PubMsg pubMsg = PubMsg.pubMsg(NodeClazz.class.getSimpleName(), NodeClazz.shutdown.name(), jsonPayload);
|
|
|
- Message message = Message.pubMessage("", "", pubMsg);
|
|
|
- try {
|
|
|
- mqttClient.pub(topic, 1, JsonConverter.objectToJson(message));
|
|
|
- } catch (MqttException e) {
|
|
|
- log.error("{}", e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void nodeHeartbeat() {
|
|
|
- // 每小时发送一次心跳
|
|
|
- heartbeatScheduler.scheduleAtFixedRate(new Heartbeat(), 10, 10, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
-
|
|
|
- public void nodeLog() {
|
|
|
- }
|
|
|
-
|
|
|
- private NodeDTO nodeAppDTO(AppStatus appStatus) {
|
|
|
- String publicIp = getPublicIp();
|
|
|
- String privateIp = MachineId.ipv4();
|
|
|
- return new NodeDTO(nodeId, appId, publicIp, privateIp, appStatus);
|
|
|
- }
|
|
|
-
|
|
|
- private String getPublicIp() {
|
|
|
- WebResponse webResponse = webRequest.get("http://ip.reghao.cn");
|
|
|
- if (webResponse.getStatusCode() != 200) {
|
|
|
- return "0.0.0.0";
|
|
|
- }
|
|
|
-
|
|
|
- JsonObject jsonObject = JsonConverter.jsonToJsonElement(webResponse.getBody()).getAsJsonObject();
|
|
|
- return jsonObject.get("ip").getAsString();
|
|
|
- }
|
|
|
-
|
|
|
- class Heartbeat implements Runnable {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- NodeDTO nodeDTO = nodeAppDTO(AppStatus.online);
|
|
|
- String jsonPayload = JsonConverter.objectToJson(nodeDTO);
|
|
|
- PubMsg pubMsg = PubMsg.pubMsg(NodeClazz.class.getSimpleName(), NodeClazz.heartbeat.name(), jsonPayload);
|
|
|
- Message message = Message.pubMessage("", "", pubMsg);
|
|
|
- try {
|
|
|
- mqttClient.pub(topic, 1, JsonConverter.objectToJson(message));
|
|
|
- } catch (MqttException e) {
|
|
|
- log.error("{}", e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|