同步操作将从 githubsync/vertx-examples 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Here you will find examples demonstrating Vert.x Service Proxies.
Service Proxies provide a way to expose services on the event bus and reducing the code required to invoke them.
In this example, we will run two verticles:
one provides a ProcessorService
the seconds one consumes it
Verticles runs on two clustered vert.x instances.
To provide a service, you need a service interface, an implementation and a verticle.
The service interface is a Java interface annotated with @ProxyGen
.
Compiling this class creates two other classes:
These classes are going to be used in the provider and consumer code
The service provider contains a verticle registering the service handler and an implementation of the service:
The service implementation is a simple Java class implementing the service interface. The only trick to know is the invocation of the given result handler:
resultHandler.handle(Future.succeededFuture(result));
In the verticle, the handler registration is made using:
new ServiceBinder(vertx)
.setAddress("vertx.processor")
.register(ProcessorService.class, service);
service
is the service object we register (so an instance of the ProcessorServiceImpl
. The last parameter is the address on which the service can be invoked.
Alternatively, you can instantiate the generated handler:
new ProcessorServiceVertxProxyHandler(vertx, service).registerHandler("vertx.processor");
In this case, classes need to be generated beforehand, and so, requires a manual mvn compile
.
To run the service provider, you first need to package it with:
cd service-provider
# Install is important here as the service consumer needs the service interface class
mvn clean install
Once done, run the verticle using:
vertx run io.vertx.examples.service.ProcessorServiceVerticle \
-cp target/service-provider-4.3.1.jar -cluster
Depending on your cluster configuration you may have to specify the cluster-host
and configure Hazelcast.
To consume our service, we just need a verticle that runs on a vert.x instance on the same cluster as the service provider.
The service is retrieved using:
ProcessorService service = ProcessorService.createProxy(vertx, "vertx.processor");
The second argument is the service address.
To run this verticle, package it using: mvn clean package
, and run it with:
vertx run io.vertx.examples.service.consumer.ConsumerVerticle \ -cp ../service-provider/target/service-provider-4.3.1.jar:target/service-consumer-4.3.1.jar \ -cluster
Depending on your cluster configuration you may have to specify the cluster-host
and configure Hazelcast.
Notice that the verticle has in its classpath the service provider verticle to access the service interface. We could have packaged the service interface in its owned packaged (alongside the generated handler and proxy).
Once launched you should see:
{"name":"vertx","approved":true}
The service can be consumed from Java.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。