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; }
@Override public Object parseAddress(String address) { if (address == null) { return null; } return new URIEndpointObject(address); }
/** * 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 TcpClientConnection(Context context, NetClientWrapper netClientWrapper, String strEndpoint) { this.setContext(context); this.netClientWrapper = netClientWrapper; endpoint = new URIEndpointObject(strEndpoint); this.socketAddress = endpoint.getSocketAddress(); this.remoteSupportLogin = Boolean.parseBoolean(endpoint.getFirst(TcpConst.LOGIN)); this.clientConfig = netClientWrapper.getClientConfig(endpoint.isSslEnabled()); }
@Override protected Object createEndpoint(String transportName, String endpoint, MicroserviceInstance instance) { URIEndpointObject uri = new URIEndpointObject(endpoint); return new Server(uri.getHostOrIp(), uri.getPort()); }
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; }
public ScbDiscoveryClient() { super(new InstanceDiscoveryFilter((String name, URIEndpointObject uri) -> new DefaultServiceInstance(name, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()))); }
void createRequest(IpPort ipPort, String path) { URIEndpointObject endpoint = (URIEndpointObject) invocation.getEndpoint().getAddress(); RequestOptions requestOptions = new RequestOptions(); requestOptions.setHost(ipPort.getHostOrIp()) .setPort(ipPort.getPort()) .setSsl(endpoint.isSslEnabled()) .setURI(path); HttpMethod method = getMethod(); LOGGER.debug("Sending request by rest, method={}, qualifiedName={}, path={}, endpoint={}.", method, invocation.getMicroserviceQualifiedName(), path, invocation.getEndpoint().getEndpoint()); clientRequest = httpClientWithContext.getHttpClient().request(method, requestOptions, this::handleResponse); }
@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; } }
/** * In the case that listening address configured as 0.0.0.0, the publish address will be determined * by the query result for the net interfaces. * * @return the publish address, or {@code null} if the param {@code address} is null. */ public static String getPublishAddress(String schema, String address) { if (address == null) { return address; } try { URI originalURI = new URI(schema + "://" + address); IpPort ipPort = NetUtils.parseIpPort(originalURI); if (ipPort == null) { LOGGER.warn("address {} not valid.", address); return null; } IpPort publishIpPort = genPublishIpPort(schema, ipPort); URIBuilder builder = new URIBuilder(originalURI); return builder.setHost(publishIpPort.getHostOrIp()).setPort(publishIpPort.getPort()).build().toString(); } catch (URISyntaxException e) { LOGGER.warn("address {} not valid.", address); return null; } }
@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 URIEndpointObject(String endpoint) { URI uri = URI.create(endpoint); setHostOrIp(uri.getHost()); if (uri.getPort() < 0) { // do not use default port throw new IllegalArgumentException("port not specified."); } setPort(uri.getPort()); querys = splitQuery(uri); sslEnabled = Boolean.parseBoolean(getFirst(SSL_ENABLED_KEY)); String httpversion = getFirst(PROTOCOL_KEY); if (httpversion != null && httpversion.equals(HTTP2)) { http2Enabled = true; } }
public IpPort getNextAvailableAddress(IpPort failedIpPort) { int currentIndex = currentAvailableIndex.get(); IpPort current = getAvailableAddress(currentIndex); if (current.equals(failedIpPort)) { currentAvailableIndex.compareAndSet(currentIndex, currentIndex + 1); current = getAvailableAddress(); } LOGGER.info("Change service center address from {} to {}", failedIpPort.toString(), current.toString()); return current; }
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; } }
@Override protected Object createEndpoint(String transportName, String endpoint, MicroserviceInstance instance) { URIEndpointObject uri = new URIEndpointObject(endpoint); return new Server(uri.getHostOrIp(), uri.getPort()); }
public ScbDiscoveryClient() { super(new InstanceDiscoveryFilter((String name, URIEndpointObject uri) -> new DefaultServiceInstance(name, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()))); }
public static IpPort parseIpPort(String scheme, String authority) { if (authority == null) { return null; } return parseIpPort(URI.create(scheme + "://" + authority)); }
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; }
/** * The format of address should be {@code IPv4:port} or {@code [IPv6]:port}, or {@code host:port}, * or you will not get expected result. * * Note that the IPv6 address should be wrapped by square brackets. * @return IpPort parsed from input param, or {@code null} if the param is null. */ public static IpPort parseIpPort(String address) { if (address == null) { return null; } URI uri = URI.create("http://" + address); return parseIpPort(uri, true); }
/** * @param uriAddress the address containing IP and port info. * @return IpPort parsed from input param, or {@code null} if the param is null. */ public static IpPort parseIpPortFromURI(String uriAddress) { if (uriAddress == null) { return null; } try { return parseIpPort(new URI(uriAddress)); } catch (URISyntaxException e) { return null; } }