forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClassNode2.java
More file actions
68 lines (64 loc) Β· 3.12 KB
/
MainClassNode2.java
File metadata and controls
68 lines (64 loc) Β· 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager;
/*
Represents Vert.x instance running on a node (node2)
This class deploys consumer verticle on node2 (machine2)
*/
public class MainClassNode2 {
public static void main(String[] args) {
JsonObject zkConfig = configureClusterManager();
ClusterManager zookeeperClusterManager = new ZookeeperClusterManager(zkConfig);
VertxOptions options = configureVertx(zookeeperClusterManager);
Vertx.clusteredVertx(options, res -> {
if (res.succeeded()) {
Vertx vertx = res.result();
DeploymentOptions deploymentOptions = new DeploymentOptions().setInstances(2);
vertx.deployVerticle("verticle.ConsumerVerticle", deploymentOptions);
System.out.println("consumer verticle deployed");
}
});
}
/**
* create configuration object to be used to configure the Zookeeper cluster manager
* @return JsonObject representing Zookeeper configuration
*/
private static JsonObject configureClusterManager() {
/*
Set zookeeperHosts property to the IPs of machines running the cluster manager, here we set it to localhost
(127.0.0.1), but in case we have multiple machines/docker containers we have to set it on every node to the IPs
of the machines running the cluster manager. For example:
zkConfig.put("zookeeperHosts", "192.168.1.12"); // Zookeeper is running on this machines
zkConfig.put("zookeeperHosts", "192.168.1.12,192.168.1.56");
*/
JsonObject zkConfig = new JsonObject();
zkConfig.put("zookeeperHosts", "localhost");
zkConfig.put("rootPath", "io.vertx");
zkConfig.put("retry", new JsonObject()
.put("initialSleepTime", 3000)
.put("maxTimes", 3));
return zkConfig;
}
/**
* Specifies Vert.x instance configuration, this is essential for clustering on multiple separate machines and
* Docker containers in order to make Event Bus send/consume messages appropriately
* @param clusterManager represents the cluster manager
* @return VertxOptions object to be used in deployment
*/
private static VertxOptions configureVertx(ClusterManager clusterManager) {
/*
the default value of the cluster host (localhost) is used here, if we want to have multiple machines/docker
containers in our cluster we must configure the cluster host properly on each node in order for the event bus
to send/consume messages properly between our verticles, to do so we use the method setClusterHost and give it
this node's IP. For example:
options.setClusterHost(192.168.1.12);
*/
VertxOptions options = new VertxOptions()
.setClustered(true)
.setClusterManager(clusterManager);
return options;
}
}