Gemfire 6.5 캐시 서버 관리 모델 분석

11554 단어 캐시gemfire
GemFire 고객지원단/서버 캐시 모드는 동적 서버 연결 탱크 관리, 균형적이고 조건적인 서버 불러오기, 논리적인 서버 그룹 관리, 그리고 효율적인 서버에 대한 자동 실효 지원을 제공할 수 있다.
이전 버전의 GemFire에 비해 분포식 시스템의 연결 관리에서 이전 버전은 Distributed System 클래스를 이용하여 연결 관리를 한다.최신 버전은 CacheServer와 CacheClient 클래스를 제공하여 클라이언트/서버 모드를 실현합니다.다음은 간단한 예입니다.
서버 구성:
1.Server.xml

<?xml version="1.0"?>

<!-- Server.xml
     Configures a cache to serve caching clients at port 40405.
     The example region also is configured with a loader. 
-->

<!DOCTYPE cache PUBLIC
  "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
  "http://www.gemstone.com/dtd/cache6_5.dtd">
<cache>
    <cache-server port="40404"/>
    <region name="exampleRegion">
      <region-attributes refid="REPLICATE">
        <cache-loader>
          <class-name>quickstart.SimpleCacheLoader</class-name>
        </cache-loader>
      </region-attributes>
    </region>
</cache>

2. 서버에서 사용하는 것은 GemFire cacheserver 프로세스입니다.
클라이언트 구성:
1. Client.xml

<client-cache>
	<pool name="client" subscription-enabled="true">
		<server host="localhost" port="40404" />
	</pool>

	<region name="exampleRegion">
		<region-attributes refid="CACHING_PROXY">
			<cache-listener>
				<class-name>quickstart.SimpleCacheListener</class-name>
			</cache-listener>
		</region-attributes>
	</region>
</client-cache>

2. ClientWorker.java, 이 클래스는cache에서 get과put 데이터에 사용됩니다.

public class ClientWorker {

	public static final String EXAMPLE_REGION_NAME = "exampleRegion";

	public static void main(String[] args) throws Exception {

		System.out.println("Connecting to the distributed system and creating the cache.");
		// Create the cache which causes the cache-xml-file to be parsed
		ClientCache cache = new ClientCacheFactory()
                  .set("name", "ClientWorker")
                  .set("cache-xml-file", "xml/Client.xml")
                  .create();

		// Get the exampleRegion
		Region<String, String> exampleRegion = cache.getRegion(EXAMPLE_REGION_NAME);
		System.out.println("Example region \"" + exampleRegion.getFullPath() + "\" created in cache.");
		System.out.println();
		System.out.println("Getting three values from the cache server.");
		System.out.println("This will cause the server's loader to run, which will add the values");
		System.out.println("to the server cache and return them to me. The values will also be");
		System.out.println("forwarded to any other client that has subscribed to the region.");

		// Get three values from the cache
		for (int count = 0; count < 3; count++) {
			String key = "key" + count;
			System.out.println("Getting key " + key);
			exampleRegion.get(key);
		}

		System.out.println("Note the other client's region listener in response to these gets.");
		System.out.println("Press Enter to continue.");
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		bufferedReader.readLine();

		System.out.println("Changing the data in my cache - all destroys and updates are forwarded");
		System.out.println("through the server to other clients. Invalidations are not forwarded.");

		// Update one value in the cache
		System.out.println("Putting new value for key0");
		exampleRegion.put("key0", "ClientValue0");

		// Invalidate one entry in the cache
		System.out.println("Invalidating key1");
		exampleRegion.invalidate("key1");

		// Destroy one entry in the cache
		System.out.println("Destroying key2");
		exampleRegion.destroy("key2");

		// Close the cache and disconnect from GemFire distributed system
		System.out.println("Closing the cache and disconnecting.");
		cache.close();

		System.out.println("In the other session, please hit Enter in the Consumer client");
		System.out.println("and then stop the cacheserver with 'cacheserver stop'.");
	}
}

3. ClientConsumer.java, 서버에서 데이터를 가져오는 시뮬레이션.

public class ClientConsumer {

