05.hazelcast
Hazelcast
  site
    http://www.hazelcast.com/
  installl
    1. download
      http://www.hazelcast.com/downloads.jsp
    2. UnZip
    단지 다운로드 받아서 적당한 위치에 놓고 풀어만 놓으면 설치는 끝이다.
  run
    bin 디렉토리에 가서 run.bat 실행
    여러개를 실행 시키면 port가 5701부터 순차적으로 증가된다.
    클러스터링을 위해 특별히 설정을 건드리거나 할 필요없다. 설치와 실행이 쉬운게 장점
  docs
    http://www.hazelcast.com/documentation.jsp
    http://code.google.com/docreader/#p=hazelcast&s=hazelcast&t=hazelcast
  client
    memcached protocol
      1.9.2.2 문제점
        객체를 넣거나 뺄때 deserializable 또는 압축을 풀수 없다.
        MemCachedClient.java에서 loadItems 메소드를 보면
sock.realine()을 통해 받은 값 중 flag가 "0"으로 고정되서 오작동한다.
이부분을 수정해야만 사용 가능하다.
      1.9.3 문제점
        1.9.2.2 문제를 reporting 했더니 금방 1.9.3을 release 해줬다.
그런데 key 값에 ":" 이 값이 있으면 앞에 부분이 사라진다.
      고려사항
        compressThreshold 값이 최적인 것을 찾아야 한다.
너무 작으면 빈번하게 압축이 발생하고 너무 크면 통신 구간에 비용이 발생
        Memcached protocol 을 이용하는 거 보다 Hazelcast client 이용하는 편이 낫다.
    Spring Integration
      summary Hazelcast Spring integration
Beans Namespace
You can declare Hazelcast beans for Spring context using beans namespace
(default spring beans namespace) as well to declare hazelcast maps,
queues and others.
Hazelcast-Spring integration requires either hazelcast-spring jar or
hazelcast-all jar in the classpath.
<bean id="instance" class="com.hazelcast.core.Hazelcast"
factory-method="newHazelcastInstance">
<constructor-arg>
<bean class="com.hazelcast.config.Config">
<property name="groupConfig">
<bean class="com.hazelcast.config.GroupConfig">
<property name="name" value="dev"/>
<property name="password" value="pwd"/>
</bean>
</property>
<!-- and so on ... -->
</bean>
</constructor-arg>
</bean>
<bean id="map" factory-bean="instance" factory-method="getMap">
<constructor-arg value="map"/>
</bean>
Hazelcast Namespace
Hazelcast has Spring integration (requires version 2.5 or greater)
since 1.9.1 using hazelcast namespace.
Add namespace xmlns:hz="http://www.hazelcast.com/schema/config"
to beans tag in context file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:hz="http://www.hazelcast.com/schema/config"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-spring.xsd">
Use hz namespace shortcuts to declare cluster, its items and so on.
Hazelcast Instance
After that you can configure Hazelcast instance (node):
<hz:hazelcast id="instance">
<hz:config>
<hz:group name="dev" password="password"/>
<hz:properties>
<hz:property name="hazelcast.merge.first.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.merge.next.run.delay.seconds">5</hz:property>
</hz:properties>
<hz:network port="5701" port-auto-increment="false">
<hz:join>
<hz:multicast enabled="false"
multicast-group="224.2.2.3"
multicast-port="54327"/>
<hz:tcp-ip enabled="true">
<hz:members>10.10.1.2, 10.10.1.3</hz:members>
</hz:tcp-ip>
</hz:join>
</hz:network>
<hz:map name="map"
backup-count="2"
max-size="0"
eviction-percentage="30"
read-backup-data="true"
cache-value="true"
eviction-policy="NONE"
merge-policy="hz.ADD_NEW_ENTRY"/>
</hz:config>
</hz:hazelcast>
As of version hazelcast 1.9.3, you can easily configure map-store and near-cache too.
(For map-store you should set either class-name or implementation attribute.)
<hz:config>
<hz:map name="map1">
<hz:near-cache time-to-live-seconds="0" max-idle-seconds="60"
eviction-policy="LRU" max-size="5000" invalidate-on-change="true"/>
<hz:map-store enabled="true" class-name="com.foo.DummyStore"
write-delay-seconds="0"/>
</hz:map>
<hz:map name="map2">
<hz:map-store enabled="true" implementation="dummyMapStore"
write-delay-seconds="0"/>
</hz:map>
<bean id="dummyMapStore" class="com.foo.DummyStore" />
</hz:config>
It's possible to use placeholders instead of concrete values.
For instance, use property file app-default.properties for group configuration:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/app-default.properties</value>
</list>
</property>
</bean>
<hz:hazelcast id="instance">
<hz:config>
<hz:group
name="${cluster.group.name}"
password="${cluster.group.password}"/>
<!-- ... -->
</hz:config>
</hz:hazelcast>
Hazelcast client
Similar for client
<hz:client id="client"
group-name="${cluster.group.name}" group-password="${cluster.group.password}">
<hz:members>10.10.1.2:5701, 10.10.1.3:5701</hz:members>
</hz:client>
Maps, Queues and so on
You can declare beans for the following Hazelcast objects:
map
multiMap
queue
topic
set
list
executorService
idGenerator
atomicNumber
Example:
<hz:map id="map" instance-ref="instance" name="map"/>
<hz:multiMap id="multiMap" instance-ref="instance" name="multiMap"/>
<hz:queue id="queue" instance-ref="instance" name="queue"/>
<hz:topic id="topic" instance-ref="instance" name="topic"/>
<hz:set id="set" instance-ref="instance" name="set"/>
<hz:list id="list" instance-ref="instance" name="list"/>
<hz:executorService id="executorService" instance-ref="instance"
name="executorService"/>
<hz:idGenerator id="idGenerator" instance-ref="instance" name="idGenerator"/>
<hz:atomicNumber id="atomicNumber" instance-ref="instance"
name="atomicNumber"/>
    java sample
      import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.client.HazelcastClient;
