版本 4

     

    配置集群式的Infinispan 十分简单。 Infinispan使用JGroups 进行网络传输 并且JGroups 将会处理查找之类的复杂工作。

     

    一定要确保你使用的 GlobalConfiguration是cluster-aware的。 你需要设置传输类和传输属性。

     

    1.  建立传输

    以JGroups传输为例,你只需要设置:

     

    GlobalConfiguration gc = new GlobalConfiguration();
    gc.setTransportClass(JGroupsTransport.class.getName());
    
    
    
    
    

     

    或者在你的XML配置文件中进行如下配置:

     

    <global>
         <transport transportClass="org.infinispan.remoting.transport.jgroups.JGroupsTransport" />
    </global>
    
    

     

    注意: GlobalConfiguration.getClusteredDefalut() is a quick way to get a preconfigured, cluster-aware GlobalConfiguration and can be used as a starting point to fine tuning the configuration.

    2.  传输属性

    现在您可以对一些传输属性进行设置。如果您没有进行设置的话, Infinispan将会使用默认的传输属性 。在JGroups例子中, 将会使用jgroup.jar文件中包含的udp.xml文件来对JGroup channel进行设置。

     

    您可以通过提供一个JDK Properties实例来设置GlobalConfiguration的的属性。

     

    Properties p = new Properties();
    // set some properties
    gc.setTransportProperties(p);
    
    


    可用的属性包括:

     

    属性名
    属性值
    configurationString
    一个旧有格式的关于 JGroups 配置信息的字符串
    configurationXml
    表示JGroups 配置信息的XML形式的字符串
    configurationFile
    JGroups 配置文件的名字,该配置文件必须在 classpath下。

     

    关于对JGroups进行设置的更多详细信息请参照JGroups 文档

     

    您也可以通过XML的形式设置相关属性:

    <global>
      <transport transportClass="org.infinispan.remoting.transport.jgroups.JGroupsTransport">
        <properties>
          <property name="configurationFile" value="udp.xml"/>
        </properties>
      </transport>
    
    
    
    
    
    
    
    </global>
    
    
    
    
    

     

    3. 缓存配置

     

    现在要做的就是进行全局配置,全局配置将会影响CacheManager的生成。你需要确保你的缓存使用的是集群模式。除了Configuration.CacheMode.LOCAL.之外,任何Configuration.CacheMode中包含的模式都是有效地集群模式。

     

    4.  整合

     

    4.1.  编程式:
    GlobalConfiguration gc = GlobalConfiguration.getClusteredDefault();
    Configuration c = new Configuration();
    c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
    
    CacheManager cm = new DefaultCacheManager(gc, c);
    Cache defaultCache = cm.getCache();
    

     

    4.2.  声明式

     

    cfg.xml:

    <infinispan>
      <global>
        <!-- not specifying details here will force default transport -->
        <transport />
      </global>
      <default>
        <clustering mode="replication" />
      </default>
    </infinispan>
    

     

    CacheManager cm = new DefaultCacheManager("cfg.xml");
    Cache defaultCache = cm.getCache();
    

     

    5.  共享JGroups channels

    默认情况下,同一个CacheManager创建的所有缓存实例会共享其所有的同一个JGroups channel 和多个RPC 消息。例如,

    CacheManager cm = getCacheManagerFromSomewhere();
    Cache cache1 = cm.getCache("replSyncCache");
    Cache cache2 = cm.getCache("replAsyncCache");
    Cache cache3 = cm.getCache("invalidationSyncCache");
    

     

    缓存1, 2 和3 使用相同的 JGroups channel.

     

     

    6.  更多

     

    更多细节请参照发布版中包含的实例XML配置文件。