Skip to content

Commit 5c8ce7f

Browse files
feat:support ipv6. (#610)
1 parent e8101f8 commit 5c8ce7f

File tree

21 files changed

+672
-42
lines changed

21 files changed

+672
-42
lines changed

polaris-common/polaris-client/src/main/java/com/tencent/polaris/client/remote/ServiceAddressRepository.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.tencent.polaris.api.pojo.Instance;
2626
import com.tencent.polaris.api.pojo.ServiceKey;
2727
import com.tencent.polaris.api.utils.CollectionUtils;
28+
import com.tencent.polaris.api.utils.IPAddressUtils;
2829
import com.tencent.polaris.api.utils.StringUtils;
2930
import com.tencent.polaris.client.flow.BaseFlow;
3031
import com.tencent.polaris.client.pojo.Node;
@@ -76,7 +77,7 @@ public ServiceAddressRepository(List<String> addresses, String clientId, Extensi
7677
if (StringUtils.isNotBlank(address)) {
7778
int colonIdx = address.lastIndexOf(":");
7879
if (colonIdx > 0 && colonIdx < address.length() - 1) {
79-
String host = address.substring(0, colonIdx);
80+
String host = IPAddressUtils.getIpCompatible(address.substring(0, colonIdx));
8081
try {
8182
int port = Integer.parseInt(address.substring(colonIdx + 1));
8283
nodes.add(new Node(host, port));
@@ -128,10 +129,11 @@ public Node getServiceAddressNode() throws PolarisException {
128129
return node;
129130
}
130131
Instance instance = getDiscoverInstance();
132+
String host = IPAddressUtils.getIpCompatible(instance.getHost());
131133
if (LOG.isDebugEnabled()) {
132-
LOG.debug("success to get instance for service {}, instance is {}:{}", remoteCluster, instance.getHost(), instance.getPort());
134+
LOG.debug("success to get instance for service {}, instance is {}:{}", remoteCluster, host, instance.getPort());
133135
}
134-
return new Node(instance.getHost(), instance.getPort());
136+
return new Node(IPAddressUtils.getIpCompatible(host), instance.getPort());
135137
}
136138

137139
private Instance getDiscoverInstance() throws PolarisException {

polaris-common/polaris-metadata/src/main/java/com/tencent/polaris/metadata/core/constant/MetadataConstants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ public class MetadataConstants {
4343
* local host port.
4444
*/
4545
public static final String LOCAL_PORT = "LOCAL_PORT";
46+
47+
/**
48+
* IPV4.
49+
*/
50+
public static String ADDRESS_IPV4 = "ADDRESS_IPV4";
51+
52+
/**
53+
* IPV6.
54+
*/
55+
public static String ADDRESS_IPV6 = "ADDRESS_IPV6";
4656
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making polaris-java available.
3+
*
4+
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.api.pojo;
19+
20+
import com.tencent.polaris.api.utils.MapUtils;
21+
import com.tencent.polaris.api.utils.StringUtils;
22+
import com.tencent.polaris.metadata.core.constant.MetadataConstants;
23+
24+
import java.util.Collection;
25+
import java.util.Map;
26+
27+
/**
28+
* Wrap for Instance.
29+
*
30+
* @author Haotian Zhang
31+
*/
32+
public class InstanceWrap implements Instance {
33+
34+
private final Instance originalInstance;
35+
36+
private final String host;
37+
38+
public InstanceWrap(Instance originalInstance, boolean isPreferIpv6) {
39+
this.originalInstance = originalInstance;
40+
String host = "";
41+
if (isPreferIpv6 && MapUtils.isNotEmpty(originalInstance.getMetadata())) {
42+
host = originalInstance.getMetadata().get(MetadataConstants.ADDRESS_IPV6);
43+
} else if (MapUtils.isNotEmpty(originalInstance.getMetadata())) {
44+
host = originalInstance.getMetadata().get(MetadataConstants.ADDRESS_IPV4);
45+
}
46+
if (StringUtils.isBlank(host)) {
47+
host = originalInstance.getHost();
48+
}
49+
this.host = host;
50+
}
51+
52+
@Override
53+
public String getRevision() {
54+
return originalInstance.getRevision();
55+
}
56+
57+
@Override
58+
public CircuitBreakerStatus getCircuitBreakerStatus() {
59+
return originalInstance.getCircuitBreakerStatus();
60+
}
61+
62+
@Override
63+
public Collection<StatusDimension> getStatusDimensions() {
64+
return originalInstance.getStatusDimensions();
65+
}
66+
67+
@Override
68+
public CircuitBreakerStatus getCircuitBreakerStatus(StatusDimension statusDimension) {
69+
return originalInstance.getCircuitBreakerStatus(statusDimension);
70+
}
71+
72+
@Override
73+
public boolean isHealthy() {
74+
return originalInstance.isHealthy();
75+
}
76+
77+
@Override
78+
public boolean isIsolated() {
79+
return originalInstance.isIsolated();
80+
}
81+
82+
@Override
83+
public String getProtocol() {
84+
return originalInstance.getProtocol();
85+
}
86+
87+
@Override
88+
public String getId() {
89+
return originalInstance.getId();
90+
}
91+
92+
@Override
93+
public String getVersion() {
94+
return originalInstance.getVersion();
95+
}
96+
97+
@Override
98+
public Map<String, String> getMetadata() {
99+
return originalInstance.getMetadata();
100+
}
101+
102+
@Override
103+
public boolean isEnableHealthCheck() {
104+
return originalInstance.isEnableHealthCheck();
105+
}
106+
107+
@Override
108+
public String getRegion() {
109+
return originalInstance.getRegion();
110+
}
111+
112+
@Override
113+
public String getZone() {
114+
return originalInstance.getZone();
115+
}
116+
117+
@Override
118+
public String getCampus() {
119+
return originalInstance.getCampus();
120+
}
121+
122+
@Override
123+
public int getPriority() {
124+
return originalInstance.getPriority();
125+
}
126+
127+
@Override
128+
public int getWeight() {
129+
return originalInstance.getWeight();
130+
}
131+
132+
@Override
133+
public String getLogicSet() {
134+
return originalInstance.getLogicSet();
135+
}
136+
137+
@Override
138+
public Long getCreateTime() {
139+
return originalInstance.getCreateTime();
140+
}
141+
142+
@Override
143+
public String getNamespace() {
144+
return originalInstance.getNamespace();
145+
}
146+
147+
@Override
148+
public String getService() {
149+
return originalInstance.getService();
150+
}
151+
152+
@Override
153+
public String getHost() {
154+
return host;
155+
}
156+
157+
@Override
158+
public int getPort() {
159+
return originalInstance.getPort();
160+
}
161+
162+
@Override
163+
public int compareTo(Instance o) {
164+
return originalInstance.compareTo(o);
165+
}
166+
167+
@Override
168+
public String toString() {
169+
return "InstanceWrap{" +
170+
"originalInstance=" + originalInstance +
171+
", host='" + host + '\'' +
172+
'}';
173+
}
174+
}

polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/IPAddressUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import com.tencent.polaris.logging.LoggerFactory;
2121
import org.slf4j.Logger;
2222

23+
import java.net.Inet6Address;
2324
import java.net.InetAddress;
25+
import java.net.UnknownHostException;
2426

2527
/**
2628
* @author Haotian Zhang
@@ -54,4 +56,21 @@ public static String getHostName() {
5456
return "";
5557
}
5658
}
59+
60+
/**
61+
* 检查主机地址是否为IPv6格式
62+
*
63+
* @param host
64+
* @return
65+
*/
66+
public static boolean checkIpv6Host(String host) {
67+
try {
68+
if (StringUtils.isNotBlank(host)) {
69+
return InetAddress.getByName(host) instanceof Inet6Address;
70+
}
71+
} catch (UnknownHostException e) {
72+
// ignore
73+
}
74+
return false;
75+
}
5776
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making polaris-java available.
3+
*
4+
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.api.pojo;
19+
20+
import com.tencent.polaris.metadata.core.constant.MetadataConstants;
21+
import org.junit.Test;
22+
23+
import java.util.Collections;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
public class InstanceWrapTest {
30+
31+
@Test
32+
public void testConstructorWithIpv6Preference() {
33+
// Mock data
34+
DefaultInstance originalInstance = new DefaultInstance();
35+
Map<String, String> metadata = new HashMap<>();
36+
metadata.put(MetadataConstants.ADDRESS_IPV6, "::1");
37+
originalInstance.setMetadata(metadata);
38+
originalInstance.setHost("127.0.0.1");
39+
40+
// Test with IPv6 preference
41+
InstanceWrap instanceWrap = new InstanceWrap(originalInstance, true);
42+
43+
// Verify host is set to IPv6 address
44+
assertThat(instanceWrap.getHost()).isEqualTo("::1");
45+
}
46+
47+
@Test
48+
public void testConstructorWithIpv4Preference() {
49+
// Mock data
50+
DefaultInstance originalInstance = new DefaultInstance();
51+
Map<String, String> metadata = new HashMap<>();
52+
metadata.put(MetadataConstants.ADDRESS_IPV4, "192.168.1.1");
53+
originalInstance.setMetadata(metadata);
54+
originalInstance.setHost("::1");
55+
56+
// Test with IPv4 preference
57+
InstanceWrap instanceWrap = new InstanceWrap(originalInstance, false);
58+
59+
// Verify host is set to IPv4 address
60+
assertThat(instanceWrap.getHost()).isEqualTo("192.168.1.1");
61+
}
62+
63+
@Test
64+
public void testConstructorWithNoMetadata() {
65+
// Mock data
66+
DefaultInstance originalInstance = new DefaultInstance();
67+
originalInstance.setHost("127.0.0.1");
68+
originalInstance.setMetadata(Collections.emptyMap());
69+
70+
// Test with no metadata
71+
InstanceWrap instanceWrap = new InstanceWrap(originalInstance, true);
72+
73+
// Verify host is set to original host
74+
assertThat(instanceWrap.getHost()).isEqualTo("127.0.0.1");
75+
}
76+
77+
@Test
78+
public void testConstructorWithBlankMetadata() {
79+
// Mock data
80+
DefaultInstance originalInstance = new DefaultInstance();
81+
originalInstance.setHost("127.0.0.1");
82+
Map<String, String> metadata = new HashMap<>();
83+
metadata.put(MetadataConstants.ADDRESS_IPV6, "");
84+
originalInstance.setMetadata(metadata);
85+
86+
// Test with blank metadata
87+
InstanceWrap instanceWrap = new InstanceWrap(originalInstance, true);
88+
89+
// Verify host is set to original host
90+
assertThat(instanceWrap.getHost()).isEqualTo("127.0.0.1");
91+
}
92+
}

0 commit comments

Comments
 (0)