@Override public boolean apply(Discoverable input) { return Arrays.equals(version, input.getPayload()); } };
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Discoverable other = (Discoverable) o; return name.equals(other.getName()) && address.equals(other.getSocketAddress()) && Arrays.equals(payload, other.getPayload()); }
/** * Creates a base {@link URL} for calling user service based on the given {@link Discoverable}. * * @return a {@link URL} or {@code null} if the discoverable is {@code null} or failed to construct a URL. */ @Nullable public static URL createServiceBaseURL(@Nullable Discoverable discoverable, ProgramId programId) { if (discoverable == null) { return null; } InetSocketAddress address = discoverable.getSocketAddress(); boolean ssl = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()); return createServiceBaseURL(address.getHostName(), address.getPort(), ssl, programId); }
@Nullable private URL createURL(@Nullable Discoverable discoverable, String applicationId, String serviceId) { if (discoverable == null) { return null; } InetSocketAddress address = discoverable.getSocketAddress(); String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; String path = String.format("%s%s:%d%s/namespaces/%s/apps/%s/services/%s/methods/", scheme, address.getHostName(), address.getPort(), Constants.Gateway.API_VERSION_3, namespaceId, applicationId, serviceId); try { return new URL(path); } catch (MalformedURLException e) { LOG.error("Got exception while creating serviceURL", e); return null; } } }
/** * Creates a base {@link URL} for calling user service based on the given {@link Discoverable}. * * @return a {@link URL} or {@code null} if the discoverable is {@code null} or failed to construct a URL. */ @Nullable public static URL createServiceBaseURL(@Nullable Discoverable discoverable, ProgramId programId) { if (discoverable == null) { return null; } InetSocketAddress address = discoverable.getSocketAddress(); boolean ssl = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()); return createServiceBaseURL(address.getHostName(), address.getPort(), ssl, programId); }
@Nullable private URL createURL(@Nullable Discoverable discoverable, String applicationId, String serviceId) { if (discoverable == null) { return null; } InetSocketAddress address = discoverable.getSocketAddress(); String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; String path = String.format("%s%s:%d%s/namespaces/%s/apps/%s/services/%s/methods/", scheme, address.getHostName(), address.getPort(), Constants.Gateway.API_VERSION_3, namespaceId, applicationId, serviceId); try { return new URL(path); } catch (MalformedURLException e) { LOG.error("Got exception while creating serviceURL", e); return null; } } }
@Override public JsonElement serialize(Discoverable src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("service", src.getName()); jsonObj.addProperty("hostname", src.getSocketAddress().getHostName()); jsonObj.addProperty("port", src.getSocketAddress().getPort()); jsonObj.add("payload", context.serialize(src.getPayload())); return jsonObj; }
@Override public JsonElement serialize(Discoverable src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("service", src.getName()); jsonObj.addProperty("hostname", src.getSocketAddress().getHostName()); jsonObj.addProperty("port", src.getSocketAddress().getPort()); jsonObj.add("payload", context.serialize(src.getPayload())); return jsonObj; }
@Override public JsonElement serialize(Discoverable src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("service", src.getName()); jsonObj.addProperty("hostname", src.getSocketAddress().getHostName()); jsonObj.addProperty("port", src.getSocketAddress().getPort()); jsonObj.add("payload", context.serialize(src.getPayload())); return jsonObj; } }
@Override public JsonElement serialize(Discoverable src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("service", src.getName()); jsonObj.addProperty("hostname", src.getSocketAddress().getHostName()); jsonObj.addProperty("port", src.getSocketAddress().getPort()); jsonObj.add("payload", context.serialize(src.getPayload())); return jsonObj; } }
/** * Returns the {@link SslContext} to be used for a given discoverable endpoint * * @param discoverable the endpoint to connect to * @return the {@link SslContext} or {@code null} if SSL is not needed */ @Nullable private SslHandler getSslHandler(Discoverable discoverable, ByteBufAllocator alloc) throws SSLException { if (!Arrays.equals(HTTPS_SCHEME_BYTES, discoverable.getPayload())) { return null; } SslContext context = sslContext; if (context != null) { return context.newHandler(alloc); } synchronized (this) { context = sslContext; if (context == null) { sslContext = context = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } return context.newHandler(alloc); } }
private ResolvingDiscoverable(Discoverable discoverable) { super(discoverable.getName(), discoverable.getSocketAddress(), discoverable.getPayload()); }
/** * Discover the service address, then append the base path and specified resource to get the URL. * * @param resource the resource to use * @return the resolved URL * @throws ServiceUnavailableException if the service could not be discovered */ public URL resolve(String resource) { Discoverable discoverable = endpointStrategySupplier.get().pick(1L, TimeUnit.SECONDS); if (discoverable == null) { throw new ServiceUnavailableException(discoverableServiceName); } InetSocketAddress address = discoverable.getSocketAddress(); String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; String urlStr = String.format("%s%s:%d%s%s", scheme, address.getHostName(), address.getPort(), basePath, resource); try { return new URL(urlStr); } catch (MalformedURLException e) { // shouldn't happen. If it does, it means there is some bug in the service announcer throw new IllegalStateException(String.format("Discovered service %s, but it announced malformed URL %s", discoverableServiceName, urlStr), e); } }
/** * Discover the service address, then append the base path and specified resource to get the URL. * * @param resource the resource to use * @return the resolved URL * @throws ServiceUnavailableException if the service could not be discovered */ public URL resolve(String resource) { Discoverable discoverable = endpointStrategySupplier.get().pick(1L, TimeUnit.SECONDS); if (discoverable == null) { throw new ServiceUnavailableException(discoverableServiceName); } InetSocketAddress address = discoverable.getSocketAddress(); String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; String urlStr = String.format("%s%s:%d%s%s", scheme, address.getHostName(), address.getPort(), basePath, resource); try { return new URL(urlStr); } catch (MalformedURLException e) { // shouldn't happen. If it does, it means there is some bug in the service announcer throw new IllegalStateException(String.format("Discovered service %s, but it announced malformed URL %s", discoverableServiceName, urlStr), e); } }
private ResolvingDiscoverable(Discoverable discoverable) { super(discoverable.getName(), discoverable.getSocketAddress(), discoverable.getPayload()); }
@Override public boolean isServiceAvailable() { String url = null; try { Iterable<Discoverable> discoverables = this.discoveryServiceClient.discover(getDiscoverableName()); for (Discoverable discoverable : discoverables) { String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; url = String.format("%s%s:%d/ping", scheme, discoverable.getSocketAddress().getHostName(), discoverable.getSocketAddress().getPort()); //Ping the discovered service to check its status. if (checkGetStatus(url).equals(HttpResponseStatus.OK)) { return true; } } return false; } catch (IllegalArgumentException e) { return false; } catch (Exception e) { LOG.warn("Unable to ping {} at {} : Reason : {}", serviceName, url, e.getMessage()); return false; } }
@Override public boolean isServiceAvailable() { String url = null; try { Iterable<Discoverable> discoverables = this.discoveryServiceClient.discover(getDiscoverableName()); for (Discoverable discoverable : discoverables) { String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; url = String.format("%s%s:%d/ping", scheme, discoverable.getSocketAddress().getHostName(), discoverable.getSocketAddress().getPort()); //Ping the discovered service to check its status. if (checkGetStatus(url).equals(HttpResponseStatus.OK)) { return true; } } return false; } catch (IllegalArgumentException e) { return false; } catch (Exception e) { LOG.warn("Unable to ping {} at {} : Reason : {}", serviceName, url, e.getMessage()); return false; } }
while (iterator.hasNext()) { Discoverable candidate = iterator.next(); String version = Bytes.toString(candidate.getPayload()); int sign = 1; if (!routeConfig.isValid()) {
Discoverable discoverable = endpointStrategySupplier.get().pick(3, TimeUnit.SECONDS); Assert.assertNotNull(discoverable); Assert.assertArrayEquals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()); InetSocketAddress addr = discoverable.getSocketAddress();