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();
}
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);
}
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);
}
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);
});
}
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;
}
Aggregations