当前位置:网站首页>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
边栏推荐
- 20220319
- 2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
- Unified handling and interception of exception exceptions of vertx
- Topic | synchronous asynchronous
- 7.2刷题两个
- PgSQL converts string to double type (to_number())
- Le Seigneur des anneaux: l'anneau du pouvoir
- Summary of abnormal mechanism of interview
- 高并发内存池
- Recursion, Fibonacci sequence
猜你喜欢

File operation serialization recursive copy
![[solved] unknown error 1146](/img/f1/b8dd3ca8359ac9eb19e1911bd3790a.png)
[solved] unknown error 1146
![PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef](/img/65/1f28071fc15e76abb37f1b128e1d90.jpg)
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef

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

1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log

Selenium key knowledge explanation

JUC forkjoinpool branch merge framework - work theft

Common architectures of IO streams

Common problems in io streams

7.2 brush two questions
随机推荐
Longest common prefix and
Common problems in io streams
Deep learning parameter initialization (I) Xavier initialization with code
MySQL syntax (basic)
Advanced API (serialization & deserialization)
IO stream system and FileReader, filewriter
4279. Cartesian tree
PgSQL converts string to double type (to_number())
Selenium key knowledge explanation
Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
Common methods of file class
Visit Google homepage to display this page, which cannot be displayed
Store WordPress media content on 4everland to complete decentralized storage
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
3311. Longest arithmetic
docket
深度学习参数初始化(一)Xavier初始化 含代码
2021-07-18
Advanced API (multithreading 02)
II. D3.js draw a simple figure -- circle