support hostname for server cluster
This commit is contained in:
parent
077c4cdb6f
commit
1f6271e1a1
@ -34,6 +34,16 @@ public class SystemUtils {
|
||||
*/
|
||||
public static final String STANDALONE_MODE_PROPERTY_NAME = "nacos.standalone";
|
||||
|
||||
/**
|
||||
* The System property name of prefer hostname over ip
|
||||
*/
|
||||
public static final String PREFER_HOSTNAME_OVER_IP_PROPERTY_NAME ="nacos.preferHostnameOverIp";
|
||||
/**
|
||||
* Flag to say that, when guessing a hostname, the hostname of the server should be
|
||||
* used in preference to the IP address reported by the OS.
|
||||
*/
|
||||
public static final boolean PREFER_HOSTNAME_OVER_IP=Boolean.getBoolean(PREFER_HOSTNAME_OVER_IP_PROPERTY_NAME);
|
||||
|
||||
/**
|
||||
* Standalone mode or not
|
||||
*/
|
||||
|
@ -34,10 +34,12 @@ public class SystemUtilsTest {
|
||||
private static final Random random = new Random();
|
||||
|
||||
private static boolean standaloneMode = random.nextBoolean();
|
||||
private static boolean preferHostMode = random.nextBoolean();
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
System.setProperty("nacos.standalone", String.valueOf(standaloneMode));
|
||||
System.setProperty("nacos.preferHostnameOverIp", String.valueOf(preferHostMode));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -54,4 +56,19 @@ public class SystemUtilsTest {
|
||||
Assert.assertEquals(standaloneMode, SystemUtils.STANDALONE_MODE);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreferHostModeConstants() {
|
||||
|
||||
System.out.printf("System property \"%s\" = %s \n", "nacos.preferrHostnameOverIp", preferHostMode);
|
||||
|
||||
if ("true".equalsIgnoreCase(System.getProperty("nacos.preferHostnameOverIp"))) {
|
||||
Assert.assertTrue(SystemUtils.PREFER_HOSTNAME_OVER_IP);
|
||||
} else {
|
||||
Assert.assertFalse(SystemUtils.PREFER_HOSTNAME_OVER_IP);
|
||||
}
|
||||
|
||||
Assert.assertEquals(preferHostMode, SystemUtils.PREFER_HOSTNAME_OVER_IP);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.alibaba.nacos.common.util.Pair;
|
||||
import com.alibaba.nacos.naming.misc.*;
|
||||
import com.alibaba.nacos.naming.monitor.PerformanceLoggerThread;
|
||||
import com.alibaba.nacos.naming.push.PushService;
|
||||
@ -577,12 +576,12 @@ public class DomainsManager {
|
||||
|
||||
List<String> sameSiteServers = NamingProxy.getSameSiteServers().get("sameSite");
|
||||
|
||||
if (sameSiteServers == null || sameSiteServers.size() <= 0 || !NamingProxy.getServers().contains(NetUtils.localIP())) {
|
||||
if (sameSiteServers == null || sameSiteServers.size() <= 0 || !NamingProxy.getServers().contains(NetUtils.localServer())) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (String server : sameSiteServers) {
|
||||
if (server.equals(NetUtils.localIP())) {
|
||||
if (server.equals(NetUtils.localServer())) {
|
||||
continue;
|
||||
}
|
||||
synchronizer.send(server, msg);
|
||||
|
@ -267,7 +267,7 @@ public class VirtualClusterDomain implements Domain, RaftListener {
|
||||
entry.getValue().destroy();
|
||||
}
|
||||
|
||||
if (RaftCore.isLeader(NetUtils.localIP())) {
|
||||
if (RaftCore.isLeader(NetUtils.localServer())) {
|
||||
RaftCore.signalDelete(UtilsAndCommons.getIPListStoreKey(this));
|
||||
}
|
||||
|
||||
|
@ -100,12 +100,12 @@ public abstract class AbstractHealthCheckProcessor {
|
||||
|
||||
List<String> sameSiteServers = NamingProxy.getSameSiteServers().get("sameSite");
|
||||
|
||||
if (sameSiteServers == null || sameSiteServers.size() <= 0 || !NamingProxy.getServers().contains(NetUtils.localIP())) {
|
||||
if (sameSiteServers == null || sameSiteServers.size() <= 0 || !NamingProxy.getServers().contains(NetUtils.localServer())) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (String server : sameSiteServers) {
|
||||
if (server.equals(NetUtils.localIP())) {
|
||||
if (server.equals(NetUtils.localServer())) {
|
||||
continue;
|
||||
}
|
||||
Map<String, String> params = new HashMap<>(10);
|
||||
|
@ -37,7 +37,7 @@ public class DomainStatusSynchronizer implements Synchronizer {
|
||||
Map<String,String> params = new HashMap<String, String>(10);
|
||||
|
||||
params.put("domsStatus", msg.getData());
|
||||
params.put("clientIP", NetUtils.localIP());
|
||||
params.put("clientIP", NetUtils.localServer());
|
||||
|
||||
|
||||
String url = "http://" + serverIP + ":" + RunningConfig.getServerPort() + RunningConfig.getContextPath() +
|
||||
|
@ -17,13 +17,13 @@ package com.alibaba.nacos.naming.misc;
|
||||
|
||||
import com.alibaba.nacos.common.util.SystemUtils;
|
||||
import com.alibaba.nacos.naming.boot.RunningConfig;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -109,7 +109,7 @@ public class NamingProxy {
|
||||
|
||||
if (STANDALONE_MODE) {
|
||||
servers = new ArrayList<>();
|
||||
servers.add(InetAddress.getLocalHost().getHostAddress() + ":" + RunningConfig.getServerPort());
|
||||
servers.add(NetUtils.localServer());
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -20,14 +20,20 @@ import com.alibaba.nacos.naming.boot.RunningConfig;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import static com.alibaba.nacos.common.util.SystemUtils.PREFER_HOSTNAME_OVER_IP;
|
||||
|
||||
/**
|
||||
* @author nacos
|
||||
*/
|
||||
public class NetUtils {
|
||||
|
||||
public static String localIP() {
|
||||
public static String localServer() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostAddress() + ":" + RunningConfig.getServerPort();
|
||||
if (PREFER_HOSTNAME_OVER_IP) {
|
||||
return InetAddress.getLocalHost().getHostName() + ":" + RunningConfig.getServerPort();
|
||||
} else {
|
||||
return InetAddress.getLocalHost().getHostAddress() + ":" + RunningConfig.getServerPort();
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
return "resolve_failed";
|
||||
}
|
||||
@ -45,4 +51,6 @@ public class NetUtils {
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ public class PeerSet {
|
||||
if (STANDALONE_MODE) {
|
||||
RaftPeer local = local();
|
||||
local.state = RaftPeer.State.LEADER;
|
||||
local.voteFor = NetUtils.localIP();
|
||||
local.voteFor = NetUtils.localServer();
|
||||
|
||||
}
|
||||
}
|
||||
@ -175,9 +175,9 @@ public class PeerSet {
|
||||
}
|
||||
|
||||
public RaftPeer local() {
|
||||
RaftPeer peer = peers.get(NetUtils.localIP());
|
||||
RaftPeer peer = peers.get(NetUtils.localServer());
|
||||
if (peer == null) {
|
||||
throw new IllegalStateException("unable to find local peer: " + NetUtils.localIP() + ", all peers: "
|
||||
throw new IllegalStateException("unable to find local peer: " + NetUtils.localServer() + ", all peers: "
|
||||
+ Arrays.toString(peers.keySet().toArray()));
|
||||
}
|
||||
|
||||
|
@ -433,7 +433,7 @@ public class RaftCore {
|
||||
return;
|
||||
}
|
||||
|
||||
RaftPeer local = peers.get(NetUtils.localIP());
|
||||
RaftPeer local = peers.get(NetUtils.localServer());
|
||||
Loggers.RAFT.info("leader timeout, start voting,leader: " + JSON.toJSONString(getLeader()) + ", term: " + local.term);
|
||||
|
||||
peers.reset();
|
||||
@ -481,7 +481,7 @@ public class RaftCore {
|
||||
throw new IllegalStateException("not ready yet");
|
||||
}
|
||||
|
||||
RaftPeer local = peers.get(NetUtils.localIP());
|
||||
RaftPeer local = peers.get(NetUtils.localServer());
|
||||
if (remote.term.get() <= local.term.get()) {
|
||||
String msg = "received illegitimate vote" +
|
||||
", voter-term:" + remote.term + ", votee-term:" + local.term;
|
||||
@ -894,7 +894,7 @@ public class RaftCore {
|
||||
}
|
||||
|
||||
public static boolean isLeader() {
|
||||
return peers.isLeader(NetUtils.localIP());
|
||||
return peers.isLeader(NetUtils.localServer());
|
||||
}
|
||||
|
||||
public static String buildURL(String ip, String api) {
|
||||
|
@ -951,7 +951,7 @@ public class ApiCommands {
|
||||
RaftCore.OPERATE_LOCK.lock();
|
||||
try {
|
||||
final CountDownLatch countDownLatch = new CountDownLatch(RaftCore.getPeerSet().majorityCount());
|
||||
proxyParams.put("clientIP", NetUtils.localIP());
|
||||
proxyParams.put("clientIP", NetUtils.localServer());
|
||||
proxyParams.put("notify", "true");
|
||||
|
||||
proxyParams.put("term", String.valueOf(RaftCore.getPeerSet().local().term));
|
||||
@ -2296,7 +2296,7 @@ public class ApiCommands {
|
||||
diff.add(ip + "_" + domString);
|
||||
}
|
||||
|
||||
if (ip.equals(NetUtils.localIP())) {
|
||||
if (ip.equals(NetUtils.localServer())) {
|
||||
localDomString = domString;
|
||||
}
|
||||
|
||||
|
@ -23,19 +23,25 @@ import com.alibaba.nacos.naming.core.DomainsManager;
|
||||
import com.alibaba.nacos.naming.core.VirtualClusterDomain;
|
||||
import com.alibaba.nacos.naming.misc.NetUtils;
|
||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
import com.alibaba.nacos.naming.raft.*;
|
||||
import com.alibaba.nacos.naming.raft.Datum;
|
||||
import com.alibaba.nacos.naming.raft.RaftCore;
|
||||
import com.alibaba.nacos.naming.raft.RaftListener;
|
||||
import com.alibaba.nacos.naming.raft.RaftPeer;
|
||||
import com.alibaba.nacos.naming.raft.RaftStore;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author nacos
|
||||
*/
|
||||
@ -79,14 +85,14 @@ public class RaftCommands {
|
||||
RaftPeer peer = null;
|
||||
|
||||
for (RaftPeer peer1 : peers) {
|
||||
if (StringUtils.equals(peer1.ip, NetUtils.localIP())) {
|
||||
if (StringUtils.equals(peer1.ip, NetUtils.localServer())) {
|
||||
peer = peer1;
|
||||
}
|
||||
}
|
||||
|
||||
if (peer == null) {
|
||||
peer = new RaftPeer();
|
||||
peer.ip = NetUtils.localIP();
|
||||
peer.ip = NetUtils.localServer();
|
||||
}
|
||||
|
||||
return JSON.parseObject(JSON.toJSONString(peer));
|
||||
|
@ -41,10 +41,10 @@ public class BaseTest {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
RaftPeer peer = new RaftPeer();
|
||||
peer.ip = NetUtils.localIP();
|
||||
peer.ip = NetUtils.localServer();
|
||||
RaftCore.setPeerSet(peerSet);
|
||||
Mockito.when(peerSet.local()).thenReturn(peer);
|
||||
Mockito.when(peerSet.getLeader()).thenReturn(peer);
|
||||
Mockito.when(peerSet.isLeader(NetUtils.localIP())).thenReturn(true);
|
||||
Mockito.when(peerSet.isLeader(NetUtils.localServer())).thenReturn(true);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user