Search in sources :

Example 1 with Record

use of io.vertx.servicediscovery.Record 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 Record

use of io.vertx.servicediscovery.Record in project georocket by georocket.

the class ServiceTest method publishOnce.

/**
 * Test if a service is really published only once
 * @param context the test context
 */
@Test
public void publishOnce(TestContext context) {
    Vertx vertx = new Vertx(rule.vertx());
    Async async = context.async();
    ServiceDiscovery discovery = ServiceDiscovery.create(vertx);
    Service.publishOnce("A", "A", discovery, vertx).flatMap(v -> Service.publishOnce("A", "A", discovery, vertx)).flatMap(v -> Service.publishOnce("A", "B", discovery, vertx)).flatMap(v -> {
        return discovery.rxGetRecords(record -> true);
    }).doAfterTerminate(discovery::close).subscribe(recordList -> {
        List<Record> lA = recordList.stream().filter(r -> r.getName().equals("A")).collect(Collectors.toList());
        context.assertEquals(2, lA.size());
        async.complete();
    }, context::fail);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) List(java.util.List) Rule(org.junit.Rule) RunWith(org.junit.runner.RunWith) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Record(io.vertx.servicediscovery.Record) Collectors(java.util.stream.Collectors) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Vertx(io.vertx.rxjava.core.Vertx) Async(io.vertx.ext.unit.Async) Record(io.vertx.servicediscovery.Record) Vertx(io.vertx.rxjava.core.Vertx) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) Test(org.junit.Test)

Example 3 with Record

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

the class CommonArithmetic method search.

@Override
public Record search(final List<Record> records, final RoutingContext context) {
    final HttpServerRequest request = context.request();
    // Input source
    final String uri = request.uri();
    final Optional<Record> hitted = records.stream().filter(record -> this.isMatch(uri, record)).findAny();
    // Find valid;
    return hitted.orElse(null);
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest) List(java.util.List) Strings(io.vertx.zero.eon.Strings) Origin(io.vertx.up.micro.discovery.Origin) Ut(io.vertx.up.tool.Ut) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) RoutingContext(io.vertx.ext.web.RoutingContext) Record(io.vertx.servicediscovery.Record) Pattern(java.util.regex.Pattern) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Record(io.vertx.servicediscovery.Record)

Example 4 with Record

use of io.vertx.servicediscovery.Record 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)

Example 5 with Record

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

the class RpcClientImpl method connect.

@Override
public RpcClient connect(final JsonObject config, final JsonObject data, final Handler<AsyncResult<JsonObject>> handler) {
    final Record record = RpcHelper.getRecord(config);
    // Service Configuration
    final String name = config.getString(Key.NAME);
    final String address = config.getString(Key.ADDR);
    final JsonObject normalized = RpcHelper.normalize(name, config, record);
    this.holder = this.lookupHolder(this.vertx, name, normalized);
    // Get Channel
    final IpcType type = Ut.toEnum(IpcType.class, config.getString(Key.TYPE));
    final RpcStub stub = this.getStub(type);
    // Future result return to client.
    final IpcData request = new IpcData();
    request.setType(type);
    request.setAddress(address);
    // The same operation for request.
    DataEncap.in(request, record);
    DataEncap.in(request, Envelop.success(data));
    LOGGER.info(Info.CLIENT_TRAFFIC, request.toString());
    final Future<JsonObject> future = stub.traffic(request);
    future.setHandler(res -> handler.handle(Future.succeededFuture(res.result())));
    return this;
}
Also used : IpcType(io.vertx.up.eon.em.IpcType) JsonObject(io.vertx.core.json.JsonObject) Record(io.vertx.servicediscovery.Record) RpcStub(io.vertx.up.plugin.rpc.client.RpcStub) IpcData(io.vertx.up.atom.flux.IpcData)

Aggregations

Record (io.vertx.servicediscovery.Record)13 JsonObject (io.vertx.core.json.JsonObject)5 ServiceDiscovery (io.vertx.servicediscovery.ServiceDiscovery)3 IpcType (io.vertx.up.eon.em.IpcType)3 Origin (io.vertx.up.micro.discovery.Origin)3 Ut (io.vertx.up.tool.Ut)3 Strings (io.vertx.zero.eon.Strings)3 Observable (io.reactivex.Observable)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 ServiceReference (io.vertx.servicediscovery.ServiceReference)2 HttpEndpoint (io.vertx.servicediscovery.types.HttpEndpoint)2 IpcData (io.vertx.up.atom.flux.IpcData)2 Fn (io.vertx.up.func.Fn)2 Annal (io.vertx.up.log.Annal)2 Instance (io.vertx.up.tool.mirror.Instance)2 List (java.util.List)2 Objects (java.util.Objects)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ConcurrentSet (io.netty.util.internal.ConcurrentSet)1 CircuitBreaker (io.vertx.circuitbreaker.CircuitBreaker)1