redpipe icon indicating copy to clipboard operation
redpipe copied to clipboard

automatic registration in the vertx ServiceDiscovery

Open fiorenzino opened this issue 6 years ago • 6 comments

Another suggestion:

the automatic registration of rest service on ServiceDiscovery should be interesting.

for example: @ServiceDiscovery("api.name", "host", port, "root") @Path("/") public FlowerService {

}

translated in vertx: Record record = HttpEndpoint.createRecord(name, host, port, root, new JsonObject().put("api.name", name)); serviceDiscovery = ServiceDiscovery.create(vertx, new ServiceDiscoveryOptions().setBackendConfiguration(config())); serviceDiscovery.publish(record, ar -> { if (ar.succeeded()) { registeredRecords.add(record); logger.info("Service <" + ar.result().getName() + "> published"); future.complete(); } else { future.fail(ar.cause()); } });

what do you think about it?

fiorenzino avatar Mar 22 '18 10:03 fiorenzino

That's very interesting, except I think the host/port parts should come from the current config, no? It seems error-prone to specify them in the code when they are deployment-dependent. I guess the same could be said of the root.

But certainly, it would be useful to declare services with an annotation. I think the name should be enough, the rest we can get from the config.

FroMage avatar Mar 22 '18 11:03 FroMage

Yes, the host/port/root parts are always derived from server config and @Path annotations. But in vertx you must declare that.

fiorenzino avatar Mar 22 '18 12:03 fiorenzino

Sure, but they don't belong in the annotation if the framework can detect them. Unless they're not required/defaulted.

FroMage avatar Mar 22 '18 14:03 FroMage

If you like my idea, i can try to write my first extension on redpipe: redpipe-servicediscovery, dependent from redpipe-cdi.

We need to initialize in some point: ServiceDiscovery serviceDiscovery = ServiceDiscovery.create(AppGlobals.get().getVertx(), new ServiceDiscoveryOptions().setBackendConfiguration(AppGlobals.get().getConfig())); AppGlobals.get().setGlobal("serviceDiscovery", serviceDiscovery);

Two annotations: @Service(name="name", host="localhost", port=8081, path="/") for JAXRS resources. Where name, host, port, path are optionals:

  • host=> config().get("http_address")
  • port => config().get("http_port")
  • path=> default value is "/"
  • name=> default value is java className

@ServiceClient(name="name") where name is the service name.

The example use for service: @Path("/") @ApplicationScoped @Service public class UsersService {}

and for the client: @ServiceClient(name="name") Single<WebClient> serviceClient; and in some method:

serviceClient.subscribe((client) -> { if (client == null) { System.out.println("no client"); } else { Single<HttpResponse<Buffer>> req= client.get("/path").rxSend(); req.map(response -> { resultHandler.toHandler().handle(Future.succeededFuture(response.body().toString())); return ""; }).subscribe(); } });

what do you think about it?

fiorenzino avatar Apr 04 '18 14:04 fiorenzino

Yes this sounds great.

FroMage avatar Apr 04 '18 14:04 FroMage

work in progress: https://github.com/fiorenzino/weld-service-discovery

fiorenzino avatar Apr 05 '18 00:04 fiorenzino