逖靖寒的世界

每天进步一点点

导航

Java分布式内存开源实现: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,只需要以下代码即可实现:

 

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应用便捷,但是要将其实际应用于生产环境,还是具有一定的风险的,这个需要大量的实际应用来验证。

 

更多关于ZooKeeper的文章请参考:http://www.cnblogs.com/gpcuster/tag/ZooKeeper/

posted on 2010-09-26 15:20  逖靖寒  阅读(14707)  评论(8编辑  收藏  举报