---
title: "Ehcache集群使用"
author: "wenjinglian"
date: "2012-07-21T15:48:38Z"
tags: ["JGroups", "ehcache", "集群"]
categories: ["编程语言"]
slug: "ehcache"
draft: false
lastmod: "2012-08-30T10:41:40Z"
id: 1602372
url: "https://nassir.iteye.com/blog/1602372"
---

集群的事例是由别人提供(附件也提供下载，我将相关包已导入)，我主要解释相关配置及注意事项

本事例采用的jgroups udp实现ehcache集群，下载下来找两台局域网的机器就能测试啦。

 

**核心配置：**

 

```
<ehcache updateCheck="false" dynamicConfig="false">

    <diskStore path="java.io.tmpdir/ehcache"/>

	<!-- mcast_addr=231.12.21.132; -->
	<cacheManagerPeerProviderFactory 
		class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
	    properties="connect=UDP(mcast_addr=231.12.21.132;mcast_port=45566;bind_port=33433;ip_ttl=32;
	    mcast_send_buf_size=150000;mcast_recv_buf_size=80000):
	    PING(timeout=2000;num_initial_members=6):
	    MERGE2(min_interval=5000;max_interval=10000):
	    FD_SOCK:VERIFY_SUSPECT(timeout=1500):
	    pbcast.NAKACK(gc_lag=10;retransmit_timeout=3000):
	    UNICAST(timeout=5000):
	    pbcast.STABLE(desired_avg_gossip=20000):
	    FRAG:
	    pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;
	    shun=false;print_local_addr=true)"
	 	propertySeparator="::"
     />

    <defaultCache
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
	     
    <cache name="mycache"
        maxElementsInMemory="10"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true">
        <cacheEventListenerFactory
	class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
		   properties="replicateAsynchronously=true, replicatePuts=true,
		   replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true" />		        
    </cache>

</ehcache>
```

 

参数解释：

 

**mcast\_addr:  组播地址;如果不设置默认为 228.8.8.8. (我的理解是就相当于是一个组，每个节点配置都一样)**

**mcast\_port:  组播的发送端口;如是不设置默认为7600** 

**ip\_ttl:  time-to-live 数据包在当前sokets中存活时间，默认为32s**

 

**bind\_port:  当前节点传输端口，如果不配置系统随机分配。（官方配置没提供这个端口配置，如果采用随机分配对后面服务器部署防火墙策略不好控制）**

 

 

jgroups官方udp api: [http://www.jgroups.org/javadoc/org/jgroups/protocols/UDP.html](http://www.jgroups.org/javadoc/org/jgroups/protocols/UDP.html)

 

```
public class UDP
extends TP
IP multicast transport based on UDP. Messages to the group (msg.dest == null) will be multicast (to all group members), whereas point-to-point messages (msg.dest != null) will be unicast to a single member. Uses a multicast and a unicast socket.

The following properties are read by the UDP protocol:
参数：
param mcast_addr - the multicast address to use; default is 228.8.8.8.
param mcast_port - (int) the port that the multicast is sent on; default is 7600
param ip_mcast - (boolean) flag whether to use IP multicast; default is true.
param ip_ttl - the default time-to-live for multicast packets sent out on this socket; default is 32.
param use_packet_handler - boolean, defaults to false. If set, the mcast and ucast receiver threads just put the datagram's payload (a byte buffer) into a queue, from where a separate thread will dequeue and handle them (unmarshal and pass up). This frees the receiver threads from having to do message unmarshalling; this time can now be spent receiving packets. If you have lots of retransmissions because of network input buffer overflow, consider setting this property to true.
```

 

**在linux服务器部署 防火墙端口开放策略：**

 mcast\_port 与 bind\_port 端口都要打开

/etc/sysconfig/iptables 配置：

 

```
[root@ldap apache-tomcat-7.0.12]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.3.5 on Sat Jul 21 13:57:58 2012
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 81 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT 
-A INPUT -p udp --dport 33433 -j ACCEPT
-A INPUT -p udp --dport 45566 -j ACCEPT 
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
```

 

 

mcast\_port :   -A INPUT -p udp --dport 33433 -j ACCEPT

bind\_port: -A INPUT -p udp --dport 33433 -j ACCEPT

 

可能会碰到的问题：

java.lang.RuntimeException: the type of the stack (IPv6) and the user supplied addresses (IPv4) don't match: /231.12.21.132.

解决方案：[http://nassir.iteye.com/blog/1602522](/blog/1602522)

 

linux服务器配置使用命令：

 

  netstat -alu   查看所有udp端口

 

查看Linux下端口被哪个进程占用

  如：查看9425 被哪个程序占用

  lsof -i:9425

 

 

参考：

深入探讨在集群环境中使用 EhCache 缓存系统

[http://www.ibm.com/developerworks/cn/java/j-lo-ehcache/index.html](http://www.ibm.com/developerworks/cn/java/j-lo-ehcache/index.html)

 

Ehcache详细解读

[http://raychase.iteye.com/blog/1545906](http://raychase.iteye.com/blog/1545906)

 

官方jGroups集群配置

[http://ehcache.org/documentation/replication/jgroups-replicated-caching](http://ehcache.org/documentation/replication/jgroups-replicated-caching)

 

(原)EhCache 集群演示程序

[http://www.oschina.net/question/12\_3984](/admin/blogs/1602372/ "http://www.oschina.net/question/12_3984")