当前位置:网站首页>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
边栏推荐
- 带你全流程,全方位的了解属于测试的软件事故
- Advanced API (batch image Download & socket dialog)
- 【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
- Advanced API (character stream & net for beginners)
- Distributed ID
- II. D3.js draw a simple figure -- circle
- TreeMap
- Use of file class
- OSI knowledge sorting
- Circuit, packet and message exchange
猜你喜欢
Deep learning parameter initialization (I) Xavier initialization with code
IP home online query platform
C代码生产YUV420 planar格式文件
TCP cumulative acknowledgement and window value update
Common methods of file class
高并发内存池
JUC forkjoinpool branch merge framework - work theft
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
论文学习——鄱阳湖星子站水位时间序列相似度研究
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
随机推荐
HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq
La différence entre le let Typescript et le Var
Unified handling and interception of exception exceptions of vertx
Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
docker建立mysql:5.7版本指定路径挂载不上。
[solved] unknown error 1146
Map interface and method
【CoppeliaSim4.3】C#调用 remoteApi控制场景中UR5
Final, override, polymorphism, abstraction, interface
4279. Cartesian tree
Common architectures of IO streams
Distributed transactions
Basic components and intermediate components
VMware network mode - bridge, host only, NAT network
【最详细】最新最全Redis面试大全(50道)
Win 2008 R2 crashed at the final installation stage
C代码生产YUV420 planar格式文件
Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
The embodiment of generics in inheritance and wildcards