Start a Vert.x cluster in code

This article aims to develop a demo to start Vert.x cluster by code. Start the Vert.x cluster by coding to start the debugger in eclipse.

code show as below.

package com.wof.realtime.apigateway;

import com.hazelcast.config.Config;
import com.hazelcast.config.JoinConfig;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager;

/**
 * start the clustered MainVerticle in code,
 *
 * @author lhx
 *
 */
public class RunApiGateway {

	public static void main(String[] args) {
		// Hazelcast configuration class
		Config cfg = new Config();

		// Disable UDP multicast and use TCP for cluster communication.
		JoinConfig joinConfig = cfg.getNetworkConfig().getJoin();
		joinConfig.getMulticastConfig().setEnabled(false);
		joinConfig.getTcpIpConfig().setEnabled(true);
		joinConfig.getTcpIpConfig().addMember("192.168.1.100,192.168.1.132,192.168.1.130");// If there are multiple target nodes, you need to write as many addresses.

		// Specify the network card used for communication (if you don't specify multiple network cards on the machine, there will be problems, no matter whether there are multiple network cards or not, it is best to set it up.)
		cfg.getNetworkConfig().getInterfaces().setEnabled(true);
		cfg.getNetworkConfig().getInterfaces().addInterface("192.168.1.*");

		// Declare the cluster manager
		ClusterManager mgr = new HazelcastClusterManager(cfg);
		VertxOptions options = new VertxOptions().setClusterManager(mgr);
		options.setClustered(true);
		options.setClusterHost("192.168.1.100");//This must be set (when the machine has multiple network cards, if it is not set, no message will be received.)

		// clustered vertx	
		Vertx.clusteredVertx(options, res -> {
			if (res.succeeded()) {
				Vertx vertx = res.result();
				vertx.deployVerticle(ApiGatewayVerticle.class.getName());
				System.out.println("Api Gateway : cluster succeeded");
			} else {
				res.cause().printStackTrace();
			}
		});
	}
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326445002&siteId=291194637