/** * Get the address the server has bound to. * * @return socket address the server has bound to. */ public InetSocketAddress getBindAddress() { return discoverable.getSocketAddress(); }
@Override public Discoverable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String service = jsonObj.get("service").getAsString(); String hostname = jsonObj.get("hostname").getAsString(); int port = jsonObj.get("port").getAsInt(); InetSocketAddress address = new InetSocketAddress(hostname, port); byte[] payload = context.deserialize(jsonObj.get("payload"), BYTE_ARRAY_TYPE); return new Discoverable(service, address, payload); } }
private ResolvingDiscoverable(Discoverable discoverable) { super(discoverable.getName(), discoverable.getSocketAddress(), discoverable.getPayload()); }
@Override public int compare(Discoverable o1, Discoverable o2) { int cmp = o1.getName().compareTo(o2.getName()); if (cmp != 0) { return cmp; } InetSocketAddress address1 = o1.getSocketAddress(); InetSocketAddress address2 = o2.getSocketAddress(); cmp = Bytes.compareTo(address1.getAddress().getAddress(), address2.getAddress().getAddress()); return (cmp == 0) ? Ints.compare(address1.getPort(), address2.getPort()) : cmp; } }
/** * 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); }
@Test public void testMasterDiscovery() { Injector injector = Guice.createInjector( new ConfigModule(cConf), new ZKClientModule(), new ZKDiscoveryModule() ); ZKClientService zkClient = injector.getInstance(ZKClientService.class); zkClient.startAndWait(); try { DiscoveryService discoveryService = injector.getInstance(DiscoveryService.class); DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class); // Register a master service InetSocketAddress socketAddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 43210); Cancellable cancellable = discoveryService.register(new Discoverable(Constants.Service.APP_FABRIC_HTTP, socketAddr)); try { // Discover the master service Discoverable discoverable = new RandomEndpointStrategy( () -> discoveryServiceClient.discover(Constants.Service.APP_FABRIC_HTTP)).pick(10, TimeUnit.SECONDS); Assert.assertNotNull(discoverable); Assert.assertEquals(Constants.Service.APP_FABRIC_HTTP, discoverable.getName()); Assert.assertEquals(socketAddr, discoverable.getSocketAddress()); } finally { cancellable.cancel(); } } finally { zkClient.stopAndWait(); } }
@Override public void finished(Throwable failureCause) { if (failureCause != null) { LOG.error("Finished with failure for Stream handler instance {}", discoverableSupplier.get().getName(), failureCause); } } }
@Override public boolean apply(Discoverable input) { return Arrays.equals(version, input.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); }
@Override public int compare(Discoverable o1, Discoverable o2) { int cmp = o1.getName().compareTo(o2.getName()); if (cmp != 0) { return cmp; } InetSocketAddress address1 = o1.getSocketAddress(); InetSocketAddress address2 = o2.getSocketAddress(); cmp = Bytes.compareTo(address1.getAddress().getAddress(), address2.getAddress().getAddress()); return (cmp == 0) ? Ints.compare(address1.getPort(), address2.getPort()) : cmp; } }
@Override public boolean contains(Discoverable discoverable) { // If the name doesn't match, it shouldn't be in the list. return discoverable.getName().equals(name) && discoverables.get().contains(discoverable); }
/** * 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); } }
@Override public Discoverable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String service = jsonObj.get("service").getAsString(); String hostname = jsonObj.get("hostname").getAsString(); int port = jsonObj.get("port").getAsInt(); byte[] payload = context.deserialize(jsonObj.get("payload"), BYTE_ARRAY_TYPE); InetSocketAddress address = new InetSocketAddress(hostname, port); return new Discoverable(service, address, payload); }
private boolean isValid(Discoverable endpoint) { for (Discoverable discoverable : serviceDiscovered) { if (discoverable.getSocketAddress().equals(endpoint.getSocketAddress())) { return true; } } return false; } }
private ResolvingDiscoverable(Discoverable discoverable) { super(discoverable.getName(), discoverable.getSocketAddress(), discoverable.getPayload()); }
@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; } } }
/** * Generate unique node path for a given {@link Discoverable}. * @param discoverable An instance of {@link Discoverable}. * @return A node name based on the discoverable. */ private String getNodePath(Discoverable discoverable) { InetSocketAddress socketAddress = discoverable.getSocketAddress(); String node = Hashing.md5() .newHasher() .putBytes(socketAddress.getAddress().getAddress()) .putInt(socketAddress.getPort()) .hash().toString(); return String.format("/%s/%s", discoverable.getName(), node); }
@Override public boolean contains(Discoverable discoverable) { // If the name doesn't match, it shouldn't be in the list. return discoverable.getName().equals(name) && discoverables.get().contains(discoverable); }
while (iterator.hasNext()) { Discoverable candidate = iterator.next(); String version = Bytes.toString(candidate.getPayload()); int sign = 1; if (!routeConfig.isValid()) {
@Override public Discoverable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String service = jsonObj.get("service").getAsString(); String hostname = jsonObj.get("hostname").getAsString(); int port = jsonObj.get("port").getAsInt(); InetSocketAddress address = new InetSocketAddress(hostname, port); byte[] payload = context.deserialize(jsonObj.get("payload"), BYTE_ARRAY_TYPE); return new Discoverable(service, address, payload); } }