1 Star 0 Fork 2

周周/vertx-examples

forked from githubsync/vertx-examples 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
README.adoc 3.99 KB
一键复制 编辑 原始数据 按行查看 历史
Julien Viet 提交于 2022-05-24 10:38 . 4.3.1 upgrade

Vert.x Service Proxy examples

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.

Providing a service

To provide a service, you need a service interface, an implementation and a verticle.

Service interface

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

Service Provider

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.

Running the service provider

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.

Client generation

Vert.x generates the clients to use this service in the different supported languages.

Consuming a service

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.

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhouzq2046/vertx-examples.git
git@gitee.com:zhouzq2046/vertx-examples.git
zhouzq2046
vertx-examples
vertx-examples
4.x

搜索帮助