public RegistrationBuilder defaultUriSpec() { this.builder.uriSpec(new UriSpec(DEFAULT_URI_SPEC)); return this; } }
public RegistrationBuilder uriSpec(String uriSpec) { this.builder.uriSpec(new UriSpec(uriSpec)); return this; }
@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; }
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); } }
@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(); }
private void prepareServiceInstance() throws Exception { Address privateAddress = thisNode.getPrivateAddress(); serviceInstance = ServiceInstance.<Void>builder() .uriSpec(new UriSpec("{scheme}://{address}:{port}")) .address(privateAddress.getHost()) .port(privateAddress.getPort()) .name(group) .build(); }
@Test public void testHttpRecordCreation() throws Exception { UriSpec uriSpec = new UriSpec("{scheme}://example.com:{port}/foo"); ServiceInstance<JsonObject> instance = ServiceInstance.<JsonObject>builder() .name("foo-service") .payload(new JsonObject().put("foo", "bar").put("service-type", "http-endpoint")) .port(8080) .uriSpec(uriSpec) .build(); Record record = ZookeeperServiceImporter.createRecordForInstance(instance); assertThat(record.getName()).isEqualTo("foo-service"); assertThat(record.getType()).isEqualTo("http-endpoint"); assertThat(record.getMetadata().getString("foo")).isEqualTo("bar"); assertThat(record.getMetadata().getString("zookeeper-id")).isNotEmpty(); assertThat(record.getLocation()) .contains(entry("endpoint", "http://example.com:8080/foo")) .contains(entry("port", 8080)); }
@Test public void testHttpRecordCreation() throws Exception { UriSpec uriSpec = new UriSpec("{scheme}://example.com:{port}/foo"); ServiceInstance<JsonObject> instance = ServiceInstance.<JsonObject>builder() .name("foo-service") .payload(new JsonObject().put("foo", "bar").put("service-type", "http-endpoint")) .port(8080) .uriSpec(uriSpec) .build(); Record record = ZookeeperServiceImporter.createRecordForInstance(instance); assertThat(record.getName()).isEqualTo("foo-service"); assertThat(record.getType()).isEqualTo("http-endpoint"); assertThat(record.getMetadata().getString("foo")).isEqualTo("bar"); assertThat(record.getMetadata().getString("zookeeper-id")).isNotEmpty(); assertThat(record.getLocation()) .contains(entry("endpoint", "http://example.com:8080/foo")) .contains(entry("port", 8080)); }
@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)); }
@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)); }
@Test public void testRegistration(TestContext tc) throws Exception { Async async = tc.async(); UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); ServiceInstance<String> instance = ServiceInstance.<String>builder() .name("foo-service") .payload(new JsonObject().put("foo", "bar").encodePrettily()) .port((int) (65535 * Math.random())) .uriSpec(uriSpec) .build(); discovery.registerService(instance); sd.registerServiceImporter( new ZookeeperServiceImporter(), new JsonObject().put("connection", zkTestServer.getConnectString()), v -> { if (v.failed()) { v.cause().printStackTrace(); } tc.assertTrue(v.succeeded()); sd.getRecords(x -> true, l -> { if (l.failed()) { l.cause().printStackTrace(); } tc.assertTrue(l.succeeded()); tc.assertTrue(l.result().size() == 1); tc.assertEquals("foo-service", l.result().get(0).getName()); async.complete(); }); }); }
@Test public void testRegistration(TestContext tc) throws Exception { Async async = tc.async(); UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); ServiceInstance<String> instance = ServiceInstance.<String>builder() .name("foo-service") .payload(new JsonObject().put("foo", "bar").encodePrettily()) .port((int) (65535 * Math.random())) .uriSpec(uriSpec) .build(); discovery.registerService(instance); sd.registerServiceImporter( new ZookeeperServiceImporter(), new JsonObject().put("connection", zkTestServer.getConnectString()), v -> { if (v.failed()) { v.cause().printStackTrace(); } tc.assertTrue(v.succeeded()); sd.getRecords(x -> true, l -> { if (l.failed()) { l.cause().printStackTrace(); } tc.assertTrue(l.succeeded()); tc.assertTrue(l.result().size() == 1); tc.assertEquals("foo-service", l.result().get(0).getName()); async.complete(); }); }); }
public ExampleServer(CuratorFramework client, String path, String serviceName, String description) throws Exception { // in a real application, you'd have a convention of some kind for the // URI layout UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); thisInstance = ServiceInstance.<InstanceDetails> builder().name(serviceName).payload(new InstanceDetails(description)) .port((int) (65535 * Math.random())) // in a real application, // you'd use a common // port .uriSpec(uriSpec).build(); // if you mark your payload class with @JsonRootName the provided // JsonInstanceSerializer will work JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class); serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(path).serializer(serializer) .thisInstance(thisInstance).build(); }
public ExampleServer(CuratorFramework client, String path, String serviceName, String description) throws Exception { // in a real application, you'd have a convention of some kind for the URI layout UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); thisInstance = ServiceInstance.<InstanceDetails>builder() .name(serviceName) .payload(new InstanceDetails(description)) .port((int)(65535 * Math.random())) // in a real application, you'd use a common port .uriSpec(uriSpec) .build(); // if you mark your payload class with @JsonRootName the provided JsonInstanceSerializer will work JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class); serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class) .client(client) .basePath(path) .serializer(serializer) .thisInstance(thisInstance) .build(); }
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); } }
Async async = tc.async(); UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); ServiceInstance<String> instance = ServiceInstance.<String>builder() .name("foo-service")
Async async = tc.async(); UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); ServiceInstance<String> instance = ServiceInstance.<String>builder() .name("foo-service")
Async async = tc.async(); UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); ServiceInstance<String> instance1 = ServiceInstance.<String>builder() .name("foo-service")
Async async = tc.async(); UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); ServiceInstance<String> instance1 = ServiceInstance.<String>builder() .name("foo-service")