Gemfire 6.5 캐시 서버 관리 모델 분석
이전 버전의 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Cognos BI Dynamic Query Mode (DQM) 캐시 보존 기간 이야기Cognos BI에서는 보고서를 참조할 때 쿼리 실행 모드로 동적 쿼리 모드(DQM) 및 호환 쿼리 모드(CQM)가 있습니다. 현재의 Cognos BI에서는 DQM이 기본값이지만 DQM은 보고서 실행 시 Cognos...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.