Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
public final class NetUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(NetUtils.class);

// key is host name
private static Map<String, String> allHostAddresses = new HashMap<>();

// one interface can bind to multiple address
// we only save one ip for each interface name.
// eg:
Expand All @@ -56,16 +53,23 @@ public final class NetUtils {
static {
try {
doGetIpv4AddressFromNetworkInterface();
// this will throw exception in some docker image
// getLocalHost will throw exception in some docker image and sometimes will do a hostname lookup and time consuming
hostName = InetAddress.getLocalHost().getHostName();
hostAddress = InetAddress.getLocalHost().getHostAddress();
allHostAddresses.put(hostName, hostAddress);

LOGGER.info(
"add hostName:" + hostName + ",hostAddress:" + hostAddress);
"add host name from localhost:" + hostName + ",host address:" + hostAddress);
} catch (Exception e) {
LOGGER.error("got exception when trying to get addresses: {}", e);
LOGGER.error("got exception when trying to get addresses:", e);
if (allInterfaceAddresses.size() >= 1) {
InetAddress entry = allInterfaceAddresses.entrySet().iterator().next().getValue();
// get host name will do a reverse name lookup and is time consuming
hostName = entry.getHostName();
hostAddress = entry.getHostAddress();
LOGGER.info(
"add host name from interfaces:" + hostName + ",host address:" + hostAddress);
}
}

}

private NetUtils() {
Expand Down Expand Up @@ -95,15 +99,8 @@ private static void doGetIpv4AddressFromNetworkInterface() throws SocketExceptio
}

if (Inet4Address.class.isInstance(address)) {
String host = address.getHostName();
if (host == null) {
host = network.getName();
}
hostName = host;
hostAddress = address.getHostAddress();
LOGGER.info(
"add hostName:" + host + ",hostAddress:" + address.getHostAddress());
allHostAddresses.put(address.getHostName(), address.getHostAddress());
"add network interface name:" + network.getName() + ",host address:" + address.getHostAddress());
allInterfaceAddresses.put(network.getName(), address);
}
}
Expand Down Expand Up @@ -176,10 +173,6 @@ public static String getHostAddress() {
return hostAddress;
}

public static String getHostAddress(String hostName) {
return allHostAddresses.get(hostName);
}

public static InetAddress getInterfaceAddress(String interfaceName) {
return allInterfaceAddresses.get(interfaceName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public void testNetutils() {
@Test
public void testFullOperation() {
Assert.assertNotNull(NetUtils.getHostAddress());
Assert.assertNotNull(NetUtils.getHostAddress(NetUtils.getHostName()));
Assert.assertNotNull(NetUtils.getHostName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public boolean init() throws Exception {
SimpleJsonObject json = new SimpleJsonObject();
json.put(ENDPOINT_KEY, getEndpoint());
deployOptions.setConfig(json);
return VertxUtils.blockDeploy(transportVertx, HighwayServerVerticle.class, deployOptions);
return VertxUtils.blockDeploy(transportVertx, HighwayServerVerticle.class, deployOptions) && deployClient();
}

private boolean deployClient() {
return HighwayClientManager.INSTANCE.getHighwayClient(true) != null &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分的内容感觉有点重复啊!

Copy link
Contributor Author

@liubao68 liubao68 Aug 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的。3种不同的transport。 不同的实现里面基本都是一样的过程。 有一定重复度比较正常。 如果放在一起,就变得不那么独立,也不是很好。

HighwayClientManager.INSTANCE.getHighwayClient(false) != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public boolean init() throws Exception {
setListenAddressWithoutSchema(listenAddress);
}

return true;
return deployClient();
}

private boolean deployClient() {
return RestTransportClientManager.INSTANCE.getRestTransportClient(true) != null &&
RestTransportClientManager.INSTANCE.getRestTransportClient(false) != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public boolean init() throws Exception {
SimpleJsonObject json = new SimpleJsonObject();
json.put(ENDPOINT_KEY, getEndpoint());
options.setConfig(json);
return VertxUtils.blockDeploy(transportVertx, RestServerVerticle.class, options);
return VertxUtils.blockDeploy(transportVertx, RestServerVerticle.class, options) && deployClient();
}

private boolean deployClient() {
return RestTransportClientManager.INSTANCE.getRestTransportClient(true) != null &&
RestTransportClientManager.INSTANCE.getRestTransportClient(false) != null;
}

@Override
Expand Down