Refine search
@Override public DrillbitEndpoint apply( ServiceInstance<DrillbitEndpoint> input) { return input.getPayload(); } });
if (!allowDuplicateRegistrations) { try { for (ServiceInstance<IgniteInstanceDetails> sd : discovery.queryForInstances(serviceName)) registrationsToIgnore.add(new InetSocketAddress(sd.getAddress(), sd.getPort())); ServiceInstance<IgniteInstanceDetails> si = ServiceInstance.<IgniteInstanceDetails>builder() .name(serviceName) .uriSpec(URI_SPEC) .address(addr.getAddress().getHostAddress()) .port(addr.getPort()) .build(); discovery.registerService(si);
instance.getPort() >= 0 || (instance.getSslPort() != null && instance.getSslPort() >= 0), "WTH?! Both port and sslPort not set" ); final int port; final String scheme; if (instance.getSslPort() == null) { port = instance.getPort(); scheme = "http"; } else { port = instance.getSslPort() >= 0 ? instance.getSslPort() : instance.getPort(); scheme = instance.getSslPort() >= 0 ? "https" : "http";
/** {@inheritDoc} */ @Override public Collection<InetSocketAddress> getRegisteredAddresses() throws IgniteSpiException { init(); if (log.isDebugEnabled()) log.debug("Getting registered addresses from ZooKeeper IP Finder."); Collection<ServiceInstance<IgniteInstanceDetails>> serviceInstances; try { serviceInstances = discovery.queryForInstances(serviceName); } catch (Exception e) { log.warning("Error while getting registered addresses from ZooKeeper IP Finder.", e); return Collections.emptyList(); } Set<InetSocketAddress> answer = new HashSet<>(); for (ServiceInstance<IgniteInstanceDetails> si : serviceInstances) answer.add(new InetSocketAddress(si.getAddress(), si.getPort())); if (log.isInfoEnabled()) log.info("ZooKeeper IP Finder resolved addresses: " + answer); return answer; }
private ServiceInstance<ModelEndpoint> createInstance(ModelEndpoint ep) throws Exception { URL url = new URL(ep.getEndpoint().getUrl()); ServiceInstanceBuilder<ModelEndpoint> builder = ServiceInstance.<ModelEndpoint> builder() .address(url.getHost()) .id(ep.getContainerId()) .name(ep.getName()) .port(url.getPort()) .registrationTimeUTC(System.currentTimeMillis()) .serviceType(ServiceType.STATIC) .payload(ep) ; return builder.build(); }
public ServiceInstance<JsonObject> deserialize(byte[] bytes) throws Exception { ServiceInstance rawServiceInstance = this.mapper.readValue(bytes, this.type); ServiceInstanceBuilder<JsonObject> builder = ServiceInstance.<JsonObject>builder() .address(rawServiceInstance.getAddress()) .id(rawServiceInstance.getId()) .name(rawServiceInstance.getName()) .payload(new JsonObject(rawServiceInstance.getPayload().toString())) .registrationTimeUTC(rawServiceInstance.getRegistrationTimeUTC()) .serviceType(rawServiceInstance.getServiceType()); if (rawServiceInstance.getSslPort() != null) { builder.sslPort(rawServiceInstance.getSslPort()); } if (rawServiceInstance.getPort() != null) { builder.sslPort(rawServiceInstance.getPort()); } if (rawServiceInstance.getUriSpec() != null) { builder.uriSpec(rawServiceInstance.getUriSpec()); } return builder.build(); } }
@Override public void unregister(RegistrationHandle handle) { if (!(handle instanceof ZKRegistrationHandle)) { throw new UnsupportedOperationException( "Unknown handle type: " + handle.getClass().getName()); } // when Drillbit is unregistered, clean all the listeners registered in CC. this.listeners.clear(); ZKRegistrationHandle h = (ZKRegistrationHandle) handle; try { ServiceInstance<DrillbitEndpoint> serviceInstance = ServiceInstance .<DrillbitEndpoint> builder().address("").port(0).id(h.id) .name(serviceName).build(); discovery.unregisterService(serviceInstance); } catch (Exception e) { Throwables.throwIfUnchecked(e); throw new RuntimeException(e); } }
@Override public byte[] serialize(ServiceInstance<T> instance) throws Exception { if ( compatibleSerializationMode ) { OldServiceInstance<T> compatible = new OldServiceInstance<T>(instance.getName(), instance.getId(), instance.getAddress(), instance.getPort(), instance.getSslPort(), instance.getPayload(), instance.getRegistrationTimeUTC(), instance.getServiceType(), instance.getUriSpec()); return mapper.writeValueAsBytes(compatible); } return mapper.writeValueAsBytes(instance); } }
protected ServiceInstance serviceInstance(StubConfiguration stubConfiguration, int port) { try { return ServiceInstance.builder() .uriSpec(new UriSpec(this.zookeeperDiscoveryProperties.getUriSpec())) .address("localhost").port(port).name(name(stubConfiguration)) .build(); } catch (Exception e) { throw new IllegalStateException(e); } }
@Bean @ConditionalOnMissingBean @ConditionalOnWebApplication public ServiceInstance serviceInstance(EmbeddedWebApplicationContext webContext, ServiceDiscovery serviceDiscovery) throws Exception { final Map<String, Object> details = serviceDetails(getServicePort(webContext)); final ServiceInstanceBuilder<Map<String, Object>> builder = ServiceInstance.builder(); Optional.ofNullable(details.get("port")).ifPresent(port -> builder.port((Integer) port)); final ServiceInstance<Map<String, Object>> serviceInstance = builder .id((String) details.get("id")) .name((String) details.get("name")) .payload(details) .uriSpec(new UriSpec("http://{address}:{port}")) .build(); serviceDiscovery.registerService(serviceInstance); return serviceInstance; }
public void regService(String host, int port) throws Exception { haContext.startCuratorFramework(); ServiceInstance<Void> serviceInstance = ServiceInstance.<Void>builder() .address(host) .port(port) .name(HAContext.SENTRY_SERVICE_REGISTER_NAMESPACE) .build(); InstanceSerializer<Void> instanceSerializer = new FixedJsonInstanceSerializer<Void>(Void.class); ServiceDiscoveryBuilder.builder(Void.class) .basePath(HAContext.SENTRY_SERVICE_REGISTER_NAMESPACE) .client(haContext.getCuratorFramework()) .serializer(instanceSerializer) .thisInstance(serviceInstance) .build() .start(); }
public static UnitInstance unitInstance(ServiceInstance<UnitProxy> serviceInstance) { if (serviceInstance == null) return null; UnitInstance unitInstance = new UnitInstance(); unitInstance.setId(serviceInstance.getId()); unitInstance.setAddress(serviceInstance.getAddress()); unitInstance.setEnabled(serviceInstance.isEnabled()); unitInstance.setName(serviceInstance.getName()); unitInstance.setPayload(serviceInstance.getPayload()); unitInstance.setPort(serviceInstance.getPort()); unitInstance.setRegistrationTimestamp(serviceInstance.getRegistrationTimeUTC()); return unitInstance; }
@Override public List<ServiceDefinition> getServices(String name) { if (serviceDiscovery == null) { return Collections.emptyList(); } try { return serviceDiscovery.queryForInstances(name).stream() .map(si -> { Map<String, String> meta = new HashMap<>(); ObjectHelper.ifNotEmpty(si.getPayload(), meta::putAll); meta.putIfAbsent(ServiceDefinition.SERVICE_META_NAME, si.getName()); meta.putIfAbsent(ServiceDefinition.SERVICE_META_ID, si.getId()); return new DefaultServiceDefinition( si.getName(), si.getAddress(), si.getSslPort() != null ? si.getSslPort() : si.getPort(), meta); }) .collect(Collectors.toList()); } catch (Exception e) { throw new RuntimeCamelException(e); } }
@Test public void testPick() throws Exception { EasyMock.expect(serviceProvider.getInstance()).andReturn(instance).anyTimes(); EasyMock.expect(instance.getAddress()).andReturn(ADDRESS).anyTimes(); EasyMock.expect(instance.getPort()).andReturn(PORT).anyTimes(); EasyMock.expect(instance.getSslPort()).andReturn(-1).anyTimes(); EasyMock.replay(instance, serviceProvider); Server server = serverDiscoverySelector.pick(); Assert.assertEquals(PORT, server.getPort()); Assert.assertEquals(ADDRESS, server.getAddress()); Assert.assertTrue(server.getHost().contains(Integer.toString(PORT))); Assert.assertTrue(server.getHost().contains(ADDRESS)); Assert.assertEquals("http", server.getScheme()); EasyMock.verify(instance, serviceProvider); final URI uri = new URI( server.getScheme(), null, server.getAddress(), server.getPort(), "/druid/indexer/v1/action", null, null ); Assert.assertEquals(PORT, uri.getPort()); Assert.assertEquals(ADDRESS, uri.getHost()); Assert.assertEquals("http", uri.getScheme()); }
@Override public List<ComponentInfo> getComponents(ComponentCategory category) { try { Collection<ServiceInstance<ComponentInfo>> instances = serviceDiscovery.queryForInstances(category.toString()); List<ComponentInfo> result = new ArrayList<>(instances.size()); instances.forEach(i -> result.add(i.getPayload())); return result; } catch (Exception e) { e.printStackTrace(); } return new ArrayList<ComponentInfo>(); }
private DiscoveryInstance toSoaInstance(ServiceInstance<Payload> instance) { if ( instance == null ) { return null; } Payload payload = instance.getPayload(); int port = Objects.firstNonNull(instance.getPort(), Objects.firstNonNull(instance.getSslPort(), 0)); return new DiscoveryInstanceImpl(instance.getId(), instance.getAddress(), port, instance.getSslPort() != null, payload); }
/** * @param serviceId The service id to be used * @param serviceInstance The zookeeper service instance described by this service instance */ public ZookeeperServiceInstance(String serviceId, org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance> serviceInstance) { this.serviceId = serviceId; this.serviceInstance = serviceInstance; this.host = this.serviceInstance.getAddress(); this.secure = serviceInstance.getSslPort() != null; Integer port = serviceInstance.getPort(); if (this.secure) { port = serviceInstance.getSslPort(); } this.port = port; this.uri = URI.create(serviceInstance.buildUriSpec()); if (serviceInstance.getPayload() != null) { this.metadata = serviceInstance.getPayload().getMetadata(); } else { this.metadata = new HashMap<>(); } }
/** * Verify services are registered. * * @throws Exception */ public void verifyRegistrations() throws Exception { ServiceDiscovery<MetaData> serviceDiscovery = ServiceUtil.getServiceDiscovery(curatorFramework, basePath); listInstances(serviceDiscovery); for (Map.Entry<ServiceDiscovery<MetaData>, ServiceInstance<MetaData>> entry : discoveryMap.entrySet()) { ServiceInstance<MetaData> instance = entry.getValue(); try { ServiceInstance<MetaData> found = entry.getKey().queryForInstance(instance.getName(), instance.getId()); if (found == null) { throw new RuntimeException("There is no instance for: " + instance.getName() + ":" + instance.getId() + " registered "); } log.debug(found.getName() + " is verified at: " + found.getAddress() + ":" + found.getPort()); } catch (Exception e) { log.error("Could not find service: " + (instance.getName() + ":" + instance.getId()), e); throw new RuntimeException(e); } } }