@Override public boolean ping(MicroserviceInstance instance) { if (instance.getEndpoints() != null && instance.getEndpoints().size() > 0) { IpPort ipPort = NetUtils.parseIpPortFromURI(instance.getEndpoints().get(0)); try (Socket s = new Socket()) { s.connect(new InetSocketAddress(ipPort.getHostOrIp(), ipPort.getPort()), 3000); return true; } catch (IOException e) { // ignore this error } } return false; } }
public static boolean canPublishEndpoint(String listenAddress) { if (StringUtils.isEmpty(listenAddress)) { LOGGER.info("listenAddress is null, can not publish."); return false; } IpPort ipPort = NetUtils.parseIpPortFromURI("http://" + listenAddress); if (ipPort == null) { LOGGER.info("invalid listenAddress {}, can not publish, format should be ip:port.", listenAddress); return false; } if (NetUtils.canTcpListen(ipPort.getSocketAddress().getAddress(), ipPort.getPort())) { LOGGER.info("{} is not listened, can not publish.", ipPort.getSocketAddress()); return false; } return true; }
/** * Parse a {@link URI} into an {@link IpPort}. * * <p> * A uri without port is allowed, in which case the port will be inferred from the scheme. {@code http} is 80, and * {@code https} is 443. * </p> * <p> * The host of the {@code uri} should not be null, or it will be treated as an illegal param, * and an {@link IllegalArgumentException} will be thrown. * </p> */ public static IpPort parseIpPort(URI uri) { return parseIpPort(uri, false); }
public static String getPublishHostName() { String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get(); publicAddressSetting = publicAddressSetting.trim(); if (publicAddressSetting.isEmpty()) { return NetUtils.getHostName(); } if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) { return NetUtils .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1)) .getHostName(); } return publicAddressSetting; }
public static String getPublishAddress() { String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get(); publicAddressSetting = publicAddressSetting.trim(); if (publicAddressSetting.isEmpty()) { return NetUtils.getHostAddress(); } // placeholder is network interface name if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) { return NetUtils .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1)) .getHostAddress(); } return publicAddressSetting; }
@Override public boolean canInit() { setListenAddressWithoutSchema(TransportConfig.getAddress()); URIEndpointObject ep = (URIEndpointObject) getEndpoint().getAddress(); if (ep == null) { return true; } if (!NetUtils.canTcpListen(ep.getSocketAddress().getAddress(), ep.getPort())) { LOGGER.warn( "Can not start VertxRestTransport, the port:{} may have been occupied. You can ignore this message if you are using a web container like tomcat.", ep.getPort()); return false; } return true; }
public static String getHostAddress() { //If failed to get host address ,micro-service will registry failed //So I add retry mechanism if (hostAddress == null) { doGetHostNameAndHostAddress(); } return hostAddress; }
protected void setListenAddressWithoutSchema(String addressWithoutSchema, Map<String, String> pairs) { addressWithoutSchema = genAddressWithoutSchema(addressWithoutSchema, pairs); this.endpoint = new Endpoint(this, NetUtils.getRealListenAddress(getName(), addressWithoutSchema)); if (this.endpoint.getEndpoint() != null) { this.publishEndpoint = new Endpoint(this, RegistryUtils.getPublishAddress(getName(), addressWithoutSchema)); } else { this.publishEndpoint = null; } }
private static void doGetHostNameAndHostAddress() { try { doGetIpv4AddressFromNetworkInterface(); // getLocalHost will throw exception in some docker image and sometimes will do a hostname lookup and time consuming InetAddress localHost = InetAddress.getLocalHost(); hostName = localHost.getHostName(); if ((localHost.isAnyLocalAddress() || localHost.isLoopbackAddress() || localHost.isMulticastAddress()) && !allInterfaceAddresses.isEmpty()) { InetAddress availabelAddress = allInterfaceAddresses.values().iterator().next(); hostAddress = availabelAddress.getHostAddress(); LOGGER.warn("cannot find a proper host address, choose {}, may not be correct.", hostAddress); } else { LOGGER.info("get localhost address: {}", localHost.getHostAddress()); hostAddress = localHost.getHostAddress(); } LOGGER.info( "add host name from localhost:" + hostName + ",host address:" + hostAddress); } catch (Exception 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 static IpPort genPublishIpPort(String schema, IpPort ipPort) { String publicAddressSetting = DynamicPropertyFactory.getInstance() .getStringProperty(PUBLISH_ADDRESS, "") .get(); publicAddressSetting = publicAddressSetting.trim(); if (publicAddressSetting.isEmpty()) { InetSocketAddress socketAddress = ipPort.getSocketAddress(); if (socketAddress.getAddress().isAnyLocalAddress()) { String host = NetUtils.getHostAddress(); LOGGER.warn("address {}, auto select a host address to publish {}:{}, maybe not the correct one", socketAddress, host, socketAddress.getPort()); return new IpPort(host, ipPort.getPort()); } return ipPort; } if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) { publicAddressSetting = NetUtils .ensureGetInterfaceAddress( publicAddressSetting.substring(1, publicAddressSetting.length() - 1)) .getHostAddress(); } String publishPortKey = PUBLISH_PORT.replace("{transport_name}", schema); int publishPortSetting = DynamicPropertyFactory.getInstance() .getIntProperty(publishPortKey, 0) .get(); int publishPort = publishPortSetting == 0 ? ipPort.getPort() : publishPortSetting; return new IpPort(publicAddressSetting, publishPort); }
public static String getPublishHostName() { String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get(); publicAddressSetting = publicAddressSetting.trim(); if (publicAddressSetting.isEmpty()) { return NetUtils.getHostName(); } if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) { return NetUtils .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1)) .getHostName(); } return publicAddressSetting; }
@Override public boolean canInit() { setListenAddressWithoutSchema(TransportConfig.getAddress()); URIEndpointObject ep = (URIEndpointObject) getEndpoint().getAddress(); if (ep == null) { return true; } if (!NetUtils.canTcpListen(ep.getSocketAddress().getAddress(), ep.getPort())) { LOGGER.warn( "Can not start VertxRestTransport, the port:{} may have been occupied. You can ignore this message if you are using a web container like tomcat.", ep.getPort()); return false; } return true; }
public static String getHostName() { //If failed to get host name ,micro-service will registry failed //So I add retry mechanism if (hostName == null) { doGetHostNameAndHostAddress(); } return hostName; }
protected void setListenAddressWithoutSchema(String addressWithoutSchema, Map<String, String> pairs) { addressWithoutSchema = genAddressWithoutSchema(addressWithoutSchema, pairs); this.endpoint = new Endpoint(this, NetUtils.getRealListenAddress(getName(), addressWithoutSchema)); if (this.endpoint.getEndpoint() != null) { this.publishEndpoint = new Endpoint(this, RegistryUtils.getPublishAddress(getName(), addressWithoutSchema)); } else { this.publishEndpoint = null; } }
private static void doGetHostNameAndHostAddress() { try { doGetIpv4AddressFromNetworkInterface(); // getLocalHost will throw exception in some docker image and sometimes will do a hostname lookup and time consuming InetAddress localHost = InetAddress.getLocalHost(); hostName = localHost.getHostName(); if ((localHost.isAnyLocalAddress() || localHost.isLoopbackAddress() || localHost.isMulticastAddress()) && !allInterfaceAddresses.isEmpty()) { InetAddress availabelAddress = allInterfaceAddresses.values().iterator().next(); hostAddress = availabelAddress.getHostAddress(); LOGGER.warn("cannot find a proper host address, choose {}, may not be correct.", hostAddress); } else { hostAddress = localHost.getHostAddress(); } LOGGER.info( "add host name from localhost:" + hostName + ",host address:" + hostAddress); } catch (Exception 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); } } }
public void doWatch(String configCenter) throws UnsupportedEncodingException, InterruptedException { CountDownLatch waiter = new CountDownLatch(1); IpPort ipPort = NetUtils.parseIpPortFromURI(configCenter); String url = uriConst.REFRESH_ITEMS + "?dimensionsInfo=" + StringUtils.deleteWhitespace(URLEncoder.encode(serviceName, "UTF-8"));
public static IpPort parseIpPort(String scheme, String authority) { if (authority == null) { return null; } return parseIpPort(URI.create(scheme + "://" + authority)); }
public static String getPublishAddress() { String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get(); publicAddressSetting = publicAddressSetting.trim(); if (publicAddressSetting.isEmpty()) { return NetUtils.getHostAddress(); } // placeholder is network interface name if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) { return NetUtils .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1)) .getHostAddress(); } return publicAddressSetting; }
public static String getHostAddress() { //If failed to get host address ,micro-service will registry failed //So I add retry mechanism if (hostAddress == null) { doGetHostNameAndHostAddress(); } return hostAddress; }
+ ParseConfigUtils.getInstance().getCurrentVersionInfo(); clientMgr.findThreadBindClientPool().runOnContext(client -> { IpPort ipPort = NetUtils.parseIpPortFromURI(configcenter); HttpClientRequest request = client.get(ipPort.getPort(), ipPort.getHostOrIp(), path, rsp -> { if (rsp.statusCode() == HttpResponseStatus.OK.code()) {