FroMage / redpipe

Redpipe Web Framework
Apache License 2.0
70 stars 10 forks source link

automatic registration in the vertx ServiceDiscovery #9

Open fiorenzino opened 6 years ago

fiorenzino commented 6 years ago

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?

FroMage commented 6 years ago

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.

fiorenzino commented 6 years ago

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

FroMage commented 6 years ago

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

fiorenzino commented 6 years ago

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:

@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 serviceClient; and in some method:

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

what do you think about it?

FroMage commented 6 years ago

Yes this sounds great.

fiorenzino commented 6 years ago

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