当前位置:网站首页>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
边栏推荐
- 【已解决】Unknown error 1146
- JS monitors empty objects and empty references
- The underlying mechanism of advertising on websites
- The embodiment of generics in inheritance and wildcards
- 20220319
- Qtip2 solves the problem of too many texts
- 论文学习——鄱阳湖星子站水位时间序列相似度研究
- docket
- Use of generics
- Common architectures of IO streams
猜你喜欢
Deep learning parameter initialization (I) Xavier initialization with code
Leetcode 198: 打家劫舍
Summary of Arduino serial functions related to print read
【CoppeliaSim4.3】C#调用 remoteApi控制场景中UR5
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
3311. Longest arithmetic
PAT甲级真题1166
File operation serialization recursive copy
为什么说数据服务化是下一代数据中台的方向?
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
随机推荐
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
C code production YUV420 planar format file
Advanced API (UDP connection & map set & collection set)
VMware network mode - bridge, host only, NAT network
Raspberry pie update tool chain
IPv4 address
Basic components and intermediate components
【无标题】
Advanced API (serialization & deserialization)
LeetCode
Distributed lock
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
Common operations of JSP
II. D3.js draw a simple figure -- circle
TreeMap
Visit Google homepage to display this page, which cannot be displayed
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
4279. Cartesian tree
Advanced API (local simulation download file)