介绍
Vert.x多Verticle之间共享数据
- different parts of your application, or
- different applications in the same Vert.x instance, or
- different applications across a cluster of Vert.x instances.
1. maven项目依赖
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.lance.common</groupId>
<artifactId>vertx-common-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2.YAML文件配置
server:
host: 127.0.0.1
port: 18004
3.项目主类
public class MultiVerticalApplication extends AbstractVerticle {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MultiVerticalApplication.class.getName());
}
@Override
public void start() {
ConfigRetriever retriever = readYaml(vertx);
retriever.getConfig(json -> {
JsonObject object = json.result();
DeploymentOptions options = new DeploymentOptions().setConfig(object);
Router router = Router.router(vertx);
vertx.deployVerticle(new HelloVertical(router), options);
vertx.deployVerticle(new WorldVertical(router), options);
});
}
private static ConfigRetriever readYaml(Vertx vertx) {
ConfigStoreOptions store = new ConfigStoreOptions()
.setType("file")
.setFormat("yaml")
.setOptional(true)
.setConfig(new JsonObject().put("path", "application.yaml"));
return ConfigRetriever.create(vertx, new ConfigRetrieverOptions().addStore(store));
}
}
4.Verticle One设置共享数据
public class WorldVertical extends AbstractVerticle {
public final static String DEFAULT_LOCAL_MAP_NAME = "default_local_map_name";
private final Router router;
@Override
public void start() {
router.route("/world").handler(ctx -> {
String name = ctx.queryParam("name").get(0);
SharedData sd = ctx.vertx().sharedData();
LocalMap<String, String> sharedData = sd.getLocalMap(DEFAULT_LOCAL_MAP_NAME);
sharedData.put("shareKey", "this is share world");
log.info("===> Request world name: {}, share put data: {}", name, sharedData);
ctx.json(R.success("World " + name));
});
ConfigProperties properties = config().mapTo(ConfigProperties.class);
int port = properties.getServer().getPort();
vertx.createHttpServer()
.requestHandler(router)
.listen(port, http -> {
if (http.succeeded()) {
log.info("World server started on port {}", http.result().actualPort());
} else {
log.error("World server start fail: ", http.cause());
}
});
}
}
5.Verticle Two获取共享数据
public class HelloVertical extends AbstractVerticle {
private final Router router;
@Override
public void start() {
router.route("/hello").handler(ctx -> {
String name = ctx.queryParam("name").get(0);
SharedData sd = ctx.vertx().sharedData();
log.info("===> Request hello name: {}, share get data: {}", name, sd.getLocalMap(WorldVertical.DEFAULT_LOCAL_MAP_NAME));
ctx.json(R.success("Hello " + name));
});
ConfigProperties properties = config().mapTo(ConfigProperties.class);
int port = properties.getServer().getPort();
vertx.createHttpServer()
.requestHandler(router)
.listen(port, http -> {
if (http.succeeded()) {
log.info("Hello server started on port {}", http.result().actualPort());
} else {
log.error("Hello server start fail: ", http.cause());
}
});
}
}
6. 结果
2022-02-06 23:26:43.855 INFO 24 --- [ntloop-thread-2] org.lance.multi.WorldVertical ---[ 33] : ===> Request world name: tom, share put data: {shareKey=this is share world}
2022-02-06 23:26:52.681 INFO 24 --- [ntloop-thread-2] org.lance.multi.HelloVertical ---[ 28] : ===> Request hello name: tom, share get data: {shareKey=this is share world}
7.项目完整地址
Vertx之Multi Verticle共享数据 Github 地址
Vertx之Multi Verticle共享数据 Gitee 地址
|