Skip to content

Commit

Permalink
Merge pull request dragonflyoss#277 from lowzj/supernode-localIP
Browse files Browse the repository at this point in the history
fix: unreachable supernode ip in p2p-network
  • Loading branch information
starnop authored Mar 1, 2019
2 parents dfdb250 + 80a1f56 commit 8b37cfc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/quick_start/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ You have started your Docker container.
```bash
# Replace ${imageName} with the real image name
docker run -d -p 8001:8001 -p 8002:8002 ${imageName}
# if you run docker in macOS, please use this command:
docker run -d -p 8001:8001 -p 8002:8002 ${imageName} -Dsupernode.advertiseIp=127.0.0.1
```

For example, if you're in China, run the following commands:
Expand Down
3 changes: 2 additions & 1 deletion src/supernode/src/main/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ ADD supernode.jar supernode.jar

EXPOSE 8001 8002

ENTRYPOINT ["/root/start.sh"]
CMD ["-Dsupernode.advertiseIp="]
ENTRYPOINT /root/start.sh $0 $@

2 changes: 1 addition & 1 deletion src/supernode/src/main/docker/sources/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

nginx

java -Djava.security.egd=file:/dev/./urandom -jar supernode.jar
java -Djava.security.egd=file:/dev/./urandom "$@" -jar supernode.jar
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
package com.dragonflyoss.dragonfly.supernode.config;

import javax.annotation.PostConstruct;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import com.dragonflyoss.dragonfly.supernode.common.Constants;
import com.alibaba.fastjson.JSON;

import com.dragonflyoss.dragonfly.supernode.common.Constants;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
Expand Down Expand Up @@ -68,6 +73,12 @@ public class SupernodeProperties {
*/
private String dfgetPath = Constants.DFGET_PATH;

/**
* The advertise ip is used to set the ip that we advertise to other peer in the p2p-network.
* By default, the first non-loop address is advertised.
*/
private String advertiseIp;

@PostConstruct
public void init() {
String cdnHome = baseHome + "/repo";
Expand All @@ -82,6 +93,48 @@ public void init() {
log.error("create repo dir error", e);
System.exit(1);
}

setLocalIp();

log.info("cluster members: {}", JSON.toJSONString(cluster));
}

private void setLocalIp() {
if (StringUtils.isNotBlank(advertiseIp)) {
Constants.localIp = advertiseIp;
Constants.generateNodeCid();
log.info("init local ip of supernode, use ip:{}", Constants.localIp);
} else {
List<String> ips = getAllIps();
if (!ips.isEmpty()) {
Constants.localIp = ips.get(0);
Constants.generateNodeCid();
}
log.info("init local ip of supernode, ip list:{}, use ip:{}",
JSON.toJSONString(ips), Constants.localIp);
}
}

private List<String> getAllIps() {
List<String> ips = new ArrayList<String>();
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface
.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = allNetInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface
.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ip = addresses.nextElement();
if (ip instanceof Inet4Address && !ip.isAnyLocalAddress()
&& !ip.isLoopbackAddress()) {
ips.add(ip.getHostAddress());
}
}
}
} catch (Exception e) {
log.error("getAllIps error:{}", e.getMessage(), e);
}
return ips;
}
}

0 comments on commit 8b37cfc

Please sign in to comment.