Hazelcast介绍

Hazelcast是一个Java的开源分布式内存实现,它具有以下特性:

01    Distributed implementations of java.util.{Queue, Set, List, Map}
02    Distributed implementation of java.util.concurrent.ExecutorService
03    Distributed implementation of java.util.concurrency.locks.Lock
04    Distributed Topic for publish/subscribe messaging
05    Transaction support and J2EE container integration via JCA
06    Distributed listeners and events
07    Support for cluster info and membership events
08    Dynamic HTTP session clustering
09    Dynamic clustering
10    Dynamic scaling to hundreds of servers
11    Dynamic partitioning with backups
12    Dynamic fail-over
13    Super simple to use; include a single jar
14    Super fast; thousands of operations per sec.
15    Super small; less than a MB
16    Super efficient; very nice to CPU and RAM

安装也非常方便:

1    Download hazelcast-version.zip from www.hazelcast.com
2    Unzip hazelcast-version.zip file
3    Add hazelcast.jar file into your classpath

要使用分布式的Map,只需要以下代码即可实现:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
import  com.hazelcast.core.Hazelcast;
import  java.util.Map;
import  java.util.Collection;
 
Map<String, Customer> mapCustomers = Hazelcast.getMap( "customers" );
mapCustomers.put( "1" , new  Customer( "Joe" , "Smith" ));
mapCustomers.put( "2" , new  Customer( "Ali" , "Selam" ));
mapCustomers.put( "3" , new  Customer( "Avi" , "Noyan" ));
 
Collection<Customer> colCustomers = mapCustomers.values();
for  (Customer customer : colCustomers) {
     // process customer
}

 

Hazelcast的官网上面有一个非常直观的视频:http://www.hazelcast.com/screencast.jsp,建议有兴趣的朋友花10分钟时间看看。

还有一份PDF可以参考:http://roma.javaday.it/javaday2010/sites/default/files/ClusteringHazelcast-javaday.pdf。

Hazelcast作为一款与ZooKeeper类似的开源实现,我在网上找了一篇相关的文章:http://blog.armstrongconsulting.com/?p=132 在这篇文章中有一段这样写道:

I had occasional hangs with Hazelcast 1.8.4 which caused me to switch to Zookeeper. As expected, Zookeeper was a lot harder to use than Hazelcast – you need Zookeeper installed on 3 servers. There’s no official java client, just some recipes and I found an implementation of Zookeeper locks called Cages on google code. For a java developer, Hazelcast is obviously way easier to use. 

另外,在Hazelcast的官方文档中,提到了Hazelcast的集群机制:

If there is no existing node, then the node will be the first member of the cluster. If multicast is enabled then it will start a multicast listener so that it can respond to incoming join requests. Otherwise it will listen for join request coming via TCP/IP.

If there is an existing cluster already, then the oldest member in the cluster will receive the join request and check if the request is for the right group. If so, the oldest member in the cluster will start the join process.

In the join process, the oldest member will:

  • send the new member list to all members

  • tell members to sync data in order to balance the data load

Every member in the cluster has the same member list in the same order. First member is the oldest member so if the oldest member dies, second member in the list becomes the first member in the list and the new oldest member.

从这点可以看出,虽然Hazelcast没有所谓的“Master”,但是仍然有一个Leader节点(the oldest member),这个概念与ZooKeeper中的Leader类似,但是实现原理却完全不同。同时,Hazelcast中的数据是分布式的,每一个member持有部分数据和相应的backup数据,这点也与ZooKeeper不同。

虽然Hazelcast应用便捷,但是要将其实际应用于生产环境,还是具有一定的风险的,这个需要大量的实际应用来验证。

 

Hazelcast是面向Java的缓存、集群及数据分发解决方案。最近,它的2.0版本发布了。作为新版本的一部分,该产品提供了商用企业版和免费的开源社区版。

其中,社区版在Apache许可2.0下发布,并托管于Google Code中。2.0版本包含了一个分布式备份功能,用以确保每个结点都能均匀地被所有其他结点备份。Hazelcast创始人Talip Ozturk告诉InfoQ说,“我相信我们的备份分发是一个全新的解决方案”。

采用分布式数据备份,结点在丢失时仅会对集群造成很小的影响。这点在内存中有大数据时尤为重要。

Hazelcast解决方案的工作原理是将差不多大小的数据分布到集群中的每个结点上。例如,在一个50个结点的结点集群中,每个结点存储20GB的基本数据以及20GB的备份数据。结点1数据会分成1/49大小的若干份,并由剩余49个结点中的每一个进行备份。如果结点1下线,那么任何迁移都不用就可以让集群保持均衡。随着新节点加入到系统中,Hazelcast会慢慢地将数据迁移到新结点来让所有结点上的数据保持均衡。

新版2.0中的其他特性包括:

  • 并行IO,它将对内(In)和对外(Out)通信联合到单个线程中(在1.0版本中,每个成员会拥有对内和对外线程各一个,用作处理与其他使用NIO信道的成员进行通信)。
  • 改善连接管理,Hazelcast在破损连接被宣布死亡之前会尝试进行修复。
  • 为Queue、List、Set和Topic提供新的事件容器。

另外,企业版增加了堆外(off-heap)存储(注:Hazelcast中将其称作弹性内存(Elastic Memory))、附加的安全能力和一个原生的C#客户端。

出于安全考虑,该产品包含了一份基于JAAS的实现,用作验证集群成员及客户端,并对客户端操作进行访问控制检查。访问控制可根据终端点委托或代码进行管理,而安全性可以通过使用XML或API启用和配置。

弹性内存本质上是一种解决过长GC中断时间(pause time)的变通方案。Azul推出的C4收集器是一个例外,它消除了所有的GC中断。借助它,商业JVM中的垃圾收集中断时间会随内置堆的大小明显增长。弹性内存可以减少JVM堆的大小,从而降低垃圾收集的中断时间。关于这点,Ozturk给出了一个大致的建议:

如果你的每个JVM都拥有10GB以上的数据,或者有超过1KB的值对,那么可以使用弹性内存。而如果你的每个JVM数据在4GB左右,或者少于a KB,那么我们不建议使用弹性内存。

Hazelcast的弹性内存采用直接字节缓存区(direct byte buffer)实现,其中每个缓存区分为若干块,每块默认大小为1KB。这个特性类似于Oracle的Coherence、Terracotta的Ehcache以及一些其他提供商的缓冲方案。

企业版许可模型基于每个订阅和结点,相关价格信息可通过[email protected]查询。另外,Hazelcast还为社区版提供了两个不同级别的支持,参考价格公布在了他们的网站上。

猜你喜欢

转载自kavy.iteye.com/blog/1962186
今日推荐