Skip to content
Open
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>3.0.0</version>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>sofa-dashboard</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions sofa-dashboard-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
<version>${mysql.connector.version}</version>
</dependency>

<dependency>
<groupId>net.minidev</groupId>
<artifactId>accessors-smart</artifactId>
<version>1.2</version>
</dependency>

<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ public void fetchMappingsTest() {
private HostAndPort randomInstance() {
String host = UUID.randomUUID().toString().replace("-", "").substring(8);
int port = random.nextInt(65536);
return new HostAndPort(host, port);
return new HostAndPort(host, host, port);
}
}
15 changes: 15 additions & 0 deletions sofa-dashboard-backend/sofa-dashboard-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,24 @@
<artifactId>sofa-dashboard-application</artifactId>
</dependency>

<dependency>
<groupId>net.minidev</groupId>
<artifactId>accessors-smart</artifactId>
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>activation</artifactId>
<groupId>javax.activation</groupId>
</exclusion>
<exclusion>
<artifactId>commons-io</artifactId>
<groupId>commons-io</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@
*/
public class InstanceRecord extends Application {

public InstanceRecord() {
}
public InstanceRecord() {
}

public InstanceRecord(Application other) {
setAppName(other.getAppName());
setHostName(other.getHostName());
setInternalHost(other.getInternalHost());
setPort(other.getPort());
setAppState(other.getAppState());
setStartTime(other.getStartTime());
setLastRecover(other.getLastRecover());
}
public InstanceRecord(Application other) {
setAppName(other.getAppName());
setHostName(other.getHostName());
setInternalHost(other.getInternalHost());
setPort(other.getPort());
setAppState(other.getAppState());
setStartTime(other.getStartTime());
setLastRecover(other.getLastRecover());
}

/**
* Id 是接口层概念,用来和前端交换一个短的 host&port 描述
*
* @return 唯一id
*/
public String getId() {
return HostPortUtils.uniqueId(new HostAndPort(getHostName(), getInternalHost(), getPort()));
}
/**
* Id 是接口层概念,用来和前端交换一个短的 host&port 描述
*
* @return 唯一id
*/
public String getId() {
return HostPortUtils.uniqueId(new HostAndPort(getHostName(), getInternalHost(), getPort()));
}

public void setId(String instanceId) {
// Do nothing for json serializer
}
public void setId(String instanceId) {
// Do nothing for json serializer
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,142 +30,142 @@
*/
public final class HostPortUtils {

/**
* 0~255
*/
private static final String SEGMENT_REG = "((2[0-4]\\d)|(25[0-5])|(1\\d{2})|([1-9]\\d)|(\\d))";
/**
* 0~255
*/
private static final String SEGMENT_REG = "((2[0-4]\\d)|(25[0-5])|(1\\d{2})|([1-9]\\d)|(\\d))";

private static final Pattern IP_PATTERN = getIpv4Reg();
private static final Pattern IP_PATTERN = getIpv4Reg();

/**
* 工具类隐藏构造方法
*/
private HostPortUtils() {
}
/**
* 工具类隐藏构造方法
*/
private HostPortUtils() {
}

/**
* 根据 Host and port 获取唯一id
*
* @param hostAndPort
* 地址
* @return 唯一id:(真实IP+内部IP)编码 & 端口编码
*/
public static String uniqueId(HostAndPort hostAndPort) {
long ipSeg = toDigital(hostAndPort.getHost(), 0);
long portSeg = ((long) hostAndPort.getPort() & 0xFFFF);
String internalHost = hostAndPort.getInternalHost();
if (StringUtils.isEmpty(internalHost) == false) {
long internalIpSeg = toDigital(internalHost, 1);
return Long.toHexString(ipSeg + internalIpSeg) + "&" + Long.toHexString(portSeg);
} else {
return Long.toHexString(ipSeg) + "&" + Long.toHexString(portSeg);
}
}
/**
* 根据 Host and port 获取唯一id
*
* @param hostAndPort
* 地址
* @return 唯一id:(真实IP+内部IP)编码 & 端口编码
*/
public static String uniqueId(HostAndPort hostAndPort) {
long ipSeg = toDigital(hostAndPort.getHost(), 0);
long portSeg = ((long) hostAndPort.getPort() & 0xFFFF);
String internalHost = hostAndPort.getInternalHost();
if (StringUtils.isEmpty(internalHost) == false) {
long internalIpSeg = toDigital(internalHost, 1);
return Long.toHexString(ipSeg + internalIpSeg) + "&" + Long.toHexString(portSeg);
} else {
return Long.toHexString(ipSeg) + "&" + Long.toHexString(portSeg);
}
}

/**
* 从id中获取ipv4地址
*
* @param uniqueId
* 唯一id
* @return ipv4地址
*/
public static HostAndPort getById(String uniqueId) {
String[] segment = uniqueId.split("&");
int port = ((Number) (Long.parseLong(segment[1], 16) & 0xFFFF)).intValue();
long ipPartSeg = Long.parseLong(segment[0], 16);
String ipv4 = fromDigital(ipPartSeg & 0xFFFFFFFFL);
long virtualPart = ipPartSeg >> 32;
if (virtualPart <= 0) {
return new HostAndPort(ipv4, null, port);
} else {
String virtualIp = fromDigital(virtualPart & 0xFFFFFFFFL);
return new HostAndPort(ipv4, virtualIp, port);
}
}
/**
* 从id中获取ipv4地址
*
* @param uniqueId
* 唯一id
* @return ipv4地址
*/
public static HostAndPort getById(String uniqueId) {
String[] segment = uniqueId.split("&");
int port = ((Number) (Long.parseLong(segment[1], 16) & 0xFFFF)).intValue();
long ipPartSeg = Long.parseLong(segment[0], 16);
String ipv4 = fromDigital(ipPartSeg & 0xFFFFFFFFL);
long virtualPart = ipPartSeg >> 32;
if (virtualPart <= 0) {
return new HostAndPort(ipv4, null, port);
} else {
String virtualIp = fromDigital(virtualPart & 0xFFFFFFFFL);
return new HostAndPort(ipv4, virtualIp, port);
}
}

/**
* ipv4正则
*
* @return 正则对象
*/
private static Pattern getIpv4Reg() {
StringJoiner sj = new StringJoiner("\\.");
sj.add(SEGMENT_REG);
sj.add(SEGMENT_REG);
sj.add(SEGMENT_REG);
sj.add(SEGMENT_REG);
return Pattern.compile(sj.toString());
}
/**
* ipv4正则
*
* @return 正则对象
*/
private static Pattern getIpv4Reg() {
StringJoiner sj = new StringJoiner("\\.");
sj.add(SEGMENT_REG);
sj.add(SEGMENT_REG);
sj.add(SEGMENT_REG);
sj.add(SEGMENT_REG);
return Pattern.compile(sj.toString());
}

/**
* 非法ipv4抛异常
*
* @param ipv4
* ipv4
*/
private static void checkIPv4(String ipv4) {
if (!isLegalV4(ipv4)) {
throw new IllegalArgumentException("Illegal ipv4 value " + ipv4);
}
}
/**
* 非法ipv4抛异常
*
* @param ipv4
* ipv4
*/
private static void checkIPv4(String ipv4) {
if (!isLegalV4(ipv4)) {
throw new IllegalArgumentException("Illegal ipv4 value " + ipv4);
}
}

/**
* ip转换为对应32bit数字
*
* @param ipv4
* 点分十进制ipv4
* @param segmentPart
* TODO
* @return ipv4 对应数字
*/
private static long toDigital(String ipv4, int segmentPart) {
checkIPv4(ipv4);
String[] segments = ipv4.split("\\.");
long result = 0;
for (int i = 0; i < 4; i++) {
result += Long.parseLong(segments[3 - i]) << ((8 * i) + segmentPart * Integer.SIZE);
}
return result;
}
/**
* ip转换为对应32bit数字
*
* @param ipv4
* 点分十进制ipv4
* @param segmentPart
* TODO
* @return ipv4 对应数字
*/
private static long toDigital(String ipv4, int segmentPart) {
checkIPv4(ipv4);
String[] segments = ipv4.split("\\.");
long result = 0;
for (int i = 0; i < 4; i++) {
result += Long.parseLong(segments[3 - i]) << ((8 * i) + segmentPart * Integer.SIZE);
}
return result;
}

/**
* 检查是否合法ipv4
*
* @param ipv4
* ipv4地址
* @return 是否匹配
*/
private static boolean isLegalV4(String ipv4) {
return !StringUtils.isEmpty(ipv4) && IP_PATTERN.matcher(ipv4).matches();
}
/**
* 检查是否合法ipv4
*
* @param ipv4
* ipv4地址
* @return 是否匹配
*/
private static boolean isLegalV4(String ipv4) {
return !StringUtils.isEmpty(ipv4) && IP_PATTERN.matcher(ipv4).matches();
}

/**
* 数字转换为string ip格式
*
* @param ipv4
* 32位数字ip
* @return 点分十进制ip
*/
public static String fromDigital(Long ipv4) {
if (ipv4 > 0xFFFFFFFFL || ipv4 < 0) {
throw new IllegalArgumentException("Illegal ipv4 digital " + ipv4);
}
int[] segments = new int[4];
for (int i = 0; i < 4; i++) {
segments[3 - i] = ipv4.intValue() & 0xFF;
ipv4 >>= 8;
}
StringJoiner sj = new StringJoiner(".");
for (int segment : segments) {
sj.add(String.valueOf(segment));
}
return sj.toString();
}
/**
* 数字转换为string ip格式
*
* @param ipv4
* 32位数字ip
* @return 点分十进制ip
*/
public static String fromDigital(Long ipv4) {
if (ipv4 > 0xFFFFFFFFL || ipv4 < 0) {
throw new IllegalArgumentException("Illegal ipv4 digital " + ipv4);
}
int[] segments = new int[4];
for (int i = 0; i < 4; i++) {
segments[3 - i] = ipv4.intValue() & 0xFF;
ipv4 >>= 8;
}
StringJoiner sj = new StringJoiner(".");
for (int segment : segments) {
sj.add(String.valueOf(segment));
}
return sj.toString();
}

public static void main(String[] args) {
String id = uniqueId(new HostAndPort("192.168.0.104", "8.16.32.64", 38081));
System.out.println(id);
HostAndPort hostAndPort = getById(id);
System.out.println(hostAndPort);
}
public static void main(String[] args) {
String id = uniqueId(new HostAndPort("192.168.0.104", "8.16.32.64", 38081));
System.out.println(id);
HostAndPort hostAndPort = getById(id);
System.out.println(hostAndPort);
}
}