vert.x筆記:6.vert.x集群化部署


原文及更多文章請見個人博客:http://heartlifes.com

vert.x支持集群化部署,默認封裝使用的是一個叫Hazelcast的框架,從官方github上看到的開發進度表示,3.1可能會引入比較大眾點的zookeeper作為集群的協作框架。

demo工程還是使用第5章中的dubbo服務demo代碼

修改啟動類:

package com.heartlifes.vertx.demo.dubbo;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hazelcast.config.Config;
import com.hazelcast.config.GroupConfig;

public class SpringMain {

	private static Vertx vertx = Vertx.vertx();// 集群初始化失敗的情況下,可以使用默認vertx實例
	private static ApplicationContext ctx = null;

	public static void main(String[] args) {
		// 配置文件方式
		ctx = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
		// Hazelcast配置類
		Config cfg = new Config();
		// 加入組的配置,防止廣播環境下,負載串到別的開發機中
		GroupConfig group = new GroupConfig();
		group.setName("p-dev");
		group.setPassword("p-dev");
		cfg.setGroupConfig(group);
		// 申明集群管理器
		ClusterManager mgr = new HazelcastClusterManager(cfg);
		VertxOptions options = new VertxOptions().setClusterManager(mgr);
		// 集群化vertx
		Vertx.clusteredVertx(options, SpringMain::resultHandler);

	}

	private static void resultHandler(AsyncResult<Vertx> res) {
		// 如果成功,使用集群化的vertx實例
		if (res.succeeded()) {
			vertx = res.result();
			// 這里要注意,一定要在異步回調中,獲取了vertx實例后,再去部署模塊
			// 由於vert.x所有內部邏輯都是異步調用的,所以,如果你在異步回調前就去部署模塊,最終會導致集群失敗
			deploy(vertx);
		} else {
			System.out.println("cluster failed, using default vertx");
			deploy(vertx);
		}
	}

	private static void deploy(Vertx vertx) {
		vertx.deployVerticle(new SpringVerticle(ctx));
		vertx.deployVerticle(new ServerVerticle());
	}

}

啟動多個主程序,會發現后台輸出類似如下的日志信息

八月 04, 2015 2:05:09 下午 com.hazelcast.instance.DefaultAddressPicker
信息: [LOCAL] [p-dev] [3.5] Prefer IPv4 stack is true.
八月 04, 2015 2:05:09 下午 com.hazelcast.instance.DefaultAddressPicker
信息: [LOCAL] [p-dev] [3.5] Picked Address[192.168.1.119]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
八月 04, 2015 2:05:09 下午 com.hazelcast.spi.OperationService
信息: [192.168.1.119]:5701 [p-dev] [3.5] Backpressure is disabled
八月 04, 2015 2:05:09 下午 com.hazelcast.spi.impl.operationexecutor.classic.ClassicOperationExecutor
信息: [192.168.1.119]:5701 [p-dev] [3.5] Starting with 2 generic operation threads and 4 partition operation threads.
八月 04, 2015 2:05:10 下午 com.hazelcast.system
信息: [192.168.1.119]:5701 [p-dev] [3.5] Hazelcast 3.5 (20150617 - 4270dc6) starting at Address[192.168.1.119]:5701
八月 04, 2015 2:05:10 下午 com.hazelcast.system
信息: [192.168.1.119]:5701 [p-dev] [3.5] Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
八月 04, 2015 2:05:10 下午 com.hazelcast.instance.Node
信息: [192.168.1.119]:5701 [p-dev] [3.5] Creating MulticastJoiner
八月 04, 2015 2:05:10 下午 com.hazelcast.core.LifecycleService
信息: [192.168.1.119]:5701 [p-dev] [3.5] Address[192.168.1.119]:5701 is STARTING
八月 04, 2015 2:05:14 下午 com.hazelcast.cluster.impl.MulticastJoiner
信息: [192.168.1.119]:5701 [p-dev] [3.5] 


Members [1] {
	Member [192.168.1.119]:5701 this
}

八月 04, 2015 2:05:14 下午 com.hazelcast.core.LifecycleService
信息: [192.168.1.119]:5701 [p-dev] [3.5] Address[192.168.1.119]:5701 is STARTED
八月 04, 2015 2:05:15 下午 com.hazelcast.partition.InternalPartitionService
信息: [192.168.1.119]:5701 [p-dev] [3.5] Initializing cluster partition table first arrangement...
八月 04, 2015 2:05:22 下午 com.hazelcast.nio.tcp.SocketAcceptor
信息: [192.168.1.119]:5701 [p-dev] [3.5] Accepting socket connection from /192.168.1.119:59906
八月 04, 2015 2:05:22 下午 com.hazelcast.nio.tcp.TcpIpConnectionManager
信息: [192.168.1.119]:5701 [p-dev] [3.5] Established socket connection between /192.168.1.119:5701
八月 04, 2015 2:05:28 下午 com.hazelcast.cluster.ClusterService
信息: [192.168.1.119]:5701 [p-dev] [3.5] 

Members [2] {
	Member [192.168.1.119]:5701 this
	Member [192.168.1.119]:5702
}

八月 04, 2015 2:05:29 下午 com.hazelcast.partition.InternalPartitionService
信息: [192.168.1.119]:5701 [p-dev] [3.5] Re-partitioning cluster data... Migration queue size: 135
八月 04, 2015 2:05:30 下午 com.hazelcast.partition.InternalPartitionService
信息: [192.168.1.119]:5701 [p-dev] [3.5] All migration tasks have been completed, queues are empty.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM