当前位置:网站首页>Vertx multi vertical shared data
Vertx multi vertical shared data
2022-07-03 07:25:00 【Sleeping Empire】
Introduce
Vert.x many Verticle Sharing data between
- 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 Project dependence
<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 File configuration
server:
host: 127.0.0.1
port: 18004
3. Project main class
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 Set up shared data
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 Get shared data
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. result
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. Full address of the project
边栏推荐
- Industrial resilience
- 不出网上线CS的各种姿势
- Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
- Advanced API (local simulation download file)
- When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
- Common APIs
- SharePoint modification usage analysis report is more than 30 days
- Custom generic structure
- Discussion on some problems of array
- Circuit, packet and message exchange
猜你喜欢

Introduction of buffer flow

Take you through the whole process and comprehensively understand the software accidents that belong to testing

Summary of abnormal mechanism of interview

Hash table, generic

The embodiment of generics in inheritance and wildcards

Use of generics

Various postures of CS without online line

Leetcode 198: 打家劫舍

《指环王:力量之戒》新剧照 力量之戒铸造者亮相

Summary of Arduino serial functions related to print read
随机推荐
Distributed ID
带你全流程,全方位的了解属于测试的软件事故
File operation serialization recursive copy
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
C代码生产YUV420 planar格式文件
C code production YUV420 planar format file
Book recommendation~
IP home online query platform
Thoughts on project development
[set theory] order relation (partial order relation | partial order set | example of partial order set)
Download address collection of various versions of devaexpress
SQL create temporary table
Interfaces and related concepts
JS monitors empty objects and empty references
Hash table, generic
TreeMap
Leetcode 198: 打家劫舍
【已解决】win10找不到本地组策略编辑器解决方法
FileInputStream and fileoutputstream
Advanced API (local simulation download file)