Search in sources :

Example 1 with ServiceReference

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

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

the class ServiceJet method handle.

public Handler<RoutingContext> handle() {
    return context -> {
        // Run with circuit breaker
        this.breaker.execute(future -> getEndPoints().setHandler(res -> {
            if (res.succeeded()) {
                final List<Record> records = res.result();
                // Find the record hitted. ( Include Path variable such as /xx/yy/:zz/:xy )
                final Record hitted = this.arithmetic.search(records, context);
                // Complete actions.
                if (null == hitted) {
                    // Service Not Found ( 404 )
                    reply404Error(context);
                } else {
                    // Find record, dispatch request
                    final ServiceReference reference = this.discovery.getReference(hitted);
                    doRequest(context, reference);
                }
                future.complete();
            } else {
                // Future failed
                future.fail(res.cause());
            }
        }));
    };
}
Also used : CircuitBreakerOptions(io.vertx.circuitbreaker.CircuitBreakerOptions) io.vertx.up.exception._404ServiceNotFoundException(io.vertx.up.exception._404ServiceNotFoundException) Arithmetic(io.vertx.up.micro.matcher.Arithmetic) Strings(io.vertx.zero.eon.Strings) CircuitBreaker(io.vertx.circuitbreaker.CircuitBreaker) Visitor(io.vertx.zero.marshal.Visitor) RoutingContext(io.vertx.ext.web.RoutingContext) Annal(io.vertx.up.log.Annal) Envelop(io.vertx.up.atom.Envelop) WebException(io.vertx.up.exception.WebException) ServiceDiscovery(io.vertx.servicediscovery.ServiceDiscovery) Fn(io.vertx.up.func.Fn) ServiceReference(io.vertx.servicediscovery.ServiceReference) Vertx(io.vertx.core.Vertx) Record(io.vertx.servicediscovery.Record) Future(io.vertx.core.Future) io.vertx.core.http(io.vertx.core.http) Instance(io.vertx.up.tool.mirror.Instance) HttpEndpoint(io.vertx.servicediscovery.types.HttpEndpoint) CommonArithmetic(io.vertx.up.micro.matcher.CommonArithmetic) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) CircuitVisitor(io.vertx.zero.micro.config.CircuitVisitor) Answer(io.vertx.up.rs.hunt.Answer) io.vertx.up.exception._405MethodForbiddenException(io.vertx.up.exception._405MethodForbiddenException) Handler(io.vertx.core.Handler) Record(io.vertx.servicediscovery.Record) ServiceReference(io.vertx.servicediscovery.ServiceReference)

Aggregations

Record (io.vertx.servicediscovery.Record)2 ServiceDiscovery (io.vertx.servicediscovery.ServiceDiscovery)2 ServiceReference (io.vertx.servicediscovery.ServiceReference)2 CircuitBreaker (io.vertx.circuitbreaker.CircuitBreaker)1 CircuitBreakerOptions (io.vertx.circuitbreaker.CircuitBreakerOptions)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 io.vertx.core.http (io.vertx.core.http)1 HttpClient (io.vertx.core.http.HttpClient)1 JsonObject (io.vertx.core.json.JsonObject)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 ServiceDiscoveryOptions (io.vertx.servicediscovery.ServiceDiscoveryOptions)1 HttpEndpoint (io.vertx.servicediscovery.types.HttpEndpoint)1 Envelop (io.vertx.up.atom.Envelop)1 WebException (io.vertx.up.exception.WebException)1 io.vertx.up.exception._404ServiceNotFoundException (io.vertx.up.exception._404ServiceNotFoundException)1 io.vertx.up.exception._405MethodForbiddenException (io.vertx.up.exception._405MethodForbiddenException)1 Fn (io.vertx.up.func.Fn)1