Skip to content

Commit

Permalink
Merge pull request alibaba#213 from paderlol/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Fury Zhu authored Nov 11, 2018
2 parents cb4e863 + d60c03e commit 724ced4
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
17 changes: 17 additions & 0 deletions common/src/test/java/com/alibaba/nacos/common/SystemUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,4 +56,19 @@ public void testStandaloneModeConstants() {
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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand Down Expand Up @@ -577,12 +576,12 @@ public void run() {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void destroy() throws Exception {
entry.getValue().destroy();
}

if (RaftCore.isLeader(NetUtils.localIP())) {
if (RaftCore.isLeader(NetUtils.localServer())) {
RaftCore.signalDelete(UtilsAndCommons.getIPListStoreKey(this));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public void run() {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void send(final String serverIP, Message msg) {
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() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

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;
Expand Down Expand Up @@ -109,7 +109,7 @@ public static void refreshSrvIfNeed(String env) {

if (STANDALONE_MODE) {
servers = new ArrayList<>();
servers.add(InetAddress.getLocalHost().getHostAddress() + ":" + RunningConfig.getServerPort());
servers.add(NetUtils.localServer());
return;
}

Expand Down
17 changes: 15 additions & 2 deletions naming/src/main/java/com/alibaba/nacos/naming/misc/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@
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();
InetAddress inetAddress = InetAddress.getLocalHost();
String serverAddress = inetAddress.getHostAddress();
if (PREFER_HOSTNAME_OVER_IP) {
if (inetAddress.getHostName().equals(inetAddress.getCanonicalHostName())) {
serverAddress = inetAddress.getHostName();
} else {
serverAddress = inetAddress.getCanonicalHostName();
}
}
return serverAddress + UtilsAndCommons.CLUSTER_CONF_IP_SPLITER + RunningConfig.getServerPort();
} catch (UnknownHostException e) {
return "resolve_failed";
}
Expand All @@ -45,4 +56,6 @@ public static String num2ip(int ip) {

return x;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void add(List<String> servers) {
if (STANDALONE_MODE) {
RaftPeer local = local();
local.state = RaftPeer.State.LEADER;
local.voteFor = NetUtils.localIP();
local.voteFor = NetUtils.localServer();

}
}
Expand Down Expand Up @@ -175,9 +175,9 @@ public Integer onCompleted(Response response) throws Exception {
}

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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public static void sendVote() {
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();
Expand Down Expand Up @@ -481,7 +481,7 @@ public static RaftPeer receivedVote(RaftPeer remote) {
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;
Expand Down Expand Up @@ -894,7 +894,7 @@ public static boolean isLeader(String ip) {
}

public static boolean isLeader() {
return peers.isLeader(NetUtils.localIP());
return peers.isLeader(NetUtils.localServer());
}

public static String buildURL(String ip, String api) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ private String doAddIP4Dom(HttpServletRequest request) throws Exception {
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));
Expand Down Expand Up @@ -2306,7 +2306,7 @@ public JSONObject checkDataConsistence(HttpServletRequest request) throws Except
diff.add(ip + "_" + domString);
}

if (ip.equals(NetUtils.localIP())) {
if (ip.equals(NetUtils.localServer())) {
localDomString = domString;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@
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
*/
Expand Down Expand Up @@ -79,14 +85,14 @@ public JSONObject getPeer(HttpServletRequest request, HttpServletResponse respon
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));
Expand Down
4 changes: 2 additions & 2 deletions naming/src/test/java/com/alibaba/nacos/naming/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public void before() {
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);
}
}

0 comments on commit 724ced4

Please sign in to comment.