Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dashboard-client支持内部IP,虚拟IP,虚拟端口 #29

Merged
merged 4 commits into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ tmp/
logs/
node_modules
coverage-report
.vscode
.vscode
*.factorypath
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ public void afterStarted(CuratorFramework client) {
private String getBizPath() {
return Constants.SOFA_BOOT_CLIENT_ROOT + Constants.SOFA_BOOT_CLIENT_BIZ
+ Constants.SEPARATOR + application.getAppName() + Constants.SEPARATOR
+ application.getHostName();
+ application.getHostName() + Constants.COLON + application.getInternalHost();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.Serializable;
import java.util.Objects;

import org.springframework.util.StringUtils;

/**
* Application instance model definition
*
Expand All @@ -38,6 +40,12 @@ public class Application implements Serializable, Comparable<Application> {
*/
private String hostName;

/**
* 内部IP
*
*/
private String internalHost;

/**
* Binding port
*/
Expand All @@ -64,6 +72,7 @@ public Application() {
private Application(Builder builder) {
setAppName(builder.appName);
setHostName(builder.hostName);
setInternalHost(builder.internalHost);
setPort(builder.port);
setAppState(builder.appState);
setStartTime(builder.startTime);
Expand All @@ -78,7 +87,15 @@ public static Builder newBuilder() {
public String toString() {
return "Application{" + "appName='" + appName + '\'' + ", hostName='" + hostName + '\''
+ ", port=" + port + ", appState='" + appState + '\'' + ", startTime=" + startTime
+ ", lastRecover=" + lastRecover + '}';
+ ", lastRecover=" + lastRecover + "internalHost" + internalHost + '}';
}

public String getInternalHost() {
return internalHost;
}

public void setInternalHost(String internalHost) {
this.internalHost = internalHost;
}

public String getAppName() {
Expand Down Expand Up @@ -137,25 +154,37 @@ public boolean equals(Object o) {
return false;
Application that = (Application) o;
return getPort() == that.getPort() && Objects.equals(getAppName(), that.getAppName())
&& Objects.equals(getHostName(), that.getHostName());
&& Objects.equals(getHostName(), that.getHostName())
&& Objects.equals(getInternalHost(), that.getInternalHost());
}

@Override
public int hashCode() {
return Objects.hash(getAppName(), getHostName(), getPort());
return Objects.hash(getAppName(), getHostName(), getInternalHost(), getPort());
}

@Override
public int compareTo(Application o) {
int nameSign = Integer.compare(appName.compareTo(o.appName), 0) << 2;
int hostSign = Integer.compare(hostName.compareTo(o.hostName), 0) << 1;
int nameSign = Integer.compare(appName.compareTo(o.appName), 0) << 3;
int hostSign = Integer.compare(hostName.compareTo(o.hostName), 0) << 2;
int portSign = Integer.compare(port, o.port);
return nameSign + hostSign + portSign;
boolean isTargetInternalHostNull = StringUtils.isEmpty(o.internalHost);
boolean isInternalHostNull = StringUtils.isEmpty(internalHost);
if (isInternalHostNull && isTargetInternalHostNull) {
return nameSign + hostSign + portSign;
} else if (isTargetInternalHostNull == false && isInternalHostNull == false) {
int internalSign = Integer.compare(internalHost.compareTo(o.internalHost), 0) << 1;
return nameSign + hostSign + internalSign + portSign;
} else {
return 1;
}
}

public static final class Builder {

private String appName;
private String hostName;
private String internalHost;
private int port;
private String appState;
private long startTime;
Expand All @@ -164,6 +193,11 @@ public static final class Builder {
private Builder() {
}

public Builder internalHost(String internalHost) {
this.internalHost = internalHost;
return this;
}

public Builder appName(String appName) {
this.appName = appName;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,76 +19,94 @@
import java.io.Serializable;
import java.util.Objects;

import org.springframework.util.StringUtils;

/**
* Host and port definition
*
* @author chen.pengzhi ([email protected])
*/
public class HostAndPort implements Comparable<HostAndPort>, Serializable {

private static final int serialVersionUID = 0x11;

private String host;

private int port;

public HostAndPort() {
}

public HostAndPort(String host, int port) {
this.host = host;
this.port = port;
}

/**
* Convert host & port to instance id
*
* @return instance id
*/
public String toInstanceId() {
return String.format("%s_%d", host, port);
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

@Override
public int compareTo(HostAndPort o) {
int hostSign = Integer.compare(host.compareTo(o.host), 0) << 2;
int portSign = Integer.compare(port, o.port);
return hostSign + portSign;
}

@Override
public String toString() {
return "HostAndPort{" + "host='" + host + '\'' + ", port=" + port + '}';
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
HostAndPort that = (HostAndPort) o;
return getPort() == that.getPort() && Objects.equals(getHost(), that.getHost());
}

@Override
public int hashCode() {
return Objects.hash(getHost(), getPort());
}
private static final int serialVersionUID = 0x11;

private String host;

private String internalHost;

private int port;

// public HostAndPort() {
// }

public HostAndPort(String host, String interHost, int port) {
this.host = host;
this.port = port;
this.internalHost = interHost;
}

/**
* Convert host & port to instance id
*
* @return instance id
*/
public String toInstanceId() {
if (StringUtils.isEmpty(internalHost)) {
return String.format("%s_%d", host, port);
} else {
return String.format("%s_%s_%d", host, internalHost, port);
}
}

public String getInternalHost() {
return "null".equals(internalHost) ? null : internalHost;
}

public void setInternalHost(String internalHost) {
this.internalHost = internalHost;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

@Override
public int compareTo(HostAndPort o) {
int hostSign = Integer.compare(host.compareTo(o.host), 0) << 2;
int internalSign = Integer.compare(internalHost.compareTo(o.internalHost), 0) << 1;
int portSign = Integer.compare(port, o.port);
return hostSign + portSign + internalSign;
}

@Override
public String toString() {
return "HostAndPort{" + "host='" + host + '\'' + ", internalHost='" + internalHost + '\'' + ", port=" + port + '}';
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
HostAndPort that = (HostAndPort) o;
return getPort() == that.getPort() && Objects.equals(getHost(), that.getHost()) && Objects.equals(getInternalHost(), that.getInternalHost());
}

@Override
public int hashCode() {
return Objects.hash(getHost(), getInternalHost(), getPort());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
*/
package com.alipay.sofa.dashboard.client.registry.zookeeper;

import com.alipay.sofa.dashboard.client.model.common.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import com.alipay.sofa.dashboard.client.model.common.Application;

/**
* @author [email protected]
* @date 2019-08-29 09:18
Expand All @@ -45,14 +47,15 @@ final class ZookeeperRegistryUtils {
/**
* Convert an instance definition into session node name
*
* @param instance application instance
* @param instance
* application instance
* @return session node name
*/
@NonNull
static String toSessionNode(Application instance) {
String appId = String.format("%s:%d?startTime=%d&lastRecover=%d&state=%s",
instance.getHostName(), instance.getPort(), instance.getStartTime(),
instance.getLastRecover(), instance.getAppState());
String appId = String.format("%s:%d?internalHost=%s&startTime=%d&lastRecover=%d&state=%s",
instance.getHostName(), instance.getPort(), instance.getInternalHost(),
instance.getStartTime(), instance.getLastRecover(), instance.getAppState());
String appName = instance.getAppName();
return String.format("%s/%s/%s", ZookeeperConstants.SOFA_BOOT_CLIENT_INSTANCE, appName,
appId);
Expand All @@ -61,7 +64,8 @@ static String toSessionNode(Application instance) {
/**
* Parse session node to application instance.
*
* @param sessionNode session node path
* @param sessionNode
* session node path
* @return {@code null}, if this session path is not a legal one
*/
@Nullable
Expand Down Expand Up @@ -90,6 +94,7 @@ static Application parseSessionNode(String sessionNode) {

Application application = new Application();
application.setAppName(appName);
application.setInternalHost(query.get("internalHost"));
application.setHostName(instanceUri.getHost());
application.setPort(instanceUri.getPort());
application.setAppState(query.get("state"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
*/
package com.alipay.sofa.dashboard.redis.config;

import com.alipay.sofa.dashboard.redis.io.LettuceConnFactoryProvider;
import com.alipay.sofa.dashboard.redis.io.RedisRecordExporter;
import com.alipay.sofa.dashboard.redis.io.RedisRecordImporter;
import com.alipay.sofa.dashboard.redis.properties.SofaDashboardRedisProperties;
import io.lettuce.core.resource.DefaultClientResources;
import java.io.Closeable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -31,7 +28,12 @@
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.Closeable;
import com.alipay.sofa.dashboard.redis.io.LettuceConnFactoryProvider;
import com.alipay.sofa.dashboard.redis.io.RedisRecordExporter;
import com.alipay.sofa.dashboard.redis.io.RedisRecordImporter;
import com.alipay.sofa.dashboard.redis.properties.SofaDashboardRedisProperties;

import io.lettuce.core.resource.DefaultClientResources;

/**
* @author chen.pengzhi ([email protected])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void recycleServer() {

@Test
public void writeRecordAndRead() {
final HostAndPort hostAndPort = new HostAndPort("127.0.0.1", 8080);
final HostAndPort hostAndPort = new HostAndPort("127.0.0.1", "10.41.0.1", 8080);
final String schemeName = "test";
List<StoreRecord> samples = Lists.newArrayList(StoreRecord.newBuilder()
.schemeName(schemeName).timestamp(System.currentTimeMillis()).value("aaaaa").build());
Expand Down
Loading