Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9167577
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类: 架构设计与优化

2013-04-19 10:36:39

GemFire 客服端/服务器 缓存模式能提供动态的服务器连接池管理,均衡和有条件的服务器加载,逻辑的服务器组管理,以及对高效**器的自动失效备援。

和以往旧版本的GemFire相比,在分布式系统的连接管理中,旧版本是采用DistributedSystem类进行连接管理。最新版本提供了CacheServer 和 CacheClient类实现客户端/服务器模式。以下是一个简单的例子:

服务器配置:
1.Server.xml
Xml代码 复制代码 收藏代码
  1. xml version="1.0"?>  
  2.   
  3. quickstart.SimpleCacheLoader

    2.服务器在这里使用的是GemFire cacheserver 进程。

    客户端配置:

    1. Client.xml
    Xml代码 复制代码 收藏代码
    1. <client-cache>  
    2.     <pool name="client" subscription-enabled="true">  
    3.         <server host="localhost" port="40404" />  
    4.     pool>  
    5.   
    6.     <region name="exampleRegion">  
    7.         <region-attributes refid="CACHING_PROXY">  
    8.             <cache-listener>  
    9.                 <class-name>quickstart.SimpleCacheListenerclass-name>  
    10.             cache-listener>  
    11.         region-attributes>  
    12.     region>  
    13. client-cache>  


    2. ClientWorker.java,该类用于往cache中get和put数据。
    Java代码 复制代码 收藏代码
    1. public class ClientWorker {   
    2.   
    3.     public static final String EXAMPLE_REGION_NAME = "exampleRegion";   
    4.   
    5.     public static void main(String[] args) throws Exception {   
    6.   
    7.         System.out.println("Connecting to the distributed system and creating the cache.");   
    8.         // Create the cache which causes the cache-xml-file to be parsed   
    9.         ClientCache cache = new ClientCacheFactory()   
    10.                   .set("name""ClientWorker")   
    11.                   .set("cache-xml-file""xml/Client.xml")   
    12.                   .create();   
    13.   
    14.         // Get the exampleRegion   
    15.         Region exampleRegion = cache.getRegion(EXAMPLE_REGION_NAME);   
    16.         System.out.println("Example region \"" + exampleRegion.getFullPath() + "\" created in cache.");   
    17.         System.out.println();   
    18.         System.out.println("Getting three values from the cache server.");   
    19.         System.out.println("This will cause the server's loader to run, which will add the values");   
    20.         System.out.println("to the server cache and return them to me. The values will also be");   
    21.         System.out.println("forwarded to any other client that has subscribed to the region.");   
    22.   
    23.         // Get three values from the cache   
    24.         for (int count = 0; count < 3; count++) {   
    25.             String key = "key" + count;   
    26.             System.out.println("Getting key " + key);   
    27.             exampleRegion.get(key);   
    28.         }   
    29.   
    30.         System.out.println("Note the other client's region listener in response to these gets.");   
    31.         System.out.println("Press Enter to continue.");   
    32.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));   
    33.         bufferedReader.readLine();   
    34.   
    35.         System.out.println("Changing the data in my cache - all destroys and updates are forwarded");   
    36.         System.out.println("through the server to other clients. Invalidations are not forwarded.");   
    37.   
    38.         // Update one value in the cache   
    39.         System.out.println("Putting new value for key0");   
    40.         exampleRegion.put("key0""ClientValue0");   
    41.   
    42.         // Invalidate one entry in the cache   
    43.         System.out.println("Invalidating key1");   
    44.         exampleRegion.invalidate("key1");   
    45.   
    46.         // Destroy one entry in the cache   
    47.         System.out.println("Destroying key2");   
    48.         exampleRegion.destroy("key2");   
    49.   
    50.         // Close the cache and disconnect from GemFire distributed system   
    51.         System.out.println("Closing the cache and disconnecting.");   
    52.         cache.close();   
    53.   
    54.         System.out.println("In the other session, please hit Enter in the Consumer client");   
    55.         System.out.println("and then stop the cacheserver with 'cacheserver stop'.");   
    56.     }   
    57. }  


    3. ClientConsumer.java,模拟从server端获取数据。
    Java代码 复制代码 收藏代码
    1. public class ClientConsumer {   
    2.   
    3.     public static final String USAGE = "Usage: java ClientConsumer \n"  
    4.             + "  register-interest-type may be one of the following:\n"  
    5.             + "    all-keys    Register interest in all keys on the server\n"  
    6.             + "    keyset      Register interest in a set of keys on the server\n"  
    7.             + "    regex       Register interest in keys on the server matching a regular expression\n";   
    8.   
    9.     public static final String EXAMPLE_REGION_NAME = "exampleRegion";   
    10.   
    11.     private static enum RegisterInterestType {   
    12.         ALL_KEYS, KEYSET, REGEX   
    13.     }   
    14.   
    15.     public static void main(String[] args) throws Exception {   
    16.   
    17.         if (args.length != 1) {   
    18.             System.out.println(USAGE);   
    19.             System.exit(1);   
    20.         }   
    21.   
    22.         RegisterInterestType registerInterestType;   
    23.         if (args[0].equals("all-keys")) {   
    24.             registerInterestType = RegisterInterestType.ALL_KEYS;   
    25.         } else if (args[0].equals("keyset")) {   
    26.             registerInterestType = RegisterInterestType.KEYSET;   
    27.         } else if (args[0].equals("regex")) {   
    28.             registerInterestType = RegisterInterestType.REGEX;   
    29.         } else {   
    30.             registerInterestType = null;   
    31.             System.out.println(USAGE);   
    32.             System.exit(2);   
    33.         }   
    34.   
    35.         // Subscribe to the indicated key set   
    36.         System.out.println("Connecting to the distributed system and creating the cache.");   
    37.         // Create the cache which causes the cache-xml-file to be parsed   
    38.         ClientCache cache = new ClientCacheFactory()   
    39.                   .set("name""ClientConsumer")   
    40.                   .set("cache-xml-file""xml/Client.xml")   
    41.                   .create();   
    42.   
    43.         // Get the exampleRegion which is a subregion of /root   
    44.         Region exampleRegion = cache.getRegion(EXAMPLE_REGION_NAME);   
    45.         System.out.println("Example region \"" + exampleRegion.getFullPath() + "\" created in cache. ");   
    46.   
    47.         switch (registerInterestType) {   
    48.         case ALL_KEYS:   
    49.             System.out.println("Asking the server to send me all data additions, updates, and destroys. ");   
    50.             exampleRegion.registerInterest("ALL_KEYS");   
    51.             break;   
    52.         case KEYSET:   
    53.             System.out.println("Asking the server to send me events for data with these keys: 'key0', 'key1'");   
    54.             exampleRegion.registerInterest("key0");   
    55.             exampleRegion.registerInterest("key1");   
    56.             break;   
    57.         case REGEX:   
    58.             System.out.println("Asking the server to register interest in keys matching this");   
    59.             System.out.println("regular expression: 'k.*2'");   
    60.             exampleRegion.registerInterestRegex("k.*2");   
    61.             break;   
    62.         default:   
    63.             // Can't happen   
    64.             throw new RuntimeException();   
    65.         }   
    66.   
    67.         System.out.println("The data region has a listener that reports all changes to standard out.");   
    68.         System.out.println();   
    69.         System.out.println("Please run the worker client in another session. It will update the");   
    70.         System.out.println("cache and the server will forward the updates to me. Note the listener");   
    71.         System.out.println("output in this session.");   
    72.         System.out.println();   
    73.         System.out.println("When the other client finishes, hit Enter to exit this program.");   
    74.   
    75.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));   
    76.         bufferedReader.readLine();   
    77.   
    78.         System.out.println("Closing the cache and disconnecting.");   
    79.         cache.close();   
    80.     }   
    81. }  


    4. SimpleCacheLoader.java,如果该Region在服务器端没有数据,将自动加载数据。
    Java代码 复制代码 收藏代码
    1. public class SimpleCacheLoader implements CacheLoader, Declarable {   
    2.      
    3.   public String load(LoaderHelper helper) {   
    4.     String key = helper.getKey();   
    5.     System.out.println("    Loader called to retrieve value for " + key);   
    6.        
    7.     // Create a value using the suffix number of the key (key1, key2, etc.)   
    8.     return "LoadedValue" + (Integer.parseInt(key.substring(3)));   
    9.   }   
    10.      
    11.   public void close() {   
    12.     // do nothing   
    13.   }   
    14.      
    15.   public void init(Properties props) {   
    16.     // do nothing   
    17.   }   
    18. }  


    5. SimpleCacheListener.java,一个异步的监听器用于监听本地缓存的变化。
    Java代码 复制代码 收藏代码
    1. public class SimpleCacheListener extends CacheListenerAdapter implements Declarable {   
    2.   
    3.   public void afterCreate(EntryEvent e) {   
    4.     System.out.println("    Received afterCreate event for entry: " +   
    5.       e.getKey() + ", " + e.getNewValue());   
    6.   }   
    7.      
    8.   public void afterUpdate(EntryEvent e) {   
    9.     System.out.println("    Received afterUpdate event for entry: " +   
    10.       e.getKey() + ", " + e.getNewValue());   
    11.   }   
    12.      
    13.   public void afterDestroy(EntryEvent e) {   
    14.     System.out.println("    Received afterDestroy event for entry: " +   
    15.       e.getKey());   
    16.   }   
    17.   
    18.   public void afterInvalidate(EntryEvent e) {   
    19.     System.out.println("    Received afterInvalidate event for entry: " +   
    20.       e.getKey());   
    21.   }   
    22.   
    23.   public void afterRegionLive(RegionEvent e) {   
    24.     System.out.println("    Received afterRegionLive event, sent to durable clients after \nthe server has finished replaying stored events.  ");   
    25.   }   
    26.   
    27.   public void init(Properties props) {   
    28.     // do nothing   
    29.   }   
    30.   
    31. }  



    启动步骤:
    1. 启动GemFire cacheserver:
    Java代码 复制代码 收藏代码
    1. cacheserver start cache-xml-file=xml/Server.xml  


    2. 启动customer client:
    Java代码 复制代码 收藏代码
    1. java quickstart.ClientConsumer all-keys  


    3. 启动ClientWorker:
    Java代码 复制代码 收藏代码
    1. java quickstart.ClientWorker  


    4. 当所有客户端退出后,停止cacheserver:
    Java代码 复制代码 收藏代码
    1. cacheserver stop  


阅读(2504) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~