JBoss DataGrid的集群部署与訪问

集群部署

JDG的缓存模式包含本地(Local)模式和集群(Clustered)模式。本项目採用多节点的Clustered模式部署。数据在多个节点的子集间进行复制。而不是同步拷贝到全部的节点。

使用子集复制能够提升容错的效率但对可伸缩性不会造成太大影响。在使用Clustered模式部署之前,应该配置JGroup。

1.       使用UDP方式广播。

l  适用于大的集群(超过100节点)。

l  适用于Invalidation和Replication模式。

l  提高socket通信的效率。

2.       使用TCP方式广播。

更适合于distribution模式的小规模(少于100节点)的集群,这时由于TCP协议在点对点通信中更加高效。

Clustered模式又分为Invalidation、Replication和Distribution模式。

Distribution模式

JDG的Distribution模式能够存储缓存数据在集群的子集节点。而不是存储数据到每个节点中。通常存储到多于1个节点来提供数据冗余和容错。

Distribution模式使用一致性Hash算法从集群中选择存储数据的节点,一致性Hash算法配置为一个缓存数据存储到多个副本。

副本数的设置须要平衡性能和容错,过多的副本会影响性能而过少的副本会造成节点失效时丢失数据。

在Distribution模式中,一个put操作会运行num_copies次远程调用,相同在随意节点的get操作会运行至少一次的远程调用。

实际上,get操作甚至也会运行num_copies次远程调用。但它们是并行的,仅仅要有一个返回。结果就会被返回给调用者。

读一致性

因为get操作是并行从多个节点取数并使用第一个返回的结果,在使用异步方式时可能会导致数据的不一致。注意这样的情况仅仅会出如今异步调用方式,假设是同步方式。则不会出现读不一致。

配置缓存容器和缓存

JDG的Cache在使用时,必须在standalone/configuration/clustered.xml配置文件里设置要使用的Cache配置。假设client訪问了未配置命名的Cache,将视为非法操作。

在<subsystemxmlns="urn:infinispan:server:core:5.2"default-cache-container="clustered"> …… </subsystem>中配置cache-container和distributed-cache,本项目使用分布式缓存模式。

节点<cache-containername="clustered" default-cache="default">中添加对Cache的配置节点,如以下的名称为default的cache(默认缓存配置):

<cache-container name="clustered" default-cache="default">

    <transport executor="infinispan-transport" lock-timeout="60000"/>

<distributed-cache name="default" mode="SYNC" segments="20" owners="2" remote-timeout="30000" start="EAGER">

         <locking isolation="READ_COMMITTED" acquire-timeout="30000" concurrency-level="1000" striping="false"/>

         <transaction mode="NONE"/>

</distributed-cache>

</cache-container>

 

以上的配置项目并不是全部的都是必须,各节点说明见下:

#定义缓存默认的过期策略

#lifespan: 缓存条目最大的生存时间。单位毫秒,-1表示从只是期。

# max-idle: 缓存条目的最大空暇时间。单位毫秒,-1表示从只是期。假设空暇时间超过该值,条目将被视为过期。

#interval: 从缓存中清除过期条目的运行间隔时间(毫秒)。假设要全然禁用定期回收任务,设置为-1

<expirationlifespan=”2000” max-idle=”1000”/>

#定义缓存默认的回收策略

#eviction: 指定回收策略。

可用的回收策略包含UNORDERED,FIFO,LIRSNONE(禁用回收策略)

#max-entries: 指定Cache实例中的最大条目数量,-1表示不限制。

实际取值为>=选定值的2的幂的最小值

<evictionstrategy="LRU" max-entries="10000"/>

#定义缓存锁

#isolation: 定义隔离级别。可用的隔离级别包含NONE,READ_UNCOMMITED,READ_COMMITED,REPEATABLE_READ,SERIALIZABLE,默认REPEATABLE_READ

#striping: 锁条带化。

假设为true,使用共享锁池来维护全部须要锁定的条目。否则,为每一个条目创建一个锁。锁条带化有助于控制内存占用。但可能会减少系统的并发能力。

#acquire-timeout: 尝试获取一个特定锁的最大超时时间。

#concurrency-level: 锁容器的并发级别。依据和infinispan并发交互的线程数量来调整这个值。

#concurrent-updates: 仅用于非事务缓存。假设设置为true(默认值),则缓存在并发更新时保持数据的一致性。对于集群模式将带来额外的RPC成本,假设你的应用不会并发写数据。禁用该标志以提升性能。

<lockingisolation="READ_COMMITTED" acquire-timeout="30000"concurrency-level="1000" striping="false"/>

#定义缓存事务

#尽管能够定义server缓存支持事务,但眼下没有可用的协议来支持事务能力。

<transactionmode="NONE"/>

启动JDG集群

能够把JDG部署在多个节点构成一个集群,眼下我们的项目使用的是数据缓存服务使用默认3个节点的分布式集群。

在启动时各个节点的nodeName必须予以区分,比如分别命名为nodeA、nodeB和nodeC。

JDG是执行在JBoss容器中的,在启动时即启动了JBoss。假设server已经执行了JBOSS,请在启动时设置portoffset。以避免冲突。

同一时候为了避免和JBoss服务器的JBOSS_HOME冲突。定义环境变量JDG_HOME(比如exportJDG_HOME=/usr/local/jboss-datagrid-server-6.1.0),并改动bin/clustered.sh中的全部JBOSS_HOME替换为JDG_HOME。