	public static final String USAGE = "Usage: java ClientConsumer <register-interest-type>
" + " register-interest-type may be one of the following:
" + " all-keys Register interest in all keys on the server
" + " keyset Register interest in a set of keys on the server
" + " regex Register interest in keys on the server matching a regular expression
"; public static final String EXAMPLE_REGION_NAME = "exampleRegion"; private static enum RegisterInterestType { ALL_KEYS, KEYSET, REGEX } public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println(USAGE); System.exit(1); } RegisterInterestType registerInterestType; if (args[0].equals("all-keys")) { registerInterestType = RegisterInterestType.ALL_KEYS; } else if (args[0].equals("keyset")) { registerInterestType = RegisterInterestType.KEYSET; } else if (args[0].equals("regex")) { registerInterestType = RegisterInterestType.REGEX; } else { registerInterestType = null; System.out.println(USAGE); System.exit(2); } // Subscribe to the indicated key set System.out.println("Connecting to the distributed system and creating the cache."); // Create the cache which causes the cache-xml-file to be parsed ClientCache cache = new ClientCacheFactory() .set("name", "ClientConsumer") .set("cache-xml-file", "xml/Client.xml") .create(); // Get the exampleRegion which is a subregion of /root Region<String,?> exampleRegion = cache.getRegion(EXAMPLE_REGION_NAME); System.out.println("Example region \"" + exampleRegion.getFullPath() + "\" created in cache. "); switch (registerInterestType) { case ALL_KEYS: System.out.println("Asking the server to send me all data additions, updates, and destroys. "); exampleRegion.registerInterest("ALL_KEYS"); break; case KEYSET: System.out.println("Asking the server to send me events for data with these keys: 'key0', 'key1'"); exampleRegion.registerInterest("key0"); exampleRegion.registerInterest("key1"); break; case REGEX: System.out.println("Asking the server to register interest in keys matching this"); System.out.println("regular expression: 'k.*2'"); exampleRegion.registerInterestRegex("k.*2"); break; default: // Can't happen throw new RuntimeException(); } System.out.println("The data region has a listener that reports all changes to standard out."); System.out.println(); System.out.println("Please run the worker client in another session. It will update the"); System.out.println("cache and the server will forward the updates to me. Note the listener"); System.out.println("output in this session."); System.out.println(); System.out.println("When the other client finishes, hit Enter to exit this program."); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); bufferedReader.readLine(); System.out.println("Closing the cache and disconnecting."); cache.close(); } }

4. SimpleCacheLoader.java, 이 Region이 서버에 데이터가 없으면 자동으로 데이터를 불러옵니다.

public class SimpleCacheLoader implements CacheLoader<String,String>, Declarable {
  
  public String load(LoaderHelper<String,String> helper) {
    String key = helper.getKey();
    System.out.println("    Loader called to retrieve value for " + key);
    
    // Create a value using the suffix number of the key (key1, key2, etc.)
    return "LoadedValue" + (Integer.parseInt(key.substring(3)));
  }
  
  public void close() {
    // do nothing
  }
  
  public void init(Properties props) {
    // do nothing
  }
}


5. SimpleCacheListener.java, 로컬 캐시의 변화를 감청하는 비동기적인 감청기.

public class SimpleCacheListener<K,V> extends CacheListenerAdapter<K,V> implements Declarable {

  public void afterCreate(EntryEvent<K,V> e) {
    System.out.println("    Received afterCreate event for entry: " +
      e.getKey() + ", " + e.getNewValue());
  }
  
  public void afterUpdate(EntryEvent<K,V> e) {
    System.out.println("    Received afterUpdate event for entry: " +
      e.getKey() + ", " + e.getNewValue());
  }
  
  public void afterDestroy(EntryEvent<K,V> e) {
    System.out.println("    Received afterDestroy event for entry: " +
      e.getKey());
  }

  public void afterInvalidate(EntryEvent<K,V> e) {
    System.out.println("    Received afterInvalidate event for entry: " +
      e.getKey());
  }

  public void afterRegionLive(RegionEvent e) {
    System.out.println("    Received afterRegionLive event, sent to durable clients after 
the server has finished replaying stored events. "); } public void init(Properties props) { // do nothing } }

시작 단계:
1. GemFire 캐시서버 시작:

cacheserver start cache-xml-file=xml/Server.xml

2. 고객 클라이언트를 시작합니다.

java quickstart.ClientConsumer all-keys

3. ClientWorker를 시작합니다.

java quickstart.ClientWorker

4. 모든 클라이언트가 종료되면 cacheserver를 중지합니다.

cacheserver stop

본문 발췌문:
http://community.gemstone.com/display/gemfire/Server-Managed+Caching+Example

좋은 웹페이지 즐겨찾기