Search in sources :

Example 1 with ServiceDiscovery

use of io.vertx.servicediscovery.ServiceDiscovery in project vertx-examples by vert-x3.

the class ServiceDiscoveryVerticle method start.

@Override
public void start() {
    ServiceDiscovery discovery = ServiceDiscovery.create(vertx, new ServiceDiscoveryOptions().setAnnounceAddress("service-announce").setName("my-name"));
    // create a new custom record
    Record record1 = new Record().setType("eventbus-service-proxy").setLocation(new JsonObject().put("endpoint", "the-service-address")).setName("my-service").setMetadata(new JsonObject().put("some-label", "some-value"));
    // publish "my-service" service
    discovery.publish(record1, ar -> {
        if (ar.succeeded()) {
            System.out.println("\"" + record1.getName() + "\" successfully published!");
            Record publishedRecord = ar.result();
        } else {
        // publication failed
        }
    });
    // create a record from type
    Record record2 = HttpEndpoint.createRecord("some-rest-api", "localhost", 8080, "/api");
    // publish the service
    discovery.publish(record2, ar -> {
        if (ar.succeeded()) {
            System.out.println("\"" + record2.getName() + "\" successfully published!");
            Record publishedRecord = ar.result();
        } else {
        // publication failed
        }
    });
    // unpublish "my-service"
    discovery.unpublish(record1.getRegistration(), ar -> {
        if (ar.succeeded()) {
            System.out.println("\"" + record1.getName() + "\" successfully unpublished");
        } else {
        // cannot un-publish the service, may have already been removed, or the record is not published
        }
    });
    // consuming a service
    discovery.getRecord(r -> r.getName().equals(record2.getName()), ar -> {
        if (ar.succeeded()) {
            if (ar.result() != null) {
                // Retrieve the service reference
                ServiceReference reference = discovery.getReference(ar.result());
                // Retrieve the service object
                HttpClient client = reference.get();
                System.out.println("Consuming \"" + record2.getName() + "\"");
                client.getNow("/api", response -> {
                    // release the service
                    reference.release();
                });
            }
        }
    });
    discovery.close();
}
Also used : HttpClient(io.vertx.core.http.HttpClient) ServiceDiscoveryOptions(io.vertx.servicediscovery.ServiceDiscoveryOptions) JsonObject(io.vertx.core.json.JsonObject) Record(io.vertx.servicediscovery.Record) ServiceDiscovery(io.vertx.servicediscovery.ServiceDiscovery) ServiceReference(io.vertx.servicediscovery.ServiceReference)

Example 2 with ServiceDiscovery

use of io.vertx.servicediscovery.ServiceDiscovery in project vertx-zero by silentbalanceyh.

the class ZeroApiWorker method start.

@Override
public void start() {
    final ServiceDiscovery discovery = ServiceDiscovery.create(this.vertx);
    // AtomicBoolean checking
    if (!initialized.getAndSet(true)) {
        // initialized once.
        this.initializeServices(discovery);
    }
    // TODO: Discovery
    /**
     *         this.vertx.setPeriodic(5000, id -> {
     *         // Clean ko services ( Ipc & Api )
     *         Fn.safeJvm(() -> EtcdEraser.create().start(), LOGGER);
     *         });
     */
    // Scan the services every 3s
    this.vertx.setPeriodic(3000, id -> {
        // Read the latest services
        final ConcurrentMap<String, Record> services = ORIGIN.getRegistryData();
        // Read the down services
        final ConcurrentMap<Flag, Set<String>> resultMap = this.calculateServices(services);
        // Do the modification with thread.
        Fn.safeJvm(() -> {
            final CountDownLatch counter = new CountDownLatch(3);
            final Set<String> deleted = resultMap.get(Flag.DELETE);
            final Set<String> updated = resultMap.get(Flag.UPDATE);
            final Set<String> added = resultMap.get(Flag.NEW);
            Runner.run(() -> this.discoveryDeleted(counter, discovery, deleted), "discovery-deleted");
            Runner.run(() -> this.discoveryUpdate(counter, discovery, updated), "discovery-updated");
            Runner.run(() -> this.discoveryAdded(counter, discovery, added, services), "discovery-added");
            // Wait for result
            counter.await();
            LOGGER.info(Info.REG_REFRESHED, added.size(), updated.size(), deleted.size());
        }, LOGGER);
    });
}
Also used : Set(java.util.Set) ConcurrentSet(io.netty.util.internal.ConcurrentSet) HashSet(java.util.HashSet) Record(io.vertx.servicediscovery.Record) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceDiscovery(io.vertx.servicediscovery.ServiceDiscovery)

Aggregations

Record (io.vertx.servicediscovery.Record)2 ServiceDiscovery (io.vertx.servicediscovery.ServiceDiscovery)2 ConcurrentSet (io.netty.util.internal.ConcurrentSet)1 HttpClient (io.vertx.core.http.HttpClient)1 JsonObject (io.vertx.core.json.JsonObject)1 ServiceDiscoveryOptions (io.vertx.servicediscovery.ServiceDiscoveryOptions)1 ServiceReference (io.vertx.servicediscovery.ServiceReference)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1