Java Code Examples for io.vertx.serviceproxy.ProxyHelper#registerService()
The following examples show how to use
io.vertx.serviceproxy.ProxyHelper#registerService() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaRegistrationTest.java From vertx-graphql-service-discovery with Apache License 2.0 | 6 votes |
@Before public void setUp() { vertx = Vertx.vertx(); options = new ServiceDiscoveryOptions().setName("theDiscovery") .setAnnounceAddress("theAnnounceAddress").setUsageAddress("theUsageAddress"); discovery = ServiceDiscovery.create(vertx, options); record = new Record() .setName("theRecord") .setType(Queryable.SERVICE_TYPE) .setMetadata(new JsonObject().put("publisherId", "thePublisherId")) .setLocation(new JsonObject().put(Record.ENDPOINT, Queryable.ADDRESS_PREFIX + ".DroidQueries")) .setStatus(Status.UP); definition = SchemaDefinition.createInstance(droidsSchema, SchemaMetadata.create(new JsonObject().put("publisherId", "thePublisherId"))); consumer = ProxyHelper.registerService(Queryable.class, vertx, definition, Queryable.ADDRESS_PREFIX + ".DroidQueries"); }
Example 2
Source File: ServiceDiscoveryRestEndpointTest.java From vertx-service-discovery with Apache License 2.0 | 6 votes |
@Test public void testUpdateWithUUIDMismatch() throws UnsupportedEncodingException { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setLocation(new JsonObject().put(Record.ENDPOINT, "address")); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); Record retrieved = retrieve(record.getRegistration()); assertThat(retrieved.getStatus()).isEqualTo(Status.UP); retrieved.setStatus(Status.OUT_OF_SERVICE).setRegistration("not-the-right-one").getMetadata().put("foo", "bar"); Restafari.Response response = given().body(retrieved.toJson().toString()) .put("/discovery/" + record.getRegistration()); assertThat(response.getStatusCode()).isEqualTo(400); }
Example 3
Source File: CartVerticle.java From vertx-blueprint-microservice with Apache License 2.0 | 6 votes |
@Override public void start(Future<Void> future) throws Exception { super.start(); // create the service instance this.shoppingCartService = new ShoppingCartServiceImpl(vertx, discovery, config()); this.checkoutService = CheckoutService.createService(vertx, discovery); // register the service proxy on event bus ProxyHelper.registerService(CheckoutService.class, vertx, checkoutService, CheckoutService.SERVICE_ADDRESS); ProxyHelper.registerService(ShoppingCartService.class, vertx, shoppingCartService, ShoppingCartService.SERVICE_ADDRESS); // publish the service in the discovery infrastructure publishEventBusService(CheckoutService.SERVICE_NAME, CheckoutService.SERVICE_ADDRESS, CheckoutService.class) .compose(servicePublished -> publishEventBusService(ShoppingCartService.SERVICE_NAME, ShoppingCartService.SERVICE_ADDRESS, ShoppingCartService.class)) .compose(servicePublished -> publishMessageSource("shopping-payment-message-source", CheckoutService.PAYMENT_EVENT_ADDRESS)) .compose(sourcePublished -> publishMessageSource("shopping-order-message-source", CheckoutService.ORDER_EVENT_ADDRESS)) .compose(sourcePublished -> deployRestVerticle()) .setHandler(future.completer()); }
Example 4
Source File: SchemaMessageConsumers.java From vertx-graphql-service-discovery with Apache License 2.0 | 5 votes |
public <T extends Queryable> MessageConsumer<JsonObject> registerServiceConsumer(String address, T implementation) { MessageConsumer<JsonObject> serviceConsumer; if (!messageConsumers.containsKey(address)) { serviceConsumer = ProxyHelper.registerService(Queryable.class, vertx, implementation, address); messageConsumers.put(address, serviceConsumer); } else { serviceConsumer = messageConsumers.get(address); } consumerRegistrations.add(address); return serviceConsumer; }
Example 5
Source File: ServiceProxiesTest.java From vertx-lang-groovy with Apache License 2.0 | 5 votes |
@Test public void testWithGroovyConsumer() { // Step 1 - register the service HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = EventBusService.createRecord("Hello", "address", HelloService.class); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); // Step 2 - register a consumer that get the result AtomicReference<JsonObject> result = new AtomicReference<>(); vertx.eventBus().<JsonObject>consumer("result", message -> result.set(message.body())); // Step 3 - deploy the verticle vertx.deployVerticle("discovery/verticles/HelloServiceConsumer.groovy", ar -> { if (ar.failed()) { // Will fail anyway. ar.cause().printStackTrace(); } }); await().until(() -> result.get() != null); assertThat(result.get().getString("status")).isEqualTo("ok"); assertThat(result.get().getString("message")).isEqualTo("stuff vert.x"); }
Example 6
Source File: ServiceDiscoveryRestEndpointTest.java From vertx-service-discovery with Apache License 2.0 | 5 votes |
@Test public void testThatWeGetTheTwoPublishedServicesWithMetadata() { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record1 = EventBusService.createRecord("Hello", "address", HelloService.class, new JsonObject().put("key", "foo")); Record record2 = EventBusService.createRecord("Hello-2", "address", HelloService.class, new JsonObject().put("key", "bar")); discovery.publish(record1, (r) -> { }); discovery.publish(record2, (r) -> { }); await().until(() -> record1.getRegistration() != null); await().until(() -> record2.getRegistration() != null); Restafari.Response response = get("/discovery"); JsonArray services = new JsonArray(response.asString()); assertThat(services.size()).isEqualTo(2); for (Object json : services) { Record rec = new Record((JsonObject) json); assertThat(rec.getStatus()).isEqualTo(Status.UP); assertThat(rec.getRegistration()).isNotNull(); assertThat(rec.getName()).startsWith("Hello"); assertThat(rec.getMetadata().getString("key")).isNotNull(); get("/discovery/" + rec.getRegistration()).then().body("name", not(nullValue())); } }
Example 7
Source File: StoreVerticle.java From vertx-blueprint-microservice with Apache License 2.0 | 5 votes |
@Override public void start(Future<Void> future) throws Exception { super.start(); crudService = new StoreCRUDServiceImpl(vertx, config()); ProxyHelper.registerService(StoreCRUDService.class, vertx, crudService, SERVICE_ADDRESS); // publish service and deploy REST verticle publishEventBusService(SERVICE_NAME, SERVICE_ADDRESS, StoreCRUDService.class) .compose(servicePublished -> deployRestVerticle(crudService)) .setHandler(future.completer()); }
Example 8
Source File: GraphQLService.java From vertx-graphql-service-discovery with Apache License 2.0 | 5 votes |
/** * Publish a GraphQL schema for querying. * <p> * On success a {@link SchemaRegistration} is returned. It contains the message consumer of the * {@link Queryable} service proxy that supplies the published {@link SchemaDefinition}, the published service * discovery record, and the {@link ServiceDiscovery} it was published to. * <p> * Note that unless invoked from a {@link SchemaPublisher} a * client needs to keep hold of the returned {@link Record} as long as it is published. * * @param vertx the vert.x instance * @param discovery the service discovery instance * @param definition the service proxy instance exposing the graphql schema * @param resultHandler the result handler that returns the registration */ static void publish(Vertx vertx, ServiceDiscovery discovery, SchemaDefinition definition, Handler<AsyncResult<SchemaRegistration>> resultHandler) { Objects.requireNonNull(vertx, "Vertx cannot be null"); Objects.requireNonNull(discovery, "Service discovery cannot be null"); Objects.requireNonNull(definition, "GraphQL queryable cannot be null"); Objects.requireNonNull(resultHandler, "Publication result handler cannot be null"); // TODO Caching proxy ok? final MessageConsumer<JsonObject> serviceConsumer; if (definition.metadata().get("publisherId") == null) { serviceConsumer = ProxyHelper.registerService( Queryable.class, vertx, definition, definition.serviceAddress()); } else { // Publisher handles service instantiation, manages consumer. serviceConsumer = null; } Record record = new Record() .setType(SERVICE_TYPE) .setName(definition.schemaName()) .setMetadata(definition.metadata().toJson()) .setLocation(new JsonObject().put(Record.ENDPOINT, definition.serviceAddress())); discovery.publish(record, rh -> { if (rh.succeeded()) { resultHandler.handle(Future.succeededFuture( SchemaRegistration.create(discovery, null, rh.result(), definition, serviceConsumer))); } else { resultHandler.handle(Future.failedFuture(rh.cause())); } }); }
Example 9
Source File: ServiceDiscoveryRestEndpointTest.java From vertx-service-discovery with Apache License 2.0 | 5 votes |
@Test public void testFailedPublication() { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setRegistration("this-is-not-allowed") .setLocation(new JsonObject().put(Record.ENDPOINT, "address")); Restafari.Response response = given().request().body(record.toJson().toString()).post("/discovery"); assertThat(response.getStatusCode()).isEqualTo(500); }
Example 10
Source File: ProductVerticle.java From vertx-blueprint-microservice with Apache License 2.0 | 5 votes |
@Override public void start(Future<Void> future) throws Exception { super.start(); // create the service instance ProductService productService = new ProductServiceImpl(vertx, config()); // register the service proxy on event bus ProxyHelper.registerService(ProductService.class, vertx, productService, SERVICE_ADDRESS); // publish the service in the discovery infrastructure initProductDatabase(productService) .compose(databaseOkay -> publishEventBusService(ProductService.SERVICE_NAME, SERVICE_ADDRESS, ProductService.class)) .compose(servicePublished -> deployRestService(productService)) .setHandler(future.completer()); }
Example 11
Source File: PortfolioVerticle.java From microtrader with MIT License | 5 votes |
@Override public void start() { super.start(); // Create the service object PortfolioServiceImpl service = new PortfolioServiceImpl(vertx, discovery, config().getDouble("money", 10000.00)); // Register the service proxy on the event bus ProxyHelper.registerService(PortfolioService.class, vertx, service, ADDRESS); // Publish it in the discovery infrastructure publishEventBusService("portfolio", ADDRESS, PortfolioService.class, ar -> { if (ar.failed()) { ar.cause().printStackTrace(); } else { System.out.println("Portfolio service published : " + ar.succeeded()); } }); publishMessageSource("portfolio-events", EVENT_ADDRESS, ar -> { if (ar.failed()) { ar.cause().printStackTrace(); } else { System.out.println("Portfolio Events service published : " + ar.succeeded()); } }); // Java traders vertx.deployVerticle(CompulsiveTraderVerticle.class.getName(), new DeploymentOptions().setInstances(3)); }
Example 12
Source File: DiscoveryImplTestBase.java From vertx-service-discovery with Apache License 2.0 | 5 votes |
@Test public void testAnnounce() { List<Record> announces = new ArrayList<>(); vertx.eventBus().consumer(ServiceDiscoveryOptions.DEFAULT_ANNOUNCE_ADDRESS, msg -> announces.add(new Record((JsonObject) msg.body()))); HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setMetadata(new JsonObject().put("key", "A")) .setLocation(new JsonObject().put(Record.ENDPOINT, "address")); Record record2 = new Record() .setName("Hello-2") .setMetadata(new JsonObject().put("key", "B")) .setLocation(new JsonObject().put(Record.ENDPOINT, "address2")); discovery.publish(record, (r) -> { }); discovery.publish(record2, (r) -> { }); await().until(() -> record.getRegistration() != null); await().until(() -> record2.getRegistration() != null); await().until(() -> announces.size() == 2); for (Record rec : announces) { assertThat(rec.getStatus()).isEqualTo(Status.UP); } discovery.unpublish(record2.getRegistration(), v -> { }); await().until(() -> announces.size() == 3); assertThat(announces.get(2).getStatus()).isEqualTo(Status.DOWN); }
Example 13
Source File: CacheComponentVerticle.java From vertx-blueprint-microservice with Apache License 2.0 | 5 votes |
@Override public void start(Future<Void> future) throws Exception { super.start(); // create the service instance CounterService counterService = new DefaultCounterServiceImpl(vertx, config()); // register the service proxy on event bus ProxyHelper.registerService(CounterService.class, vertx, counterService, CounterService.SERVICE_ADDRESS); // publish the service in the discovery infrastructure publishEventBusService(CounterService.SERVICE_NAME, CounterService.SERVICE_ADDRESS, CounterService.class) .setHandler(future.completer()); }
Example 14
Source File: DiscoveryImplTestBase.java From vertx-service-discovery with Apache License 2.0 | 4 votes |
@Test public void testPublicationAndFilteredLookup() { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setMetadata(new JsonObject().put("key", "A")) .setLocation(new JsonObject().put(Record.ENDPOINT, "address")); Record record2 = new Record() .setName("Hello-2") .setMetadata(new JsonObject().put("key", "B")) .setLocation(new JsonObject().put(Record.ENDPOINT, "address2")); discovery.publish(record, (r) -> { }); discovery.publish(record2, (r) -> { }); await().until(() -> record.getRegistration() != null); await().until(() -> record2.getRegistration() != null); AtomicReference<Record> found = new AtomicReference<>(); discovery.getRecord(new JsonObject().put("key", "A"), ar -> { found.set(ar.result()); }); await().until(() -> found.get() != null); assertThat(found.get().getLocation().getString("endpoint")).isEqualTo("address"); found.set(null); discovery.getRecord(new JsonObject().put("key", "B"), ar -> { found.set(ar.result()); }); await().until(() -> found.get() != null); assertThat(found.get().getLocation().getString("endpoint")).isEqualTo("address2"); found.set(null); AtomicBoolean done = new AtomicBoolean(); discovery.getRecord(new JsonObject().put("key", "C"), ar -> { found.set(ar.result()); done.set(true); }); await().untilAtomic(done, is(true)); assertThat(found.get()).isNull(); found.set(null); done.set(false); discovery.getRecord(new JsonObject().put("key", "B").put("foo", "bar"), ar -> { found.set(ar.result()); done.set(true); }); await().untilAtomic(done, is(true)); assertThat(found.get()).isNull(); }
Example 15
Source File: KafkaProducerServiceVerticle.java From vertx-kafka-service with Apache License 2.0 | 4 votes |
@Override public void start() { // Get the address of EventBus where the message was published final String address = config().getString(ADDRESS); if(Strings.isNullOrEmpty(address)) { throw new IllegalStateException("address must be specified in config"); } // Get the address of EventBus where the message was published final String topic = config().getString(DEFAULT_TOPIC); if(Strings.isNullOrEmpty(topic)) { throw new IllegalStateException("topic must be specified in config"); } final JsonObject statsDConfig = config().getJsonObject(STATSD); StatsDConfiguration statsDConfiguration = null; if (statsDConfig != null) { final String prefix = statsDConfig.getString(PREFIX, PREFIX_DEFAULT); final String host = statsDConfig.getString(HOST, HOST_DEFAULT); final int port = statsDConfig.getInteger(PORT, PORT_DEFAULT); statsDConfiguration = new StatsDConfiguration(host, port, prefix); } final KafkaProducerConfiguration kafkaProducerConfiguration = new KafkaProducerConfiguration( topic, config().getString(BOOTSTRAP_SERVERS, BOOTSTRAP_SERVERS_DEFAULT), config().getString(ACKS, ACKS_DEFAULT), config().getInteger(RETRIES, RETRIES_DEFAULT), config().getInteger(REQUEST_TIMEOUT_MS, REQUEST_TIMEOUT_MS_DEFAULT), config().getInteger(MAX_BLOCK_MS, MAX_BLOCK_MS_DEFAULT), COMMA_LIST_SPLITTER.splitToList(config().getString(METRIC_CONSUMER_CLASSES, "")), config().getString(METRIC_DROPWIZARD_REGISTRY_NAME)); kafkaProducerConfiguration.setStatsDConfiguration(statsDConfiguration); final String type = config().getString(TYPE); if(!Strings.isNullOrEmpty(type)) { kafkaProducerConfiguration.setType(ProducerType.valueOf(type)); } final Integer maxRetries = config().getInteger(MAX_RETRIES); if(maxRetries != null) { kafkaProducerConfiguration.setMaxRetries(maxRetries); } final Integer retryBackoffMs = config().getInteger(RETRY_BACKOFF_MS); if(retryBackoffMs != null) { kafkaProducerConfiguration.setRetryBackoffMs(retryBackoffMs); } final Integer bufferingMaxMs = config().getInteger(BUFFERING_MAX_MS); if(bufferingMaxMs != null) { kafkaProducerConfiguration.setBufferingMaxMs(bufferingMaxMs); } final Integer bufferingMaxMessages = config().getInteger(BUFFERING_MAX_MESSAGES); if(bufferingMaxMessages != null) { kafkaProducerConfiguration.setBufferingMaxMessages(bufferingMaxMessages); } final Integer enqueueTimeout = config().getInteger(ENQUEUE_TIMEOUT); if(enqueueTimeout != null) { kafkaProducerConfiguration.setEnqueueTimeout(enqueueTimeout); } final Integer batchMessageNum = config().getInteger(BATCH_MESSAGE_NUM); if(batchMessageNum != null) { kafkaProducerConfiguration.setBatchMessageNum(batchMessageNum); } kafkaProducerService = new DefaultKafkaProducerService(kafkaProducerConfiguration); ProxyHelper.registerService(KafkaProducerService.class, vertx, kafkaProducerService, address); kafkaProducerService.start(); }
Example 16
Source File: DiscoveryImplTestBase.java From vertx-service-discovery with Apache License 2.0 | 4 votes |
@Test public void testExporter() { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setType(EventBusService.TYPE) .setLocation(new JsonObject().put(Record.ENDPOINT, "address")) .setMetadata(new JsonObject().put("foo", "foo_value_1")); TestServiceExporter exporter = new TestServiceExporter(); discovery.registerServiceExporter(exporter, new JsonObject()); discovery.publish(record, (r) -> { }); await().until(() -> exporter.state.size() > 0); String id = exporter.state.keySet().iterator().next(); assertNotNull(id); Record exported = exporter.state.get(id); assertEquals("Hello", exported.getName()); assertEquals(EventBusService.TYPE, exported.getType()); assertEquals(Status.UP, exported.getStatus()); assertEquals(new JsonObject().put(Record.ENDPOINT, "address"), exported.getLocation()); assertEquals(new JsonObject().put("foo", "foo_value_1"), exported.getMetadata()); AtomicBoolean updated = new AtomicBoolean(); discovery.update(new Record(record).setMetadata(new JsonObject().put("foo", "foo_value_2")), ar -> updated.set(true)); await().until(updated::get); assertNotSame(exporter.state.get(id), exported); exported = exporter.state.get(id); assertEquals("Hello", exported.getName()); assertEquals(EventBusService.TYPE, exported.getType()); assertEquals(Status.UP, exported.getStatus()); assertEquals(new JsonObject().put(Record.ENDPOINT, "address"), exported.getLocation()); assertEquals(new JsonObject().put("foo", "foo_value_2"), exported.getMetadata()); AtomicBoolean removed = new AtomicBoolean(); discovery.unpublish(id, ar -> removed.set(true)); await().until(removed::get); assertEquals(Collections.emptyMap(), exporter.state); discovery.close(); assertTrue(exporter.closed); }
Example 17
Source File: DiscoveryImplTestBase.java From vertx-service-discovery with Apache License 2.0 | 4 votes |
@Test public void testServiceUsage() throws InterruptedException { List<JsonObject> usages = new ArrayList<>(); vertx.eventBus().<JsonObject>consumer(ServiceDiscoveryOptions.DEFAULT_USAGE_ADDRESS, msg -> usages.add(msg.body())); HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setMetadata(new JsonObject() .put("key", "A") .put("service.interface", HelloService.class.getName())) .setType(EventBusService.TYPE) .setLocation(new JsonObject().put(Record.ENDPOINT, "address")); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); ServiceReference reference = discovery.getReference(record); await().until(() -> usages.size() == 1); assertThat(usages.get(0).getJsonObject("record").getJsonObject("location").getString(Record.ENDPOINT)) .isEqualToIgnoringCase("address"); assertThat(usages.get(0).getString("type")).isEqualTo("bind"); assertThat(usages.get(0).getString("id")).isNotNull().isNotEmpty(); assertThat((HelloService) reference.cached()).isNull(); assertThat((HelloService) reference.get()).isNotNull(); assertThat((HelloService) reference.cached()).isNotNull(); reference.release(); Assertions.assertThat(discovery.bindings()).isEmpty(); await().until(() -> usages.size() == 2); assertThat(usages.get(1).getJsonObject("record").getJsonObject("location").getString(Record.ENDPOINT)) .isEqualToIgnoringCase("address"); assertThat(usages.get(1).getString("type")).isEqualTo("release"); assertThat(usages.get(1).getString("id")).isNotNull().isNotEmpty(); // Check that even if we release the reference another time the service event is not send a second time. reference.release(); Assertions.assertThat(discovery.bindings()).isEmpty(); Thread.sleep(100); assertThat(usages).hasSize(2); }
Example 18
Source File: HelloServiceImpl.java From vertx-rx with Apache License 2.0 | 4 votes |
public void start(Vertx vertx, String address) { service = ProxyHelper.registerService(HelloService.class, vertx, this, address); }
Example 19
Source File: EchoServiceVerticle.java From weld-vertx with Apache License 2.0 | 4 votes |
@Override public void start(Future<Void> startFuture) throws Exception { super.start(startFuture); ProxyHelper.registerService(EchoService.class, vertx, new EchoServiceImpl(), "echo-service-address"); }
Example 20
Source File: HelloServiceImpl.java From vertx-service-discovery with Apache License 2.0 | 4 votes |
public void start(Vertx vertx, String address) { service = ProxyHelper.registerService(HelloService.class, vertx, this, address); }