import java.util.Map;
import java.util.Collection;
HazelcastInstance client = HazelcastClient.newHazelcastClient("dev",
"dev-pass", "10.90.0.1", "10.90.0.2:5702");
//All Cluster Operations that you can do with ordinary HazelcastInstance
Map<String, Customer> mapCustomers = client.getMap("customers");
// 아래는 put 하는 부분
mapCustomers.put("1", new Customer("Joe", "Smith"));
mapCustomers.put("2", new Customer("Ali", "Selam"));
mapCustomers.put("3", new Customer("Avi", "Noyan"));
// 아래는 get 하는 부분
Collection<Customer> colCustomers = mapCustomers.values();
for (Customer customer : colCustomers) {
// process customer
}
Hazelcast is an open source clustering and highly scalable data distribution platform for Java, which is:
Lightning-fast; thousands of operations/sec.
Fail-safe; no losing data after crashes.
Dynamically scales as new servers added.
Super-easy to use; include a single jar.
Hazecast는 자바를 지원하는 클러스터링과 확장성이 높은 데이터 분산 플랫폼을 위한 오픈 소스이다.
엄청 빠름
데이터 충돌 후에도 데이터를 잃지 않는 안정성
동적 확장성, 새로운 서버 추가시
엄청 쉬움, 사용하기 위해서는 jar 파일 하나만 포함시키면 된다.
<bean id="memCachedClient"
class="com.danga.MemCached.MemCachedClient"
depends-on="sockIOPool">
<property name="compressEnable" value="true" />
<property name="compressThreshold" value="65536" /><!-- 64K -->
</bean>
<bean id="sockIOPool" class="com.danga.MemCached.SockIOPool"
factory-method="getInstance" init-method="initialize"
destroy-method="shutDown">
<property name="servers">
<list>
<value>localhost:5701</value>
<value>localhost:5702</value>
<value>localhost:5703</value>
</list>
</property>
<property name="weights">
<list>
<value>1</value>
</list>
</property>
<property name="initConn" value="5" />
<property name="minConn" value="5" />
<property name="maxConn" value="10" />
<property name="maxIdle" value="21600000" /><!-- 6 hours: 1000 * 60 * 60 * 6 -->
<property name="maintSleep" value="30" />
<property name="nagle" value="false" />
<property name="socketTO" value="3000" />
<property name="socketConnectTO" value="0" />
</bean>
summary Hazelcast Spring integration
Beans Namespace
You can declare Hazelcast beans for Spring context using beans namespace
(default spring beans namespace) as well to declare hazelcast maps,
queues and others.
Hazelcast-Spring integration requires either hazelcast-spring jar or
hazelcast-all jar in the classpath.
<bean id="instance" class="com.hazelcast.core.Hazelcast"
factory-method="newHazelcastInstance">
<constructor-arg>
<bean class="com.hazelcast.config.Config">
<property name="groupConfig">
<bean class="com.hazelcast.config.GroupConfig">
<property name="name" value="dev"/>
<property name="password" value="pwd"/>
</bean>
</property>
<!-- and so on ... -->
</bean>
</constructor-arg>
</bean>
<bean id="map" factory-bean="instance" factory-method="getMap">
<constructor-arg value="map"/>
</bean>
Hazelcast Namespace
Hazelcast has Spring integration (requires version 2.5 or greater)
since 1.9.1 using hazelcast namespace.
Add namespace xmlns:hz="http://www.hazelcast.com/schema/config"
to beans tag in context file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:hz="http://www.hazelcast.com/schema/config"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-spring.xsd">
Use hz namespace shortcuts to declare cluster, its items and so on.
Hazelcast Instance
After that you can configure Hazelcast instance (node):
<hz:hazelcast id="instance">
<hz:config>
<hz:group name="dev" password="password"/>
<hz:properties>
<hz:property name="hazelcast.merge.first.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.merge.next.run.delay.seconds">5</hz:property>
</hz:properties>
<hz:network port="5701" port-auto-increment="false">
<hz:join>
<hz:multicast enabled="false"
multicast-group="224.2.2.3"
multicast-port="54327"/>
<hz:tcp-ip enabled="true">
<hz:members>10.10.1.2, 10.10.1.3</hz:members>
</hz:tcp-ip>
</hz:join>
</hz:network>
<hz:map name="map"
backup-count="2"
max-size="0"
eviction-percentage="30"
read-backup-data="true"
cache-value="true"
eviction-policy="NONE"
merge-policy="hz.ADD_NEW_ENTRY"/>
</hz:config>
</hz:hazelcast>
As of version hazelcast 1.9.3, you can easily configure map-store and near-cache too.
(For map-store you should set either class-name or implementation attribute.)
<hz:config>
<hz:map name="map1">
<hz:near-cache time-to-live-seconds="0" max-idle-seconds="60"
eviction-policy="LRU" max-size="5000" invalidate-on-change="true"/>
<hz:map-store enabled="true" class-name="com.foo.DummyStore"
write-delay-seconds="0"/>
</hz:map>
<hz:map name="map2">
<hz:map-store enabled="true" implementation="dummyMapStore"
write-delay-seconds="0"/>
</hz:map>
<bean id="dummyMapStore" class="com.foo.DummyStore" />
</hz:config>
It's possible to use placeholders instead of concrete values.
For instance, use property file app-default.properties for group configuration:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/app-default.properties</value>
</list>
</property>
</bean>
<hz:hazelcast id="instance">
<hz:config>
<hz:group
name="${cluster.group.name}"
password="${cluster.group.password}"/>
<!-- ... -->
</hz:config>
</hz:hazelcast>
Hazelcast client
Similar for client
<hz:client id="client"
group-name="${cluster.group.name}" group-password="${cluster.group.password}">
<hz:members>10.10.1.2:5701, 10.10.1.3:5701</hz:members>
</hz:client>
Maps, Queues and so on
You can declare beans for the following Hazelcast objects:
map
multiMap
queue
topic
set
list
executorService
idGenerator
atomicNumber
Example:
<hz:map id="map" instance-ref="instance" name="map"/>
<hz:multiMap id="multiMap" instance-ref="instance" name="multiMap"/>
<hz:queue id="queue" instance-ref="instance" name="queue"/>
<hz:topic id="topic" instance-ref="instance" name="topic"/>
<hz:set id="set" instance-ref="instance" name="set"/>
<hz:list id="list" instance-ref="instance" name="list"/>
<hz:executorService id="executorService" instance-ref="instance"
name="executorService"/>
<hz:idGenerator id="idGenerator" instance-ref="instance" name="idGenerator"/>
<hz:atomicNumber id="atomicNumber" instance-ref="instance"
name="atomicNumber"/>
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.client.HazelcastClient;
import java.util.Map;
import java.util.Collection;
HazelcastInstance client = HazelcastClient.newHazelcastClient("dev", "dev-pass", "10.90.0.1", "10.90.0.2:5702");
//All Cluster Operations that you can do with ordinary HazelcastInstance
Map<String, Customer> mapCustomers = client.getMap("customers");
// 아래는 put 하는 부분
mapCustomers.put("1", new Customer("Joe", "Smith"));
mapCustomers.put("2", new Customer("Ali", "Selam"));
mapCustomers.put("3", new Customer("Avi", "Noyan"));
// 아래는 get 하는 부분
Collection<Customer> colCustomers = mapCustomers.values();
for (Customer customer : colCustomers) {
// process customer
}