以nodeA节点为例(JDG对外訪问的port为11222,以下offset后为11322)。serverip是192.168.1.100,运行启动命令为:

./clustered.sh-Djboss.socket.binding.port-offset=100 -Djboss.bind.address=192.168.1.100 -Djboss.node.name=nodeA

 client訪问

远程client能够使用REST, memcached或HotRod协议,我们这里使用HotRod协议,它是一种二进制协议,性能较好,同一时候它提供了自己主动的负载均衡和failover。

配置文件

客户端訪问缓存服务端的配置文件默认命名为hotrod-client.properties。定义解释见下。

############JDG server配置############

##请求均衡策略,default =org.infinispan.client.hotrod.impl.transport.tcp.RoundRobinBalancingStrategy

#infinispan.client.hotrod.request_balancing_strategy=

##server列表,default = 127.0.0.1:11222

infinispan.client.hotrod.server_list=192.168.1.100:11322;192.168.1.101:11322;192.168.1.102:11322

##是否强迫返回值, default = false

#infinispan.client.hotrod.force_return_values=

##TCP_NO_DELAY, default = true

#infinispan.client.hotrod.tcp_no_delay=

##启动时是否发送PING请求来获取CLUSTER拓扑, default = true

#infinispan.client.hotrod.ping_on_startup=

##控制使用的传输机制。眼下仅仅支持TCP, default =org.infinispan.client.hotrod.impl.transport.tcp.TcpTransportFactory

#infinispan.client.hotrod.transport_factory=

##序列化使用的Marshaller, default =org.infinispan.marshall.jboss.GenericJBossMarshaller

##假设要减少传输负载,能够配置为ApacheAvroMarshaller

#infinispan.client.hotrod.marshaller=

##指定自己定义的AsyncExecutorFactory, default =org.infinispan.client.hotrod.impl.async.DefaultAsyncExecutorFactory

#infinispan.client.hotrod.async_executor_factory=

##指定并发线程池大小, default = 10

#infinispan.client.hotrod.default_executor_factory.pool_size=

##指定并发队列大小, default = 100000

#infinispan.client.hotrod.default_executor_factory.queue_size=

##Hash函数实现的版本号及一致性Hash算法,和HotRod的server版本号相关

#infinispan.client.hotrod.hash_function_impl.1=

##序列化和反序列化键的缓存同意字节数。目的是避免数组大小调整, default =64

#infinispan.client.hotrod.key_size_estimate=

##序列化和反序列化值的缓存同意字节数,目的是避免数组大小调整, default =512

#infinispan.client.hotrod.value_size_estimate=

##socket读超时, default = 60000 (60 seconds)

infinispan.client.hotrod.socket_timeout=50000

##socket连接超时, default = 60000 (60 seconds)

infinispan.client.hotrod.connect_timeout=10000

##指定client使用的协议版本号, default = 1.1,其它值还有1.0

#infinispan.client.hotrod.protocol_version=

##指定错误时的重试次数, default = 10

#infinispan.client.hotrod.max_retries=

 

############连接池配置############

##指定每一个server的最大连接数,负值表示没有限制,默认-1

maxActive=100

##指定server组内同意的全局持久连接的数量,负值表示没有限制,默认-1

maxTotal=100

##指定每一个server空暇持久连接的最大数,负值表示没有限制。默认-1

maxIdle=20

##指定当连接池耗尽时,server怎样响应:

##0-抛出异常给调用者

##1-堵塞调用者。直到有空暇的连接

##2-创建一个新的连接(不受maxActive的限制)

##默认值是1

#whenExhaustedAction=1

##检查空暇连接的Eviction线程每次执行间隔的时间,默认是2分钟

#timeBetweenEvictionRunsMillis=120000

##在空暇池中的连接存在多长时间须要被销毁,负值表示没有空暇连接被销毁,默认值为30分钟

#minEvictableIdleTimeMillis=1800000

##指定在Eviction线程运行时。空暇的连接是否通过发送一个TCP数据包到server来验证,

##即无法验证的连接将从池中被清除

##默认值为true

#testWhileIdle=true

##指定每一个server最小的可用连接的空暇线程数。默认值为1

#minIdle=1

client訪问代码

import java.net.URL;

import java.util.Map;

 

import org.infinispan.client.hotrod.RemoteCache;

import org.infinispan.client.hotrod.RemoteCacheManager;

import org.infinispan.client.hotrod.ServerStatistics;

 

 

public class Quickstart {

 

 public static void main(String[] args) {

 

  URL resource = Thread.currentThread().getContextClassLoader()

                                     .getResource("hotrod-client.properties");

  RemoteCacheManager cacheContainer = new RemoteCacheManager(resource, true);

 

  //获得一个远程的Cache

  RemoteCache cache = cacheContainer.getCache("myCache");

 

  //put数据到缓存中。然后确认是否存在

  cache.put("name", "paul");

  if(cache.get("name").equals("paul")){

   System.out.println("Cache Hit!");

  } else {

   System.out.println("Cache Miss!");

  }

 

  //删除缓存数据

  cache.remove("name");

 

  cacheContainer.stop();

 }

}




posted @ 2017-04-20 14:47  jhcelue  阅读(615)  评论(0编辑  收藏  举报