From d5366ec67df880723e26406706fc8fa2c88e3ea9 Mon Sep 17 00:00:00 2001 From: Calvin Date: Sat, 8 Jun 2024 14:08:43 +0800 Subject: [PATCH] feature: add column Version and Public Ip on Collector page --- .../dispatch/CollectorInfoProperties.java | 49 +++++++++++++++++++ .../dispatch/entrance/CollectServer.java | 32 ++++++++++-- collector/src/main/resources/application.yml | 3 ++ .../common/entity/dto/CollectorInfo.java | 6 ++- .../common/entity/manager/Collector.java | 6 +++ .../hertzbeat/common/util/IpDomainUtil.java | 4 ++ .../scheduler/CollectorJobScheduler.java | 6 +++ .../manager/scheduler/SchedulerInit.java | 5 +- web-app/src/app/pojo/Collector.ts | 2 + .../collector/collector.component.html | 4 ++ web-app/src/assets/i18n/en-US.json | 2 + web-app/src/assets/i18n/zh-CN.json | 2 + web-app/src/assets/i18n/zh-TW.json | 2 + 13 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 collector/src/main/java/org/apache/hertzbeat/collector/dispatch/CollectorInfoProperties.java diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/CollectorInfoProperties.java b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/CollectorInfoProperties.java new file mode 100644 index 00000000000..5d6b81f54bc --- /dev/null +++ b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/CollectorInfoProperties.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.collector.dispatch; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * Collector info configuration Properties + */ +@Component +@ConfigurationProperties(prefix = CollectorInfoProperties.INFO_PREFIX) +public class CollectorInfoProperties { + protected static final String INFO_PREFIX = "collector.info"; + + private String version; + private String publicIpEnv; + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getPublicIpEnv() { + return publicIpEnv; + } + + public void setPublicIpEnv(String publicIpEnv) { + this.publicIpEnv = publicIpEnv; + } +} diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/entrance/CollectServer.java b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/entrance/CollectServer.java index 8f052432892..297b319325f 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/entrance/CollectServer.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/entrance/CollectServer.java @@ -24,6 +24,7 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.collector.dispatch.CollectorInfoProperties; import org.apache.hertzbeat.collector.dispatch.DispatchProperties; import org.apache.hertzbeat.collector.dispatch.entrance.internal.CollectJobService; import org.apache.hertzbeat.collector.dispatch.entrance.processor.CollectCyclicDataProcessor; @@ -66,10 +67,13 @@ public class CollectServer implements CommandLineRunner { private ScheduledExecutorService scheduledExecutor; + private Info info; + public CollectServer(final CollectJobService collectJobService, final TimerDispatch timerDispatch, final DispatchProperties properties, - final CommonThreadPool threadPool) { + final CommonThreadPool threadPool, + final CollectorInfoProperties infoProperties) { if (properties == null || properties.getEntrance() == null || properties.getEntrance().getNetty() == null) { log.error("init error, please config dispatch entrance netty props in application.yml"); throw new IllegalArgumentException("please config dispatch entrance netty props"); @@ -81,14 +85,15 @@ public CollectServer(final CollectJobService collectJobService, this.collectJobService = collectJobService; this.timerDispatch = timerDispatch; this.collectJobService.setCollectServer(this); - this.init(properties, threadPool); + this.init(properties, threadPool, infoProperties); } - private void init(final DispatchProperties properties, final CommonThreadPool threadPool) { + private void init(final DispatchProperties properties, final CommonThreadPool threadPool, final CollectorInfoProperties infoProperties) { NettyClientConfig nettyClientConfig = new NettyClientConfig(); DispatchProperties.EntranceProperties.NettyProperties nettyProperties = properties.getEntrance().getNetty(); nettyClientConfig.setServerHost(nettyProperties.getManagerHost()); nettyClientConfig.setServerPort(nettyProperties.getManagerPort()); + this.initInfo(infoProperties); this.remotingClient = new NettyRemotingClient(nettyClientConfig, new CollectNettyEventListener(), threadPool); this.remotingClient.registerProcessor(ClusterMsg.MessageType.HEARTBEAT, new HeartbeatProcessor()); @@ -132,6 +137,8 @@ public void onChannelActive(Channel channel) { .name(identity) .ip(IpDomainUtil.getLocalhostIp()) .mode(mode) + .publicIp(info.publicIp) + .version(info.version) // todo more info .build(); timerDispatch.goOnline(); @@ -175,4 +182,23 @@ public void onChannelIdle(Channel channel) { log.info("handle idle event triggered. collector is going offline."); } } + + private void initInfo(final CollectorInfoProperties infoProperties) { + info = new Info(); + info.setVersion(infoProperties.getVersion()); + info.setPublicIp(IpDomainUtil.getIpFromEnvOrDefault(infoProperties.getPublicIpEnv(), IpDomainUtil.getLocalhostIp())); + } + + private static class Info { + private String version; + private String publicIp; + + public void setVersion(String version) { + this.version = version; + } + + public void setPublicIp(String publicIp) { + this.publicIp = publicIp; + } + } } diff --git a/collector/src/main/resources/application.yml b/collector/src/main/resources/application.yml index 3c23dea6332..dd9b1cade15 100644 --- a/collector/src/main/resources/application.yml +++ b/collector/src/main/resources/application.yml @@ -38,6 +38,9 @@ spring: on-profile: cluster collector: + info: + version: v1.5.1 + public-ip-env: PUBLIC_IP dispatch: entrance: netty: diff --git a/common/src/main/java/org/apache/hertzbeat/common/entity/dto/CollectorInfo.java b/common/src/main/java/org/apache/hertzbeat/common/entity/dto/CollectorInfo.java index 4807ab10b34..31163f204d9 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/entity/dto/CollectorInfo.java +++ b/common/src/main/java/org/apache/hertzbeat/common/entity/dto/CollectorInfo.java @@ -40,7 +40,11 @@ public class CollectorInfo { @NotNull private String ip; - + + private String publicIp; + + private String version; + @NotNull private String mode = CommonConstants.MODE_PUBLIC; } diff --git a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Collector.java b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Collector.java index 9fa86bc10c7..7aaf818720d 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Collector.java +++ b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Collector.java @@ -64,6 +64,12 @@ public class Collector { @NotNull private String ip; + @Schema(title = "collector public ip", description = "collector remote public ip") + private String publicIp; + + @Schema(title = "collector version", description = "collector version") + private String version; + @Schema(title = "collector status: 0-online 1-offline") @Min(0) private byte status; diff --git a/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java b/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java index 598d871fc01..a29badfc1a7 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java +++ b/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java @@ -107,6 +107,10 @@ public static String getLocalhostIp() { return null; } + public static String getIpFromEnvOrDefault(String env, String defaultIp) { + return System.getenv().getOrDefault(env, defaultIp); + } + /** * * @param ipDomain ip domain diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/scheduler/CollectorJobScheduler.java b/manager/src/main/java/org/apache/hertzbeat/manager/scheduler/CollectorJobScheduler.java index ac882e89090..3346a39a0a5 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/scheduler/CollectorJobScheduler.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/scheduler/CollectorJobScheduler.java @@ -107,6 +107,8 @@ public void collectorGoOnline(String identity, CollectorInfo collectorInfo) { if (collectorInfo != null) { collector.setIp(collectorInfo.getIp()); collector.setMode(collectorInfo.getMode()); + collector.setPublicIp(collectorInfo.getPublicIp()); + collector.setVersion(collectorInfo.getVersion()); } } else { if (collectorInfo == null) { @@ -117,6 +119,8 @@ public void collectorGoOnline(String identity, CollectorInfo collectorInfo) { .name(identity) .ip(collectorInfo.getIp()) .mode(collectorInfo.getMode()) + .publicIp(collectorInfo.getPublicIp()) + .version(collectorInfo.getVersion()) .status(CommonConstants.COLLECTOR_STATUS_ONLINE) .build(); } @@ -267,6 +271,8 @@ public boolean onlineCollector(String identity) { CollectorInfo collectorInfo = CollectorInfo.builder() .name(collector.getName()) .ip(collector.getIp()) + .publicIp(collector.getPublicIp()) + .version(collector.getVersion()) .mode(collector.getMode()) .build(); this.collectorGoOnline(identity, collectorInfo); diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/scheduler/SchedulerInit.java b/manager/src/main/java/org/apache/hertzbeat/manager/scheduler/SchedulerInit.java index 29b1acc937c..58df48e7a65 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/scheduler/SchedulerInit.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/scheduler/SchedulerInit.java @@ -57,7 +57,8 @@ public class SchedulerInit implements CommandLineRunner { private CollectJobScheduling collectJobScheduling; private static final String MAIN_COLLECTOR_NODE_IP = "127.0.0.1"; - + private static final String DEFAULT_COLLECTOR_VERSION = "DEBUG"; + @Autowired private AppService appService; @@ -84,6 +85,8 @@ public void run(String... args) throws Exception { CollectorInfo collectorInfo = CollectorInfo.builder() .name(CommonConstants.MAIN_COLLECTOR_NODE) .ip(MAIN_COLLECTOR_NODE_IP) + .publicIp(MAIN_COLLECTOR_NODE_IP) + .version(DEFAULT_COLLECTOR_VERSION) .build(); collectorScheduling.collectorGoOnline(CommonConstants.MAIN_COLLECTOR_NODE, collectorInfo); // init jobs diff --git a/web-app/src/app/pojo/Collector.ts b/web-app/src/app/pojo/Collector.ts index d255e87c6ba..2fc3c72f80a 100644 --- a/web-app/src/app/pojo/Collector.ts +++ b/web-app/src/app/pojo/Collector.ts @@ -21,6 +21,8 @@ export class Collector { id!: number; name!: string; ip!: string; + publicIp!: string; + version!: string; // public or private mode!: string; // collector status: 0-online 1-offline diff --git a/web-app/src/app/routes/setting/collector/collector.component.html b/web-app/src/app/routes/setting/collector/collector.component.html index 2a824afc73a..5bbbb88d4d2 100644 --- a/web-app/src/app/routes/setting/collector/collector.component.html +++ b/web-app/src/app/routes/setting/collector/collector.component.html @@ -90,6 +90,8 @@ {{ 'collector.pinned' | i18n }} {{ 'collector.dispatched' | i18n }} {{ 'collector.ip' | i18n }} + {{ 'collector.publicIp' | i18n }} + {{ 'collector.version' | i18n }} {{ 'collector.start-time' | i18n }} {{ 'common.edit' | i18n }} @@ -130,6 +132,8 @@ {{ data.collector.ip }} + {{ data.collector.publicIp }} + {{ data.collector.version }} {{ (data.collector.gmtUpdate | date : 'YYYY-MM-dd HH:mm:ss')?.trim() }} diff --git a/web-app/src/assets/i18n/en-US.json b/web-app/src/assets/i18n/en-US.json index 3de3ee76f4d..6af0247b623 100644 --- a/web-app/src/assets/i18n/en-US.json +++ b/web-app/src/assets/i18n/en-US.json @@ -573,6 +573,8 @@ "collector.task": "Total Tasks", "collector.start-time": "Start Time", "collector.ip": "IP Address", + "collector.publicIp": "Public IP Address", + "collector.version": "Version", "collector.node": "Node Name", "collector.pinned": "Pinned Tasks", "collector.dispatched": "Dispatched Tasks", diff --git a/web-app/src/assets/i18n/zh-CN.json b/web-app/src/assets/i18n/zh-CN.json index 92326cf3c28..a5f96162576 100644 --- a/web-app/src/assets/i18n/zh-CN.json +++ b/web-app/src/assets/i18n/zh-CN.json @@ -574,6 +574,8 @@ "collector.task": "总任务数量", "collector.start-time": "启动时间", "collector.ip": "IP地址", + "collector.publicIp": "公网IP地址", + "collector.version": "版本", "collector.node": "节点名称", "collector.pinned": "固定任务", "collector.dispatched": "调度任务", diff --git a/web-app/src/assets/i18n/zh-TW.json b/web-app/src/assets/i18n/zh-TW.json index 07df1657af9..0892fe186a4 100644 --- a/web-app/src/assets/i18n/zh-TW.json +++ b/web-app/src/assets/i18n/zh-TW.json @@ -571,6 +571,8 @@ "collector.mode.private": "私有云邊模式", "collector.start-time": "啟動時間", "collector.ip": "IP地址", + "collector.publicIp": "公網IP地址", + "collector.version": "版本", "collector.node": "節點名稱", "collector.pinned": "固定任務", "collector.dispatched": "調度任務",