Skip to content

Commit

Permalink
Merge pull request #210 from koupleless/youji-dev-2.1.x
Browse files Browse the repository at this point in the history
merge 1.3.3-SNAPSHOT to 2.1.8-SNAPSHOT
  • Loading branch information
lvjing2 authored Nov 20, 2024
2 parents b43ad05 + 4267272 commit a0edef1
Show file tree
Hide file tree
Showing 32 changed files with 396 additions and 602 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,23 +47,18 @@
@Singleton
public class ApiClient implements ArkletComponent {

private static final List<Tunnel> tunnelList = new ArrayList<>(8);
private static final List<Tunnel> 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());
Expand All @@ -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();
}
}
Expand All @@ -123,10 +107,6 @@ public BaseMetadataHook getMetadataHook() {
return baseMetadataHook;
}

public BaseNetworkInfoHook getNetworkInfoHook() {
return baseNetworkInfoHook;
}

private static class TunnelGuiceModule extends AbstractModule {
@Override
protected void configure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import com.alipay.sofa.koupleless.arklet.core.command.meta.Output;

import java.util.UUID;

/**
* <p>Response class.</p>
*
Expand Down Expand Up @@ -50,22 +48,22 @@ public class Response {
private String errorStackTrace;

/**
* baseID
* baseIdentity
*/
private String baseID;
private String baseIdentity;

/**
* <p>fromCommandOutput.</p>
*
* @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;
}

Expand Down Expand Up @@ -195,20 +193,20 @@ public void setErrorStackTrace(String errorStackTrace) {
}

/**
* Getter method for property <tt>baseID</tt>.
* Getter method for property <tt>baseIdentity</tt>.
*
* @return property value of baseID
* @return property value of baseIdentity
*/
public String getBaseID() {
return baseID;
public String getBaseIdentity() {
return baseIdentity;
}

/**
* Setter method for property <tt>baseID</tt>.
* Setter method for property <tt>baseIdentity</tt>.
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <p>Tunnel interface.</p>
Expand All @@ -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);

/**
* <p>run.</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
Loading

0 comments on commit a0edef1

Please sign in to comment.