Refine search
@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); } }
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();
@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 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(); } }
@Test public void testNoUriSpec() throws Exception { ServiceInstance<JsonObject> instance = ServiceInstance.<JsonObject>builder() .name("foo-service") .payload(new JsonObject().put("foo", "bar")) .sslPort(42406) .address("localhost") .build(); Record record = ZookeeperServiceImporter.createRecordForInstance(instance); assertThat(record.getName()).isEqualTo("foo-service"); assertThat(record.getType()).isEqualTo("unknown"); assertThat(record.getMetadata().getString("foo")).isEqualTo("bar"); assertThat(record.getMetadata().getString("zookeeper-id")).isNotEmpty(); assertThat(record.getLocation()) .contains(entry("endpoint", "https://localhost:42406")) .contains(entry("ssl-port", 42406)); }
@Test public void testUnknownRecordCreation() throws Exception { UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{ssl-port}"); ServiceInstance<JsonObject> instance = ServiceInstance.<JsonObject>builder() .name("foo-service") .payload(new JsonObject().put("foo", "bar")) .sslPort(42406) .uriSpec(uriSpec) .build(); Record record = ZookeeperServiceImporter.createRecordForInstance(instance); assertThat(record.getName()).isEqualTo("foo-service"); assertThat(record.getType()).isEqualTo("unknown"); assertThat(record.getMetadata().getString("foo")).isEqualTo("bar"); assertThat(record.getMetadata().getString("zookeeper-id")).isNotEmpty(); assertThat(record.getLocation()) .contains(entry("endpoint", "https://foo.com:42406")) .contains(entry("ssl-port", 42406)); }
@Override public void contextInitialized(ServletContextEvent sce) { try { ServiceInstanceBuilder<String> builder = ServiceInstance.<String>builder() .name(serviceName) .uriSpec(new UriSpec("{scheme}://{address}:{port}")); if(ssl) { builder.sslPort(port); } else { builder.port(port); } if(address != null) { builder.address(address); } instance = builder.build(); log.info(String.format("Service Registration :: %s -> %s", serviceName, instance.buildUriSpec())); Discovery.getInstance().registerService(instance); } catch (Exception e) { log.error("", e); } }
@Override public ServiceInstance<String> getInstance(String serviceName) throws Exception { String url = Configuration.getInstance().getString(serviceName); if(url == null) { return null; } return ServiceInstance.<String>builder() .name(serviceName) .payload("") .address(url) .uriSpec(new UriSpec("{address}")) .build(); }
/** * Return a new builder. The {@link #address} is set to the ip of the first * NIC in the system. The {@link #id} is set to a random UUID. * * @return builder * @throws Exception errors getting the local IP */ public static<T> ServiceInstanceBuilder<T>builder() throws Exception { String address = null; Collection<InetAddress> ips = ServiceInstanceBuilder.getAllLocalIPs(); if ( ips.size() > 0 ) { address = ips.iterator().next().getHostAddress(); // default to the first address } String id = UUID.randomUUID().toString(); return new ServiceInstanceBuilder<T>().address(address).id(id).registrationTimeUTC(System.currentTimeMillis()); }
public RegistrationBuilder port(int port) { this.builder.port(port); return this; }
public RegistrationBuilder name(String name) { this.builder.name(name); return this; }
protected void build() { this.serviceInstance = this.builder.build(); }
public RegistrationBuilder address(String address) { this.builder.address(address); return this; }
public RegistrationBuilder payload(ZookeeperInstance payload) { this.builder.payload(payload); return this; }
public RegistrationBuilder id(String id) { this.builder.id(id); return this; }
public RegistrationBuilder uriSpec(UriSpec uriSpec) { this.builder.uriSpec(uriSpec); return this; }
public RegistrationBuilder registrationTimeUTC(long registrationTimeUTC) { this.builder.registrationTimeUTC(registrationTimeUTC); return this; }
public RegistrationBuilder serviceType(ServiceType serviceType) { this.builder.serviceType(serviceType); return this; }
private ServiceInstance<ComponentInfo> createServiceInstance(Component component) throws ComponentRegistrationException { // in a real application, you'd have a convention of some kind for the URI // layout UriSpec uriSpec = new UriSpec("{scheme}://{hostName}:{port}"); ComponentInfo componentInfo = component.getComponentInfo(); try { ServiceInstance<ComponentInfo> inst = ServiceInstance.<ComponentInfo>builder() .name(component.getCategory().getName()) .id(component.getId()) .payload(componentInfo) .port((int) (65535 * Math.random())) // in a real application, you'd use a common port .uriSpec(uriSpec).build(); componentInfo.setHostName(inst.getAddress()); //componentInfo.setUriSpec(uriSpec.toString()); return inst; } catch (Exception e) { throw new ComponentRegistrationException(e); } }