diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/ApiClient.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/ApiClient.java index f22a206e..959b731f 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/ApiClient.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/ApiClient.java @@ -26,8 +26,6 @@ import com.alipay.sofa.koupleless.arklet.core.ArkletComponent; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHookImpl; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHookImpl; import com.alipay.sofa.koupleless.arklet.core.util.ClassUtils; import com.google.inject.AbstractModule; import com.google.inject.Binding; @@ -49,23 +47,18 @@ @Singleton public class ApiClient implements ArkletComponent { - private static final List tunnelList = new ArrayList<>(8); + private static final List tunnelList = new ArrayList<>(8); // CUSTOM_TUNNEL_CLASS is the key of class name of custom module tunnel - private final static String CUSTOM_TUNNEL_CLASS = "koupleless.arklet.custom.tunnel.classname"; + private final static String CUSTOM_TUNNEL_CLASS = "koupleless.arklet.custom.tunnel.classname"; // CUSTOM_BASE_METADATA_HOOK_CLASS is the key of class name of custom base metadata hook, used by tunnel to get metadata of base - private final static String CUSTOM_BASE_METADATA_HOOK_CLASS = "koupleless.arklet.custom.base.metadata.classname"; - - // CUSTOM_NETWORK_INFO_HOOK_CLASS is the key of class name of custom base network info hook, used by tunnel to get local network info of base - private final static String CUSTOM_NETWORK_INFO_HOOK_CLASS = "koupleless.arklet.custom.network.info.classname"; + private final static String CUSTOM_BASE_METADATA_HOOK_CLASS = "koupleless.arklet.custom.base.metadata.classname"; @Inject - private CommandService commandService; - - private static BaseMetadataHook baseMetadataHook; + private CommandService commandService; - private static BaseNetworkInfoHook baseNetworkInfoHook; + private static BaseMetadataHook baseMetadataHook; static { Injector injector = Guice.createInjector(new TunnelGuiceModule()); @@ -82,22 +75,13 @@ public class ApiClient implements ArkletComponent { if (customBaseMetadataHookClassName != null && !customBaseMetadataHookClassName.isEmpty()) { baseMetadataHook = ClassUtils.getBaseMetadataHookImpl(customBaseMetadataHookClassName); } - - baseNetworkInfoHook = new BaseNetworkInfoHookImpl(); - - String customNetworkInfoHookClassName = EnvironmentUtils - .getProperty(CUSTOM_NETWORK_INFO_HOOK_CLASS); - - if (customNetworkInfoHookClassName != null && !customNetworkInfoHookClassName.isEmpty()) { - baseNetworkInfoHook = ClassUtils.getNetworkInfoHook(customNetworkInfoHookClassName); - } } /** {@inheritDoc} */ @Override public void init() { for (Tunnel tunnel : tunnelList) { - tunnel.init(commandService, baseMetadataHook, baseNetworkInfoHook); + tunnel.init(commandService, baseMetadataHook); tunnel.run(); } } @@ -123,10 +107,6 @@ public BaseMetadataHook getMetadataHook() { return baseMetadataHook; } - public BaseNetworkInfoHook getNetworkInfoHook() { - return baseNetworkInfoHook; - } - private static class TunnelGuiceModule extends AbstractModule { @Override protected void configure() { diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/model/Response.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/model/Response.java index 4379a2fc..489bcf72 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/model/Response.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/model/Response.java @@ -18,8 +18,6 @@ import com.alipay.sofa.koupleless.arklet.core.command.meta.Output; -import java.util.UUID; - /** *

Response class.

* @@ -50,9 +48,9 @@ public class Response { private String errorStackTrace; /** - * baseID + * baseIdentity */ - private String baseID; + private String baseIdentity; /** *

fromCommandOutput.

@@ -60,12 +58,12 @@ public class Response { * @param output a {@link com.alipay.sofa.koupleless.arklet.core.command.meta.Output} object * @return a {@link com.alipay.sofa.koupleless.arklet.core.api.model.Response} object */ - public static Response fromCommandOutput(Output output, String baseID) { + public static Response fromCommandOutput(Output output, String baseIdentity) { Response response = new Response(); response.code = output.getCode(); response.data = output.getData(); response.message = output.getMessage(); - response.baseID = baseID; + response.baseIdentity = baseIdentity; return response; } @@ -195,20 +193,20 @@ public void setErrorStackTrace(String errorStackTrace) { } /** - * Getter method for property baseID. + * Getter method for property baseIdentity. * - * @return property value of baseID + * @return property value of baseIdentity */ - public String getBaseID() { - return baseID; + public String getBaseIdentity() { + return baseIdentity; } /** - * Setter method for property baseID. + * Setter method for property baseIdentity. * - * @param baseID value to be assigned to property baseID + * @param baseIdentity value to be assigned to property baseIdentity */ - public void setBaseID(String baseID) { - this.baseID = baseID; + public void setBaseIdentity(String baseIdentity) { + this.baseIdentity = baseIdentity; } } diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/Tunnel.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/Tunnel.java index 627a0cad..e72bb11c 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/Tunnel.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/Tunnel.java @@ -18,9 +18,6 @@ import com.alipay.sofa.koupleless.arklet.core.command.CommandService; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; - -import java.util.UUID; /** *

Tunnel interface.

@@ -36,8 +33,7 @@ public interface Tunnel { * * @param commandService a {@link com.alipay.sofa.koupleless.arklet.core.command.CommandService} object */ - void init(CommandService commandService, BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook); + void init(CommandService commandService, BaseMetadataHook baseMetadataHook); /** *

run.

diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/HeartBeatScheduledMission.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/HeartBeatScheduledMission.java new file mode 100644 index 00000000..20ae353a --- /dev/null +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/HeartBeatScheduledMission.java @@ -0,0 +1,103 @@ +/* + * 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 com.alipay.sofa.koupleless.arklet.core.api.tunnel.http; + +import com.alibaba.fastjson.JSONObject; +import com.alipay.sofa.ark.spi.model.BizState; +import com.alipay.sofa.koupleless.arklet.core.common.exception.ArkletRuntimeException; +import com.alipay.sofa.koupleless.arklet.core.common.log.ArkletLogger; +import com.alipay.sofa.koupleless.arklet.core.common.log.ArkletLoggerFactory; +import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; +import com.alipay.sofa.koupleless.arklet.core.common.model.BaseStatus; +import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +class HeartBeatScheduledMission implements Runnable { + + private static final ArkletLogger LOGGER = ArkletLoggerFactory.getDefaultLogger(); + + private final String heartBeatEndpoint; + private int port; + private final BaseMetadataHook baseMetadataHook; + + public HeartBeatScheduledMission(int port, String heartBeatEndpoint, + BaseMetadataHook baseMetadataHook) { + this.port = port; + this.heartBeatEndpoint = heartBeatEndpoint; + this.baseMetadataHook = baseMetadataHook; + } + + private void sendHeartBeatMessage(BaseStatus baseStatus) throws ArkletRuntimeException { + String body = JSONObject.toJSONString(baseStatus); + HttpURLConnection conn = null; + try { + LOGGER.debug("Heartbeat message sent successfully. {}", body); + conn = getHttpURLConnection(); + try (OutputStream out = conn.getOutputStream()) { + out.write(body.getBytes()); + out.flush(); + } + if (200 != conn.getResponseCode()) { + LOGGER.error("ResponseCode is an error code:{}", conn.getResponseCode()); + } + } catch (Exception e) { + LOGGER.error("sendHeartBeatMessage failed", e); + throw new ArkletRuntimeException(e); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + } + + private HttpURLConnection getHttpURLConnection() throws IOException { + URL url = new URL(heartBeatEndpoint + "/heartbeat"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + //发送POST请求必须设置为true + conn.setDoOutput(true); + conn.setDoInput(true); + //设置连接超时时间和读取超时时间 + conn.setConnectTimeout(30000); + conn.setReadTimeout(10000); + conn.setRequestProperty("Content-Type", "application/json"); + return conn; + } + + @Override + public void run() { + try { + // send heart beat message + BaseMetadata baseMetadata = BaseMetadata.builder() + .identity(baseMetadataHook.getIdentity()) + .clusterName(baseMetadataHook.getClusterName()).build(); + BaseStatus baseStatus = BaseStatus.builder().baseMetadata(baseMetadata) + .localIP(baseMetadataHook.getLocalIP()) + .localHostName(baseMetadataHook.getLocalHostName()) + .state(BizState.ACTIVATED.getBizState()).port(port).build(); + + sendHeartBeatMessage(baseStatus); + } catch (Exception e) { + LOGGER.error("Exception occurred during heartbeat execution", e); + } + + } +} diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/HttpTunnel.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/HttpTunnel.java index a9200b83..6d1ac45b 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/HttpTunnel.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/HttpTunnel.java @@ -16,24 +16,15 @@ */ package com.alipay.sofa.koupleless.arklet.core.api.tunnel.http; -import java.io.IOException; -import java.net.URL; -import java.net.HttpURLConnection; -import java.io.OutputStream; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import com.alibaba.fastjson.JSONObject; import com.alipay.sofa.ark.common.util.AssertUtils; import com.alipay.sofa.ark.common.util.EnvironmentUtils; import com.alipay.sofa.ark.common.util.PortSelectUtils; import com.alipay.sofa.ark.common.util.StringUtils; -import com.alipay.sofa.ark.spi.model.BizState; import com.alipay.sofa.koupleless.arklet.core.api.tunnel.Tunnel; import com.alipay.sofa.koupleless.arklet.core.api.tunnel.http.netty.NettyHttpServer; import com.alipay.sofa.koupleless.arklet.core.command.CommandService; @@ -41,14 +32,9 @@ import com.alipay.sofa.koupleless.arklet.core.common.exception.ArkletRuntimeException; import com.alipay.sofa.koupleless.arklet.core.common.log.ArkletLogger; import com.alipay.sofa.koupleless.arklet.core.common.log.ArkletLoggerFactory; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseNetworkInfo; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; import com.google.inject.Singleton; -import static com.alipay.sofa.koupleless.arklet.core.health.model.Constants.*; - /** *

HttpTunnel class.

* @@ -79,8 +65,7 @@ public class HttpTunnel implements Tunnel { /** {@inheritDoc} */ @Override - public void init(CommandService commandService, BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook) { + public void init(CommandService commandService, BaseMetadataHook baseMetadataHook) { if (init.compareAndSet(false, true)) { this.commandService = commandService; this.baseMetadataHook = baseMetadataHook; @@ -101,9 +86,9 @@ public void init(CommandService commandService, BaseMetadataHook baseMetadataHoo if (!StringUtils.isEmpty(heartBeatEndpoint)) { heartBeatExecutor = Executors.newScheduledThreadPool(1); - heartBeatExecutor.scheduleAtFixedRate(new HeartBeatScheduledMission(port, - heartBeatEndpoint, baseMetadataHook, baseNetworkInfoHook), 0, 120000L, - TimeUnit.MILLISECONDS); + heartBeatExecutor.scheduleAtFixedRate( + new HeartBeatScheduledMission(port, heartBeatEndpoint, baseMetadataHook), 0, + 120000L, TimeUnit.MILLISECONDS); } LOGGER.info("http tunnel initialized: {}", this); @@ -118,7 +103,7 @@ public void run() { try { LOGGER.info("http tunnel listening on port: " + port); nettyHttpServer = new NettyHttpServer(port, commandService); - nettyHttpServer.open(baseMetadataHook.getBaseID()); + nettyHttpServer.open(baseMetadataHook.getIdentity()); } catch (InterruptedException e) { LOGGER.error("Unable to open netty schedule http server.", e); throw new ArkletRuntimeException(e); @@ -145,88 +130,4 @@ public void shutdown() { } } } - - static class HeartBeatScheduledMission implements Runnable { - - private final String heartBeatEndpoint; - private int port; - private final BaseMetadataHook baseMetadataHook; - private final BaseNetworkInfoHook baseNetworkInfoHook; - - public HeartBeatScheduledMission(int port, String heartBeatEndpoint, - BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook) { - this.port = port; - this.heartBeatEndpoint = heartBeatEndpoint; - this.baseMetadataHook = baseMetadataHook; - this.baseNetworkInfoHook = baseNetworkInfoHook; - } - - private void sendHeartBeatMessage(Map heartBeatData) throws ArkletRuntimeException { - String body = JSONObject.toJSONString(heartBeatData); - HttpURLConnection conn = null; - try { - LOGGER.debug("Heartbeat message sent successfully. {}", body); - conn = getHttpURLConnection(); - try (OutputStream out = conn.getOutputStream()) { - out.write(body.getBytes()); - out.flush(); - } - if (200 != conn.getResponseCode()) { - LOGGER.error("ResponseCode is an error code:{}", conn.getResponseCode()); - } - } catch (Exception e) { - LOGGER.error("sendHeartBeatMessage failed", e); - throw new ArkletRuntimeException(e); - } finally { - if (conn != null) { - conn.disconnect(); - } - } - } - - private HttpURLConnection getHttpURLConnection() throws IOException { - URL url = new URL(heartBeatEndpoint + "/heartbeat"); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("POST"); - //发送POST请求必须设置为true - conn.setDoOutput(true); - conn.setDoInput(true); - //设置连接超时时间和读取超时时间 - conn.setConnectTimeout(30000); - conn.setReadTimeout(10000); - conn.setRequestProperty("Content-Type", "application/json"); - return conn; - } - - @Override - public void run() { - try { - // send heart beat message - Map heartBeatData = new HashMap<>(); - - heartBeatData.put(BASE_ID, baseMetadataHook.getBaseID()); - - BaseMetadata metadata = baseMetadataHook.getBaseMetadata(); - heartBeatData.put(MASTER_BIZ_INFO, metadata); - - Map networkInfo = new HashMap<>(); - - BaseNetworkInfo baseNetworkInfo = baseNetworkInfoHook.getNetworkInfo(); - - networkInfo.put(LOCAL_IP, baseNetworkInfo.getLocalIP()); - networkInfo.put(LOCAL_HOST_NAME, baseNetworkInfo.getLocalHostName()); - networkInfo.put(ARKLET_PORT, port); - - heartBeatData.put(NETWORK_INFO, networkInfo); - heartBeatData.put(STATE, BizState.ACTIVATED); - - sendHeartBeatMessage(heartBeatData); - } catch (Exception e) { - LOGGER.error("Exception occurred during heartbeat execution", e); - } - - } - } - } diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/netty/NettyHttpServer.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/netty/NettyHttpServer.java index 8bac5912..8fae0f0c 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/netty/NettyHttpServer.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/api/tunnel/http/netty/NettyHttpServer.java @@ -19,7 +19,6 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.UUID; import com.alibaba.fastjson.JSONObject; @@ -106,12 +105,12 @@ public Channel getChannel() { * * @throws java.lang.InterruptedException if any. */ - public void open(String baseID) throws InterruptedException { + public void open(String baseIdentity) throws InterruptedException { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024); serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) - .childHandler(new NettyHttpInitializer(commandService, baseID)); + .childHandler(new NettyHttpInitializer(commandService, baseIdentity)); channel = serverBootstrap.bind(port).sync().channel(); } @@ -127,11 +126,11 @@ public void close() { static class NettyHttpInitializer extends ChannelInitializer { private final CommandService commandService; - private final String baseID; + private final String baseIdentity; - public NettyHttpInitializer(CommandService commandService, String baseID) { + public NettyHttpInitializer(CommandService commandService, String baseIdentity) { this.commandService = commandService; - this.baseID = baseID; + this.baseIdentity = baseIdentity; } @Override @@ -141,7 +140,7 @@ protected void initChannel(SocketChannel channel) throws Exception { pipeline.addLast(new HttpRequestDecoder()); pipeline.addLast(new HttpObjectAggregator(1024 * 1024)); pipeline.addLast(new HttpServerExpectContinueHandler()); - pipeline.addLast(new NettyHttpHandler(commandService, baseID)); + pipeline.addLast(new NettyHttpHandler(commandService, baseIdentity)); } } @@ -149,11 +148,11 @@ protected void initChannel(SocketChannel channel) throws Exception { public static class NettyHttpHandler extends SimpleChannelInboundHandler { private final CommandService commandService; - private final String baseID; + private final String baseIdentity; - public NettyHttpHandler(CommandService commandService, String baseID) { + public NettyHttpHandler(CommandService commandService, String baseIdentity) { this.commandService = commandService; - this.baseID = baseID; + this.baseIdentity = baseIdentity; } @Override @@ -169,7 +168,7 @@ protected void channelRead0(ChannelHandlerContext ctx, } Output output = commandService.process(validation.getCmd(), validation.getCmdContent()); - Response response = Response.fromCommandOutput(output, baseID); + Response response = Response.fromCommandOutput(output, baseIdentity); returnResponse(ctx, response); } } catch (Throwable e) { diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseMetadata.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseMetadata.java index 708eb20d..5e5bbf7a 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseMetadata.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseMetadata.java @@ -18,6 +18,8 @@ import lombok.*; +import java.io.Serializable; + /** * @author dongnan * @version $Id: BaseMetadata.java, v 0.1 2024年07月01日 20:02 dongnan Exp $ @@ -27,16 +29,21 @@ @AllArgsConstructor @Data @Builder -public class BaseMetadata { +public class BaseMetadata implements Serializable { + private static final long serialVersionUID = 1L; /** * runtime base name */ - private String name; + private String identity; /** * runtime base version */ - private String version; + private String version; + /** + * cluster name for this base instance + */ + private String clusterName; } diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseNetworkInfo.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseStatus.java similarity index 64% rename from arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseNetworkInfo.java rename to arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseStatus.java index aabae533..2244ee25 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseNetworkInfo.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/common/model/BaseStatus.java @@ -16,44 +16,39 @@ */ package com.alipay.sofa.koupleless.arklet.core.common.model; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; -/** - * @author dongnan - * @version $Id: NetworkInfo.java, v 0.1 2024年10月24日 20:02 dongnan Exp $ - */ +import java.io.Serializable; @NoArgsConstructor @AllArgsConstructor @Data @Builder -public class BaseNetworkInfo { +public class BaseStatus implements Serializable { + private static final long serialVersionUID = 1L; /** - * runtime localIP + * base metadata */ - private String localIP; + private BaseMetadata baseMetadata; /** - * runtime local host name + * runtime localIP */ - private String localHostName; + private String localIP; /** - * Getter method for property localIP. - * - * @return property value of localIP + * runtime local host name */ - public String getLocalIP() { - return localIP; - } + private String localHostName; /** - * Getter method for property localHostName. - * - * @return property value of localHostName + * for communicate with devops to send the heart beat message */ - public String getLocalHostName() { - return localHostName; - } + private int port; + + private String state; } diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/health/model/Constants.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/health/model/Constants.java index 66a21703..ea780d15 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/health/model/Constants.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/health/model/Constants.java @@ -54,30 +54,12 @@ public class Constants { /** Constant BIZ_INFO="bizInfo" */ public static final String BIZ_INFO = "bizInfo"; - /** Constant BASE_ID="baseID" */ - public static final String BASE_ID = "baseID"; - - /** Constant STATE="state" */ - public static final String STATE = "state"; - /** Constant BIZ_NAME="bizName" */ public static final String BIZ_NAME = "bizName"; /** Constant BIZ_VERSION="bizVersion" */ public static final String BIZ_VERSION = "bizVersion"; - /** Constant NETWORK_INFO="networkInfo" */ - public static final String NETWORK_INFO = "networkInfo"; - - /** Constant LOCAL_IP="localIP" */ - public static final String LOCAL_IP = "localIP"; - - /** Constant LOCAL_HOST_NAME="localHostName" */ - public static final String LOCAL_HOST_NAME = "localHostName"; - - /** Constant ARKLET_PORT="arkletPort" */ - public static final String ARKLET_PORT = "arkletPort"; - /** Constant BIZ_LIST_INFO="bizListInfo" */ public static final String BIZ_LIST_INFO = "bizListInfo"; diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/base/BaseMetadataHook.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/base/BaseMetadataHook.java index a4448c1e..43e2a214 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/base/BaseMetadataHook.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/base/BaseMetadataHook.java @@ -16,8 +16,6 @@ */ package com.alipay.sofa.koupleless.arklet.core.hook.base; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; - /** *

MetadataHook interface.

* @@ -26,11 +24,35 @@ * @version 1.0.0 */ public interface BaseMetadataHook { + /** + * name for this base instance, should be unique + * @return + */ + String getIdentity(); - BaseMetadata getBaseMetadata(); + String getVersion(); + /** + * env for this base instance + * @return + */ String getRuntimeEnv(); - String getBaseID(); + /** + * cluster name for this base instance + * @return + */ + String getClusterName(); + + /** + * local ip + * @return + */ + String getLocalIP(); + /** + * local host name + * @return + */ + String getLocalHostName(); } diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/base/BaseMetadataHookImpl.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/base/BaseMetadataHookImpl.java index 57f3916b..ad418d5e 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/base/BaseMetadataHookImpl.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/base/BaseMetadataHookImpl.java @@ -16,10 +16,14 @@ */ package com.alipay.sofa.koupleless.arklet.core.hook.base; -import com.alipay.sofa.ark.common.util.EnvironmentUtils; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; +import com.alipay.sofa.koupleless.arklet.core.common.exception.ArkletInitException; +import com.alipay.sofa.koupleless.common.util.RandomUtils; -import java.util.UUID; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; /** *

BaseMetadataHookImpl.

@@ -30,32 +34,60 @@ */ public class BaseMetadataHookImpl implements BaseMetadataHook { - final String defaultNameEnvKey = "koupleless.arklet.metadata.name"; - final String defaultVersionEnvKey = "koupleless.arklet.metadata.version"; - final String defaultEnvKey = "koupleless.arklet.metadata.env"; + private final String UNNKOWN = "unknown"; + private InetAddress localHost = null; - final UUID baseID = UUID.randomUUID(); + { + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + NetworkInterface networkInterface = interfaces.nextElement(); + Enumeration addresses = networkInterface.getInetAddresses(); + while (addresses.hasMoreElements()) { + InetAddress address = addresses.nextElement(); + if (!address.isLoopbackAddress() && address instanceof Inet4Address) { + // find first non-loopback ipv4 address + localHost = address; + break; + } + } + } + } catch (SocketException e) { + throw new ArkletInitException("getLocalNetworkInfo error", e); + } + } - private String getName() { - return EnvironmentUtils.getProperty(defaultNameEnvKey); + @Override + public String getIdentity() { + String ip = getLocalIP(); + if (!UNNKOWN.equals(ip)) { + return ip; + } + return UNNKOWN + "-" + RandomUtils.generateRandomString(5); } - private String getVersion() { - return EnvironmentUtils.getProperty(defaultVersionEnvKey); + public String getVersion() { + return "1.0.0"; } @Override - public BaseMetadata getBaseMetadata() { - return BaseMetadata.builder().name(getName()).version(getVersion()).build(); + public String getRuntimeEnv() { + return "default"; } @Override - public String getRuntimeEnv() { - return EnvironmentUtils.getProperty(defaultEnvKey); + public String getClusterName() { + // TODO: add app name + return "default"; + } + + @Override + public String getLocalIP() { + return localHost != null ? localHost.getHostAddress() : "unknown"; } @Override - public String getBaseID() { - return baseID.toString(); + public String getLocalHostName() { + return localHost != null ? localHost.getHostName() : "unknown"; } } diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/network/BaseNetworkInfoHook.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/network/BaseNetworkInfoHook.java deleted file mode 100644 index 6e5fbae9..00000000 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/network/BaseNetworkInfoHook.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 com.alipay.sofa.koupleless.arklet.core.hook.network; - -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseNetworkInfo; - -/** - *

NetworkHook interface.

- * - * @author dongnan - * @since 2024/10/24 - * @version 1.0.0 - */ -public interface BaseNetworkInfoHook { - - BaseNetworkInfo getNetworkInfo(); - -} diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/network/BaseNetworkInfoHookImpl.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/network/BaseNetworkInfoHookImpl.java deleted file mode 100644 index 8b906b92..00000000 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/hook/network/BaseNetworkInfoHookImpl.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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 com.alipay.sofa.koupleless.arklet.core.hook.network; - -import com.alipay.sofa.koupleless.arklet.core.common.exception.ArkletInitException; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseNetworkInfo; - -import java.net.*; -import java.util.Enumeration; - -/** - *

NetworkInfoHookImpl.

- * - * @author dongnan - * @since 2024/10/24 - * @version 1.0.0 - */ -public class BaseNetworkInfoHookImpl implements BaseNetworkInfoHook { - - private InetAddress localHost = null; - - { - try { - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - while (interfaces.hasMoreElements()) { - NetworkInterface networkInterface = interfaces.nextElement(); - Enumeration addresses = networkInterface.getInetAddresses(); - while (addresses.hasMoreElements()) { - InetAddress address = addresses.nextElement(); - if (!address.isLoopbackAddress() && address instanceof Inet4Address) { - // find first non-loopback ipv4 address - localHost = address; - break; - } - } - } - } catch (SocketException e) { - throw new ArkletInitException("getLocalNetworkInfo error", e); - } - } - - @Override - public BaseNetworkInfo getNetworkInfo() { - String localIP = "unknown"; - String localHostName = "unknown"; - if (localHost != null) { - localIP = localHost.getHostAddress(); - localHostName = localHost.getHostName(); - } - return BaseNetworkInfo.builder().localIP(localIP).localHostName(localHostName).build(); - } - -} diff --git a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/util/ClassUtils.java b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/util/ClassUtils.java index 38c847ad..3b5522b2 100644 --- a/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/util/ClassUtils.java +++ b/arklet-core/src/main/java/com/alipay/sofa/koupleless/arklet/core/util/ClassUtils.java @@ -21,7 +21,6 @@ import com.alipay.sofa.koupleless.arklet.core.common.log.ArkletLogger; import com.alipay.sofa.koupleless.arklet.core.common.log.ArkletLoggerFactory; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; import java.lang.reflect.InvocationTargetException; @@ -85,36 +84,4 @@ public static BaseMetadataHook getBaseMetadataHookImpl(String baseMetadataHookCl return hookInstance; } - - /** - * Validate current object must be null - * - * @param networkInfoHookClassName String custom network info hook class name - */ - public static BaseNetworkInfoHook getNetworkInfoHook(String networkInfoHookClassName) throws ArkletInitException { - Class hookClass; - try { - hookClass = Class.forName(networkInfoHookClassName); - } catch (ClassNotFoundException e) { - LOGGER.error("custom network info hook class not found"); - throw new ArkletInitException("custom network info hook class found", e); - } - if (!BaseNetworkInfoHook.class.isAssignableFrom(hookClass)) { - LOGGER.error("custom network info hook class didn't implement Metadata interface"); - throw new ArkletInitException( - "custom network info hook class didn't implement tunnel interface"); - } - BaseNetworkInfoHook hookInstance; - try { - hookInstance = (BaseNetworkInfoHook) hookClass.getDeclaredConstructor().newInstance(); - } catch (InstantiationException | IllegalAccessException | NoSuchMethodException - | InvocationTargetException e) { - LOGGER.error("Failed to instantiate the custom network info hook class", e); - throw new ArkletInitException( - "Failed to instantiate the custom network info hook class", e); - } - - return hookInstance; - } - } diff --git a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/BaseTest.java b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/BaseTest.java index 47508fe7..42040e23 100644 --- a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/BaseTest.java +++ b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/BaseTest.java @@ -18,7 +18,6 @@ import com.alipay.sofa.koupleless.arklet.core.command.CommandService; import com.alipay.sofa.koupleless.arklet.core.health.HealthService; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; import com.alipay.sofa.koupleless.arklet.core.ops.UnifiedOperationService; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; import org.junit.Before; @@ -47,16 +46,12 @@ public class BaseTest { @Mock public static BaseMetadataHook baseMetadataHook; - @Mock - public static BaseNetworkInfoHook networkInfoHook; - @Before public void setup() { commandService = ArkletComponentRegistry.getCommandServiceInstance(); operationService = ArkletComponentRegistry.getOperationServiceInstance(); healthService = ArkletComponentRegistry.getHealthServiceInstance(); baseMetadataHook = ArkletComponentRegistry.getApiClientInstance().getMetadataHook(); - networkInfoHook = ArkletComponentRegistry.getApiClientInstance().getNetworkInfoHook(); } } diff --git a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/network/BaseNetworkInfoHookTest.java b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hook/BaseMetadataHookTest.java similarity index 76% rename from arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/network/BaseNetworkInfoHookTest.java rename to arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hook/BaseMetadataHookTest.java index 02fe5843..47a89742 100644 --- a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/network/BaseNetworkInfoHookTest.java +++ b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hook/BaseMetadataHookTest.java @@ -14,28 +14,39 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.koupleless.arklet.core.hooks.network; +package com.alipay.sofa.koupleless.arklet.core.hook; import com.alipay.sofa.koupleless.arklet.core.BaseTest; import com.alipay.sofa.koupleless.arklet.core.common.exception.ArkletInitException; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseNetworkInfo; import org.junit.Assert; import org.junit.Test; -import java.net.*; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; /** * @author dongnan - * @since 2024/10/24 + * @since 2024/09/04 */ -public class BaseNetworkInfoHookTest extends BaseTest { +public class BaseMetadataHookTest extends BaseTest { + @Test + public void getRuntimeEnv() { + String env = baseMetadataHook.getRuntimeEnv(); + Assert.assertEquals("default", env); + } + + @Test + public void getName() { + baseMetadataHook.getIdentity(); + } + @Test public void getNetworkInfo() { - BaseNetworkInfo networkInfo = networkInfoHook.getNetworkInfo(); Map networkInfoMap = new HashMap<>(); try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); @@ -52,7 +63,7 @@ public void getNetworkInfo() { } catch (SocketException e) { throw new ArkletInitException("getLocalNetworkInfo error", e); } - Assert.assertEquals(networkInfoMap.get(networkInfo.getLocalIP()), - networkInfo.getLocalHostName()); + Assert.assertEquals(networkInfoMap.get(baseMetadataHook.getLocalIP()), + baseMetadataHook.getLocalHostName()); } } diff --git a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/metadata/MockBaseMetadataHook.java b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hook/MockBaseMetadataHookImpl.java similarity index 68% rename from arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/metadata/MockBaseMetadataHook.java rename to arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hook/MockBaseMetadataHookImpl.java index 49e44fd6..c34b6b96 100644 --- a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/metadata/MockBaseMetadataHook.java +++ b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hook/MockBaseMetadataHookImpl.java @@ -14,21 +14,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.koupleless.arklet.core.hooks.metadata; +package com.alipay.sofa.koupleless.arklet.core.hook; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import java.util.UUID; +public class MockBaseMetadataHookImpl implements BaseMetadataHook { + @Override + public String getIdentity() { + return ""; + } -/** - * @author 冬喃 - * @version : MockBaseMetadataHook, v 0.1 2024-09-04 下午3:28 dongnan Exp $ - */ -public class MockBaseMetadataHook implements BaseMetadataHook { @Override - public BaseMetadata getBaseMetadata() { - return null; + public String getVersion() { + return ""; } @Override @@ -37,7 +35,17 @@ public String getRuntimeEnv() { } @Override - public String getBaseID() { - return UUID.randomUUID().toString(); + public String getClusterName() { + return ""; + } + + @Override + public String getLocalIP() { + return ""; + } + + @Override + public String getLocalHostName() { + return ""; } } diff --git a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/metadata/MockBaseMetadataHookNotImpl.java b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hook/MockBaseMetadataHookNotImpl.java similarity index 83% rename from arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/metadata/MockBaseMetadataHookNotImpl.java rename to arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hook/MockBaseMetadataHookNotImpl.java index e3c7af29..1c6cfad1 100644 --- a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/metadata/MockBaseMetadataHookNotImpl.java +++ b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hook/MockBaseMetadataHookNotImpl.java @@ -14,11 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.koupleless.arklet.core.hooks.metadata; +package com.alipay.sofa.koupleless.arklet.core.hook; -/** - * @author 冬喃 - * @version : MockBaseMetadataHook, v 0.1 2024-09-04 下午3:28 dongnan Exp $ - */ public class MockBaseMetadataHookNotImpl { } diff --git a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/metadata/BaseMetadataHookTest.java b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/metadata/BaseMetadataHookTest.java deleted file mode 100644 index e0c14a15..00000000 --- a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/metadata/BaseMetadataHookTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 com.alipay.sofa.koupleless.arklet.core.hooks.metadata; - -import com.alipay.sofa.koupleless.arklet.core.BaseTest; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; -import org.junit.Assert; -import org.junit.Test; - -import java.util.UUID; - -/** - * @author dongnan - * @since 2024/09/04 - */ -public class BaseMetadataHookTest extends BaseTest { - @Test - public void getBaseMetadata() { - System.setProperty("koupleless.arklet.metadata.name", "test_metadata_hook"); - System.setProperty("koupleless.arklet.metadata.version", "test_metadata_hook_version"); - BaseMetadata metadata = baseMetadataHook.getBaseMetadata(); - Assert.assertEquals("test_metadata_hook", metadata.getName()); - Assert.assertEquals("test_metadata_hook_version", metadata.getVersion()); - } - - @Test - public void getRuntimeEnv() { - System.setProperty("koupleless.arklet.metadata.env", "test_env"); - String env = baseMetadataHook.getRuntimeEnv(); - Assert.assertEquals("test_env", env); - } - - @Test - public void getBaseID() { - UUID.fromString(baseMetadataHook.getBaseID()); - } -} diff --git a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/network/MockBaseNetworkInfoHook.java b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/network/MockBaseNetworkInfoHook.java deleted file mode 100644 index 896854fb..00000000 --- a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/network/MockBaseNetworkInfoHook.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 com.alipay.sofa.koupleless.arklet.core.hooks.network; - -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseNetworkInfo; -import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; -import com.google.common.graph.Network; - -/** - * @author 冬喃 - * @version : MockBaseNetworkInfoHook, v 0.1 2024-10-24 下午3:28 dongnan Exp $ - */ -public class MockBaseNetworkInfoHook implements BaseNetworkInfoHook { - - @Override - public BaseNetworkInfo getNetworkInfo() { - return BaseNetworkInfo.builder().build(); - } -} diff --git a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/tunnel/custom/MockTunnel.java b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/tunnel/custom/MockTunnel.java index f5c497ed..81af0136 100644 --- a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/tunnel/custom/MockTunnel.java +++ b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/tunnel/custom/MockTunnel.java @@ -19,7 +19,6 @@ import com.alipay.sofa.koupleless.arklet.core.api.tunnel.Tunnel; import com.alipay.sofa.koupleless.arklet.core.command.CommandService; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; import java.util.UUID; @@ -30,8 +29,7 @@ public class MockTunnel implements Tunnel { @Override - public void init(CommandService commandService, BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook) { + public void init(CommandService commandService, BaseMetadataHook baseMetadataHook) { } @Override diff --git a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/util/ClassUtilsTest.java b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/util/ClassUtilsTest.java index d0db7097..f16c42a7 100644 --- a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/util/ClassUtilsTest.java +++ b/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/util/ClassUtilsTest.java @@ -42,36 +42,18 @@ public void testGetCustomTunnelClass_NotExist() { @Test public void testGetCustomBaseMetadataHookClass_Success() { ClassUtils.getBaseMetadataHookImpl( - "com.alipay.sofa.koupleless.arklet.core.hooks.metadata.MockBaseMetadataHook"); + "com.alipay.sofa.koupleless.arklet.core.hook.MockBaseMetadataHookImpl"); } @Test(expected = ArkletInitException.class) public void testGetCustomBaseMetadataHook_NotImplementTunnel() { ClassUtils.getBaseMetadataHookImpl( - "com.alipay.sofa.koupleless.arklet.core.hooks.metadata.MockBaseMetadataHookNotImpl"); + "com.alipay.sofa.koupleless.arklet.core.hook.MockBaseMetadataHookNotImpl"); } @Test(expected = ArkletInitException.class) public void testGetCustomBaseMetadataHook_NotExist() { ClassUtils.getBaseMetadataHookImpl( - "com.alipay.sofa.koupleless.arklet.core.hooks.metadata.MockBaseMetadataHookNotExist"); - } - - @Test - public void testGetCustomBaseNetworkInfoHookClass_Success() { - ClassUtils.getNetworkInfoHook( - "com.alipay.sofa.koupleless.arklet.core.hooks.network.MockBaseNetworkInfoHook"); - } - - @Test(expected = ArkletInitException.class) - public void testGetCustomBaseNetworkInfoHookClass_NotImplementTunnel() { - ClassUtils.getNetworkInfoHook( - "com.alipay.sofa.koupleless.arklet.core.hooks.network.MockBaseNetworkInfoHookNotImpl"); - } - - @Test(expected = ArkletInitException.class) - public void testGetCustomBaseNetworkInfoHookClass_NotExist() { - ClassUtils.getNetworkInfoHook( - "com.alipay.sofa.koupleless.arklet.core.hooks.network.MockBaseNetworkInfoHookNotExist"); + "com.alipay.sofa.koupleless.arklet.core.hook.MockBaseMetadataHookNotExist"); } } diff --git a/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/MqttTunnel.java b/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/MqttTunnel.java index 92a4bf49..e5031817 100644 --- a/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/MqttTunnel.java +++ b/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/MqttTunnel.java @@ -21,7 +21,6 @@ import com.alipay.sofa.ark.common.util.StringUtils; import com.alipay.sofa.koupleless.arklet.core.api.tunnel.Tunnel; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; import com.alipay.sofa.koupleless.arklet.tunnel.mqtt.paho.PahoMqttClient; import com.alipay.sofa.koupleless.arklet.core.command.CommandService; import com.alipay.sofa.koupleless.arklet.core.common.exception.ArkletInitException; @@ -31,7 +30,6 @@ import com.google.inject.Singleton; import org.eclipse.paho.client.mqttv3.MqttException; -import java.util.UUID; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -47,7 +45,6 @@ public class MqttTunnel implements Tunnel { private static final ArkletLogger LOGGER = ArkletLoggerFactory .getDefaultLogger(); - private String baseID; private final static String MQTT_ENABLE_ATTRIBUTE = "koupleless.arklet.mqtt.enable"; private final static String MQTT_BROKER_ATTRIBUTE = "koupleless.arklet.mqtt.broker"; private final static String MQTT_PORT_ATTRIBUTE = "koupleless.arklet.mqtt.port"; @@ -67,7 +64,6 @@ public class MqttTunnel implements Tunnel { private com.alipay.sofa.koupleless.arklet.core.command.CommandService commandService; private BaseMetadataHook baseMetadataHook; - private BaseNetworkInfoHook baseNetworkInfoHook; private boolean enable = false; private int port; private String brokerUrl; @@ -80,19 +76,16 @@ public class MqttTunnel implements Tunnel { /** {@inheritDoc} */ @Override - public void init(CommandService commandService, BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook) { + public void init(CommandService commandService, BaseMetadataHook baseMetadataHook) { if (init.compareAndSet(false, true)) { String enable = EnvironmentUtils.getProperty(MQTT_ENABLE_ATTRIBUTE); if (enable == null || !enable.equals("true")) { // mqtt not enable, return return; } - this.baseID = baseMetadataHook.getBaseID(); this.enable = true; this.commandService = commandService; this.baseMetadataHook = baseMetadataHook; - this.baseNetworkInfoHook = baseNetworkInfoHook; String brokerPort = EnvironmentUtils.getProperty(MQTT_PORT_ATTRIBUTE); this.brokerUrl = EnvironmentUtils.getProperty(MQTT_BROKER_ATTRIBUTE); @@ -163,11 +156,11 @@ public void run() { // init mqtt client with ca and client crt pahoMqttClient = new PahoMqttClient(brokerUrl, port, clientPrefix, username, password, caFilePath, clientCrtFilePath, clientKeyFilePath, commandService, - baseMetadataHook, baseNetworkInfoHook); + baseMetadataHook); } else { // init mqtt client with username and password pahoMqttClient = new PahoMqttClient(brokerUrl, port, clientPrefix, username, - password, commandService, baseMetadataHook, baseNetworkInfoHook); + password, commandService, baseMetadataHook); } pahoMqttClient.open(); } catch (MqttException e) { diff --git a/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/paho/MqttMessageHandler.java b/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/paho/MqttMessageHandler.java index 320ed1db..fe4cf3df 100644 --- a/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/paho/MqttMessageHandler.java +++ b/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/paho/MqttMessageHandler.java @@ -29,7 +29,6 @@ import com.alipay.sofa.koupleless.arklet.core.common.log.ArkletLoggerFactory; import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; import com.alipay.sofa.koupleless.arklet.core.util.ExceptionUtils; import com.alipay.sofa.koupleless.arklet.tunnel.mqtt.model.Constants; import com.alipay.sofa.koupleless.arklet.tunnel.mqtt.model.MqttResponse; @@ -53,20 +52,17 @@ */ public class MqttMessageHandler { - public static boolean baselineQueryComplete = false; - private final CommandService commandService; - private final BaseMetadataHook baseMetadataHook; - private final BaseNetworkInfoHook baseNetworkInfoHook; - private final MqttClient mqttClient; - private String baseEnv; - private final AtomicBoolean run = new AtomicBoolean(false); + public static boolean baselineQueryComplete = false; + private final CommandService commandService; + private final BaseMetadataHook baseMetadataHook; + private final MqttClient mqttClient; + private String baseEnv; + private final AtomicBoolean run = new AtomicBoolean(false); public MqttMessageHandler(CommandService commandService, BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook, MqttClient mqttClient, - String baseEnv) { + MqttClient mqttClient, String baseEnv) { this.commandService = commandService; this.baseMetadataHook = baseMetadataHook; - this.baseNetworkInfoHook = baseNetworkInfoHook; this.mqttClient = mqttClient; this.baseEnv = baseEnv; if (this.baseEnv == null || this.baseEnv.isEmpty()) { @@ -82,7 +78,8 @@ public MqttMessageHandler(CommandService commandService, BaseMetadataHook baseMe * @return String */ private String getHealthTopic() { - return String.format("koupleless_%s/%s/base/health", baseEnv, baseMetadataHook.getBaseID()); + return String.format("koupleless_%s/%s/base/health", baseEnv, + baseMetadataHook.getIdentity()); } /** @@ -91,7 +88,8 @@ private String getHealthTopic() { * @return String */ private String getHeartBeatTopic() { - return String.format("koupleless_%s/%s/base/heart", baseEnv, baseMetadataHook.getBaseID()); + return String.format("koupleless_%s/%s/base/heart", baseEnv, + baseMetadataHook.getIdentity()); } /** @@ -101,7 +99,7 @@ private String getHeartBeatTopic() { */ private String getBizTopic() { return String.format("koupleless_%s/%s/base/simpleBiz", baseEnv, - baseMetadataHook.getBaseID()); + baseMetadataHook.getIdentity()); } /** @@ -111,7 +109,7 @@ private String getBizTopic() { */ private String getBizOperationResponseTopic() { return String.format("koupleless_%s/%s/base/bizOperation", baseEnv, - baseMetadataHook.getBaseID()); + baseMetadataHook.getIdentity()); } /** @@ -121,7 +119,7 @@ private String getBizOperationResponseTopic() { */ private String getQueryBaselineTopic() { return String.format("koupleless_%s/%s/base/queryBaseline", baseEnv, - baseMetadataHook.getBaseID()); + baseMetadataHook.getIdentity()); } /** @@ -131,7 +129,7 @@ private String getQueryBaselineTopic() { */ private String getBaselineTopic() { return String.format("koupleless_%s/%s/base/baseline", baseEnv, - baseMetadataHook.getBaseID()); + baseMetadataHook.getIdentity()); } /** @@ -140,7 +138,7 @@ private String getBaselineTopic() { * @return String */ private String getCommandTopic() { - return String.format("koupleless_%s/%s/+", baseEnv, baseMetadataHook.getBaseID()); + return String.format("koupleless_%s/%s/+", baseEnv, baseMetadataHook.getIdentity()); } public void onConnectCompleted() { @@ -156,7 +154,9 @@ public void onConnectCompleted() { } if (run.compareAndSet(false, true)) { // fetch baseline first - BaseMetadata metadata = baseMetadataHook.getBaseMetadata(); + BaseMetadata metadata = BaseMetadata.builder().identity(baseMetadataHook.getIdentity()) + .version(baseMetadataHook.getVersion()) + .clusterName(baseMetadataHook.getClusterName()).build(); try { mqttClient.publish(getQueryBaselineTopic(), JSONObject.toJSONString(MqttResponse.withData(metadata)).getBytes(), 1, false); @@ -166,8 +166,9 @@ public void onConnectCompleted() { ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); - executor.scheduleAtFixedRate(new HeartBeatScheduledTask(getHeartBeatTopic(), mqttClient, - baseMetadataHook, baseNetworkInfoHook), 0, 10000L, TimeUnit.MILLISECONDS); + executor.scheduleAtFixedRate( + new HeartBeatScheduledTask(getHeartBeatTopic(), mqttClient, baseMetadataHook), 0, + 10000L, TimeUnit.MILLISECONDS); } } diff --git a/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/paho/PahoMqttClient.java b/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/paho/PahoMqttClient.java index fe3c2adc..59680794 100644 --- a/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/paho/PahoMqttClient.java +++ b/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/paho/PahoMqttClient.java @@ -17,7 +17,6 @@ package com.alipay.sofa.koupleless.arklet.tunnel.mqtt.paho; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; import com.alipay.sofa.koupleless.arklet.tunnel.mqtt.executor.ExecutorServiceManager; import com.alipay.sofa.koupleless.arklet.core.command.CommandService; import com.alipay.sofa.koupleless.arklet.core.common.log.ArkletLogger; @@ -47,7 +46,6 @@ public class PahoMqttClient { private final CommandService commandService; private static final ArkletLogger LOGGER = ArkletLoggerFactory.getDefaultLogger(); private final BaseMetadataHook baseMetadataHook; - private final BaseNetworkInfoHook baseNetworkInfoHook; /** *

Constructor for PahoMqttClient.

@@ -62,10 +60,9 @@ public class PahoMqttClient { */ public PahoMqttClient(String broker, int port, String clientPrefix, String username, String password, CommandService commandService, - BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook) throws MqttException { + BaseMetadataHook baseMetadataHook) throws MqttException { this.mqttClient = new MqttClient(String.format("tcp://%s:%d", broker, port), - String.format("%s@@@%s", clientPrefix, baseMetadataHook.getBaseID()), + String.format("%s@@@%s", clientPrefix, baseMetadataHook.getIdentity()), new MemoryPersistence()); this.options.setAutomaticReconnect(true); this.options.setMaxInflight(1000); @@ -73,7 +70,6 @@ public PahoMqttClient(String broker, int port, String clientPrefix, String usern this.options.setPassword(password.toCharArray()); this.baseMetadataHook = baseMetadataHook; - this.baseNetworkInfoHook = baseNetworkInfoHook; this.env = this.baseMetadataHook.getRuntimeEnv(); this.commandService = commandService; @@ -96,10 +92,9 @@ public PahoMqttClient(String broker, int port, String clientPrefix, String usern public PahoMqttClient(String broker, int port, String clientPrefix, String username, String password, String caFilePath, String clientCrtFilePath, String clientKeyFilePath, CommandService commandService, - BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook) throws MqttException { + BaseMetadataHook baseMetadataHook) throws MqttException { this.mqttClient = new MqttClient(String.format("ssl://%s:%d", broker, port), - String.format("%s@@@%s", clientPrefix, baseMetadataHook.getBaseID()), + String.format("%s@@@%s", clientPrefix, baseMetadataHook.getIdentity()), new MemoryPersistence()); this.options.setCleanSession(true); this.options.setAutomaticReconnect(true); @@ -107,7 +102,6 @@ public PahoMqttClient(String broker, int port, String clientPrefix, String usern this.options.setUserName(username); this.options.setPassword(password.toCharArray()); this.baseMetadataHook = baseMetadataHook; - this.baseNetworkInfoHook = baseNetworkInfoHook; this.env = this.baseMetadataHook.getRuntimeEnv(); try { this.options.setSocketFactory( @@ -126,7 +120,7 @@ public PahoMqttClient(String broker, int port, String clientPrefix, String usern */ public void open() throws MqttException { this.mqttClient.setCallback(new PahoMqttCallback(this.mqttClient, this.commandService, - this.baseMetadataHook, this.baseNetworkInfoHook, this.env)); + this.baseMetadataHook, this.env)); this.mqttClient.connect(this.options); } @@ -145,10 +139,9 @@ static class PahoMqttCallback implements MqttCallbackExtended { private final MqttMessageHandler messageHandler; public PahoMqttCallback(MqttClient mqttClient, CommandService commandService, - BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook, String env) { + BaseMetadataHook baseMetadataHook, String env) { this.messageHandler = new MqttMessageHandler(commandService, baseMetadataHook, - baseNetworkInfoHook, mqttClient, env); + mqttClient, env); } @Override diff --git a/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/task/HeartBeatScheduledTask.java b/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/task/HeartBeatScheduledTask.java index ac508d2d..a9b76e7d 100644 --- a/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/task/HeartBeatScheduledTask.java +++ b/arklet-tunnel-mqtt/src/main/java/com/alipay/sofa/koupleless/arklet/tunnel/mqtt/task/HeartBeatScheduledTask.java @@ -21,62 +21,41 @@ import com.alipay.sofa.koupleless.arklet.core.common.exception.ArkletRuntimeException; import com.alipay.sofa.koupleless.arklet.core.common.log.ArkletLoggerFactory; import com.alipay.sofa.koupleless.arklet.core.common.model.BaseMetadata; -import com.alipay.sofa.koupleless.arklet.core.common.model.BaseNetworkInfo; +import com.alipay.sofa.koupleless.arklet.core.common.model.BaseStatus; import com.alipay.sofa.koupleless.arklet.core.hook.base.BaseMetadataHook; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; import com.alipay.sofa.koupleless.arklet.tunnel.mqtt.model.MqttResponse; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttException; -import java.util.HashMap; -import java.util.Map; - -import static com.alipay.sofa.koupleless.arklet.core.health.model.Constants.LOCAL_HOST_NAME; -import static com.alipay.sofa.koupleless.arklet.core.health.model.Constants.LOCAL_IP; -import static com.alipay.sofa.koupleless.arklet.core.health.model.Constants.NETWORK_INFO; -import static com.alipay.sofa.koupleless.arklet.core.health.model.Constants.STATE; - public class HeartBeatScheduledTask implements Runnable { - private final String topic; - private final MqttClient mqttClient; - private final BaseMetadataHook baseMetadataHook; - private final BaseNetworkInfoHook baseNetworkInfoHook; + private final String topic; + private final MqttClient mqttClient; + private final BaseMetadataHook baseMetadataHook; public HeartBeatScheduledTask(String topic, MqttClient mqttClient, - BaseMetadataHook baseMetadataHook, - BaseNetworkInfoHook baseNetworkInfoHook) { + BaseMetadataHook baseMetadataHook) { this.topic = topic; this.mqttClient = mqttClient; this.baseMetadataHook = baseMetadataHook; - this.baseNetworkInfoHook = baseNetworkInfoHook; } @Override public void run() { // send heart beat message - Map heartBeatData = new HashMap<>(); - - BaseMetadata metadata = baseMetadataHook.getBaseMetadata(); - heartBeatData.put( - com.alipay.sofa.koupleless.arklet.core.health.model.Constants.MASTER_BIZ_INFO, - metadata); - - Map networkInfo = new HashMap<>(); - - BaseNetworkInfo baseNetworkInfo = baseNetworkInfoHook.getNetworkInfo(); - - networkInfo.put(LOCAL_IP, baseNetworkInfo.getLocalIP()); - networkInfo.put(LOCAL_HOST_NAME, baseNetworkInfo.getLocalHostName()); - - heartBeatData.put(NETWORK_INFO, networkInfo); - heartBeatData.put(STATE, BizState.ACTIVATED); + BaseMetadata baseMetadata = BaseMetadata.builder().identity(baseMetadataHook.getIdentity()) + .version(baseMetadataHook.getVersion()).clusterName(baseMetadataHook.getClusterName()) + .build(); + BaseStatus baseStatus = BaseStatus.builder().baseMetadata(baseMetadata) + .localIP(baseMetadataHook.getLocalIP()) + .localHostName(baseMetadataHook.getLocalHostName()) + .state(BizState.ACTIVATED.getBizState()).build(); ArkletLoggerFactory.getDefaultLogger().info("send heart beat message to topic {}: {}", - topic, heartBeatData); + topic, baseStatus); try { mqttClient.publish(topic, - JSONObject.toJSONString(MqttResponse.withData(heartBeatData)).getBytes(), 1, false); + JSONObject.toJSONString(MqttResponse.withData(baseStatus)).getBytes(), 1, false); } catch (MqttException e) { throw new ArkletRuntimeException("mqtt client publish health status failed", e); } diff --git a/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/BaseTest.java b/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/BaseTest.java index 19e88d2f..750ce7f1 100644 --- a/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/BaseTest.java +++ b/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/BaseTest.java @@ -21,7 +21,6 @@ import com.alipay.sofa.ark.spi.model.BizOperation; import com.alipay.sofa.koupleless.arklet.core.ArkletComponentRegistry; import com.alipay.sofa.koupleless.arklet.core.command.builtin.handler.InstallBizHandler; -import com.alipay.sofa.koupleless.arklet.core.hook.network.BaseNetworkInfoHook; import com.alipay.sofa.koupleless.arklet.core.ops.UnifiedOperationService; import com.alipay.sofa.koupleless.arklet.core.command.CommandService; import com.alipay.sofa.koupleless.arklet.core.health.HealthService; @@ -53,23 +52,16 @@ public class BaseTest { @Mock public static BaseMetadataHook baseMetadataHook; - @Mock - public static BaseNetworkInfoHook baseNetworkInfoHook; - public final InstallBizHandler.InstallBizClientResponse success = new InstallBizHandler.InstallBizClientResponse(); public final InstallBizHandler.InstallBizClientResponse failed = new InstallBizHandler.InstallBizClientResponse(); public MockedStatic arkClient; @Before public void setup() { - System.setProperty("koupleless.arklet.metadata.name", "test_metadata_hook"); - System.setProperty("koupleless.arklet.metadata.version", "test_metadata_hook_version"); - System.setProperty("koupleless.arklet.metadata.env", "test_env"); commandService = ArkletComponentRegistry.getCommandServiceInstance(); operationService = ArkletComponentRegistry.getOperationServiceInstance(); healthService = ArkletComponentRegistry.getHealthServiceInstance(); baseMetadataHook = ArkletComponentRegistry.getApiClientInstance().getMetadataHook(); - baseNetworkInfoHook = ArkletComponentRegistry.getApiClientInstance().getNetworkInfoHook(); success.setCode(ResponseCode.SUCCESS); failed.setCode(ResponseCode.FAILED); diff --git a/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/paho/MqttMessageHandlerTest.java b/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/paho/MqttMessageHandlerTest.java index b018d834..dc0384bc 100644 --- a/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/paho/MqttMessageHandlerTest.java +++ b/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/paho/MqttMessageHandlerTest.java @@ -22,8 +22,6 @@ import org.eclipse.paho.client.mqttv3.MqttMessage; import org.junit.Test; -import static org.mockito.Mockito.mockStatic; - /** * @author dongnan * @since 2024/7/5 @@ -34,14 +32,14 @@ public class MqttMessageHandlerTest extends BaseTest { @Test(expected = NullPointerException.class) public void onConnectCompleted() { MqttMessageHandler mqttMessageHandler = new MqttMessageHandler(commandService, - baseMetadataHook, baseNetworkInfoHook, null, "test"); + baseMetadataHook, null, "test"); mqttMessageHandler.onConnectCompleted(); } @Test(expected = NullPointerException.class) public void handleHealthCommand() { MqttMessageHandler mqttMessageHandler = new MqttMessageHandler(commandService, - baseMetadataHook, baseNetworkInfoHook, null, "test"); + baseMetadataHook, null, "test"); mqttMessageHandler.handleCommand(BuiltinCommand.HEALTH.getId(), new MqttMessage("{}".getBytes())); } @@ -49,7 +47,7 @@ public void handleHealthCommand() { @Test(expected = NullPointerException.class) public void handleQueryAllBizCommand() { MqttMessageHandler mqttMessageHandler = new MqttMessageHandler(commandService, - baseMetadataHook, baseNetworkInfoHook, null, "test"); + baseMetadataHook, null, "test"); mqttMessageHandler.handleCommand(BuiltinCommand.QUERY_ALL_BIZ.getId(), new MqttMessage("{}".getBytes())); } @@ -57,7 +55,7 @@ public void handleQueryAllBizCommand() { @Test(expected = NullPointerException.class) public void handleInstallBizCommand() { MqttMessageHandler mqttMessageHandler = new MqttMessageHandler(commandService, - baseMetadataHook, baseNetworkInfoHook, null, "test"); + baseMetadataHook, null, "test"); mqttMessageHandler.handleCommand(BuiltinCommand.INSTALL_BIZ.getId(), new MqttMessage( "{\"bizName\":\"testBiz\", \"bizVersion\":\"0.1.0\", \"bizUrl\":\"testBizUrl\"}" @@ -67,7 +65,7 @@ public void handleInstallBizCommand() { @Test(expected = NullPointerException.class) public void handleUnInstallBizCommand() { MqttMessageHandler mqttMessageHandler = new MqttMessageHandler(commandService, - baseMetadataHook, baseNetworkInfoHook, null, "test"); + baseMetadataHook, null, "test"); mqttMessageHandler.handleCommand(BuiltinCommand.UNINSTALL_BIZ.getId(), new MqttMessage( "{\"bizName\":\"testBiz\", \"bizVersion\":\"0.1.0\", \"bizUrl\":\"testBizUrl\"}" diff --git a/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/paho/PahoClientTest.java b/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/paho/PahoClientTest.java index 1336f948..ed27c298 100644 --- a/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/paho/PahoClientTest.java +++ b/arklet-tunnel-mqtt/src/test/java/com/alipay/sofa/koupleless/arklet/tunnel/paho/PahoClientTest.java @@ -33,15 +33,15 @@ public class PahoClientTest extends BaseTest { @Test(expected = MqttException.class) public void openWithTcp() throws MqttException { PahoMqttClient client = new PahoMqttClient("localhost", 1883, "test", "test", "", - commandService, baseMetadataHook, baseNetworkInfoHook); + commandService, baseMetadataHook); client.open(); } @Test(expected = MqttException.class) public void openWithSSL() throws MqttException { PahoMqttClient client = new PahoMqttClient("localhost", 1883, "test", "test", "", "", "", - "", commandService, baseMetadataHook, baseNetworkInfoHook); + "", commandService, baseMetadataHook); client.open(); } -} \ No newline at end of file +} diff --git a/koupleless-common/src/main/java/com/alipay/sofa/koupleless/common/util/RandomUtils.java b/koupleless-common/src/main/java/com/alipay/sofa/koupleless/common/util/RandomUtils.java new file mode 100644 index 00000000..410d9a70 --- /dev/null +++ b/koupleless-common/src/main/java/com/alipay/sofa/koupleless/common/util/RandomUtils.java @@ -0,0 +1,47 @@ +/* + * 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 com.alipay.sofa.koupleless.common.util; + +import java.security.SecureRandom; + +public class RandomUtils { + public static String generateRandomString(int length) { + // You can customize the characters that you want to add into + // the random strings + String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz"; + String CHAR_UPPER = CHAR_LOWER.toUpperCase(); + String NUMBER = "0123456789"; + + String DATA_FOR_RANDOM_STRING = CHAR_LOWER + CHAR_UPPER + NUMBER; + SecureRandom random = new SecureRandom(); + + if (length < 1) + throw new IllegalArgumentException(); + + StringBuilder sb = new StringBuilder(length); + + for (int i = 0; i < length; i++) { + // 0-62 (exclusive), random returns 0-61 + int rndCharAt = random.nextInt(DATA_FOR_RANDOM_STRING.length()); + char rndChar = DATA_FOR_RANDOM_STRING.charAt(rndCharAt); + + sb.append(rndChar); + } + + return sb.toString(); + } +} diff --git a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/network/MockBaseNetworkInfoHookNotImpl.java b/koupleless-common/src/test/java/com/alipay/sofa/koupleless/common/util/RandomUtilsTest.java similarity index 72% rename from arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/network/MockBaseNetworkInfoHookNotImpl.java rename to koupleless-common/src/test/java/com/alipay/sofa/koupleless/common/util/RandomUtilsTest.java index 864e00be..3b54830a 100644 --- a/arklet-core/src/test/java/com/alipay/sofa/koupleless/arklet/core/hooks/network/MockBaseNetworkInfoHookNotImpl.java +++ b/koupleless-common/src/test/java/com/alipay/sofa/koupleless/common/util/RandomUtilsTest.java @@ -14,11 +14,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.koupleless.arklet.core.hooks.network; +package com.alipay.sofa.koupleless.common.util; -/** - * @author 冬喃 - * @version : MockBaseNetworkInfoHookNotImpl, v 0.1 2024-10-24 下午3:28 dongnan Exp $ - */ -public class MockBaseNetworkInfoHookNotImpl { +import static org.junit.Assert.*; + +public class RandomUtilsTest { + + @org.junit.Test + public void generateRandomString() { + String randomString = RandomUtils.generateRandomString(10); + assertEquals(10, randomString.length()); + } } diff --git a/pom.xml b/pom.xml index 2d33974a..5c85f8a0 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ pom - 2.1.7 + 2.1.8-SNAPSHOT 3.1.8 ${revision} 3.2.1