我正在尝试做以下事情:
CacheListener
entrySet
方法。我已经无法从Geode客户端访问redis数据。我必须执行以下操作:
region.get(Coder.stringToByteArrayWrapper("key"));
我在使region. entrySet()
工作时也遇到了很多麻烦。首先,它根本不适用于ClientRegion on快捷方式。PROXY
,似乎只有50%的时间适用于客户区域快捷方式。CACHING_PROXY
。
这是我用来测试的代码(请注意,我使用莴苣作为redis客户端):
@Test
public void test_subscribe() throws InterruptedException, ExecutionException {
ClientCache cache = new ClientCacheFactory()
.addPoolLocator(HOST, LOCATOR_PORT)
.create();
@SuppressWarnings({ "rawtypes", "unchecked" })
CacheListener<ByteArrayWrapper, ByteArrayWrapper> cl = new CacheListenerAdapter() {
@Override
public void afterCreate(EntryEvent event) {
System.out.println("Created: " + event.getKey() + " = " + event.getNewValue());
}
@Override
public void afterUpdate(EntryEvent event) {
System.out.println("Updated: " + event.getKey() + " replacing " + event.getOldValue() + "with" + event.getNewValue());
}
};
Region<ByteArrayWrapper, ByteArrayWrapper> region = cache
.<ByteArrayWrapper, ByteArrayWrapper> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
.addCacheListener(cl)
.setKeyConstraint(ByteArrayWrapper.class)
.setValueConstraint(ByteArrayWrapper.class)
.create(GeodeRedisServer.STRING_REGION);
RedisClient client = RedisClient.create("redis://" + HOST);
StatefulRedisConnection<String, String> connection = client.connect();
RedisAsyncCommands<String, String> cmd = connection.async();
cmd.set("1", "HelloGeodeRedis").get();
cmd.set("2", "WorldGeodeRedis" + System.currentTimeMillis()).get();
System.out.println("FromRedis: " + cmd.get("2").get());
System.out.println("FromGeode: " + region.get(Coder.stringToByteArrayWrapper("2")));
for (Map.Entry<?, ?> entry : region.entrySet()) {
System.out.format("key = %s, value = %s\n", entry.getKey(), entry.getValue());
}
cache.close();
}
我想知道50%的事情是否与我正在运行的服务器有关:
gfsh>describe region --name=ReDiS_StRiNgS
..........................................................
Name : ReDiS_StRiNgS
Data Policy : persistent partition
Hosting Members : my-redis
Non-Default Attributes Shared By Hosting Members
Type | Name | Value
------ | ----------- | --------------------
Region | size | 2
| data-policy | PERSISTENT_PARTITION
gfsh>describe region --name my-region
..........................................................
Name : my-region
Data Policy : persistent replicate
Hosting Members : my-server
my-redis
Non-Default Attributes Shared By Hosting Members
Type | Name | Value
------ | ----------- | --------------------
Region | data-policy | PERSISTENT_REPLICATE
| size | 2
| scope | distributed-ack
gfsh>list members
Name | Id
---------------- | -----------------------------------------------------------
my-locator | 172.16.202.245(my-locator:21234:locator)<ec><v0>:1024
my-server | 172.16.202.245(my-server:22154)<v1>:1025
my-redis | 172.16.202.245(my-redis:24890)<v2>:1026
如您所见,我手动创建的区域由两个服务器托管,但redis创建的区域仅由redis服务器托管。
我得到50%的时间的错误如下:
org.apache.geode.cache.client.ServerOperationException: remote server on My-Computer(4352:loner):64103:58d54999: While performing a remote get
at org.apache.geode.cache.client.internal.AbstractOp.processObjResponse(AbstractOp.java:285)
at org.apache.geode.cache.client.internal.GetOp$GetOpImpl.processResponse(GetOp.java:143)
at org.apache.geode.cache.client.internal.AbstractOp.attemptReadResponse(AbstractOp.java:171)
at org.apache.geode.cache.client.internal.AbstractOp.attempt(AbstractOp.java:382)
at org.apache.geode.cache.client.internal.ConnectionImpl.execute(ConnectionImpl.java:275)
at org.apache.geode.cache.client.internal.pooling.PooledConnection.execute(PooledConnection.java:332)
at org.apache.geode.cache.client.internal.OpExecutorImpl.executeWithPossibleReAuthentication(OpExecutorImpl.java:900)
at org.apache.geode.cache.client.internal.OpExecutorImpl.execute(OpExecutorImpl.java:158)
at org.apache.geode.cache.client.internal.OpExecutorImpl.execute(OpExecutorImpl.java:115)
at org.apache.geode.cache.client.internal.PoolImpl.execute(PoolImpl.java:763)
at org.apache.geode.cache.client.internal.GetOp.execute(GetOp.java:91)
at org.apache.geode.cache.client.internal.ServerRegionProxy.get(ServerRegionProxy.java:116)
at org.apache.geode.internal.cache.LocalRegion.findObjectInSystem(LocalRegion.java:2776)
at org.apache.geode.internal.cache.LocalRegion.nonTxnFindObject(LocalRegion.java:1488)
at org.apache.geode.internal.cache.LocalRegionDataView.findObject(LocalRegionDataView.java:175)
at org.apache.geode.internal.cache.LocalRegion.get(LocalRegion.java:1377)
at org.apache.geode.internal.cache.LocalRegion.get(LocalRegion.java:1310)
at org.apache.geode.internal.cache.LocalRegion.get(LocalRegion.java:1295)
at org.apache.geode.internal.cache.AbstractRegion.get(AbstractRegion.java:320)
at trial.GeodeTest.test_subscribe(GeodeTest.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.geode.cache.RegionDestroyedException: Server connection from [identity(192.168.64.106(4352:loner):64103:58d54999,connection=1; port=64103]: Region named /ReDiS_StRiNgS/ReDiS_StRiNgS was not found during get request
at org.apache.geode.internal.cache.tier.sockets.BaseCommand.writeRegionDestroyedEx(BaseCommand.java:615)
at org.apache.geode.internal.cache.tier.sockets.command.Get70.cmdExecute(Get70.java:126)
at org.apache.geode.internal.cache.tier.sockets.BaseCommand.execute(BaseCommand.java:165)
at org.apache.geode.internal.cache.tier.sockets.ServerConnection.doNormalMsg(ServerConnection.java:780)
at org.apache.geode.internal.cache.tier.sockets.ServerConnection.doOneMessage(ServerConnection.java:911)
at org.apache.geode.internal.cache.tier.sockets.ServerConnection.run(ServerConnection.java:1166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.geode.internal.cache.tier.sockets.AcceptorImpl$1$1.run(AcceptorImpl.java:523)
at java.lang.Thread.run(Thread.java:745)
所以你有完整的免责声明,我测试这个的原因是,我想使用kafkaredis将数据从kafka主题放入gede,以避免自己编写gedekafka连接器。
编辑:由于@Swapnil,50%的问题已经得到解决,但是现在我又回到了让entrySet
和事件通知工作的问题上。似乎除非我强行获取使用redis客户端放置的密钥,否则我不会收到任何EntryEvent
通知。
我没有收到事件的问题是因为我没有注册我感兴趣的密钥。
添加region. regsterInterestRegex(".*");
允许标记每个键的兴趣。它需要使用.setPoolSubscriptionEnable(true)
设置ClientCache
。
ClientCache cache = new ClientCacheFactory()
.addPoolLocator(HOST, LOCATOR_PORT)
.setPoolSubscriptionEnabled(true)
.create();
// ...
Region<ByteArrayWrapper, ByteArrayWrapper> region = cache
.<ByteArrayWrapper, ByteArrayWrapper> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
.addCacheListener(cl)
.setKeyConstraint(ByteArrayWrapper.class)
.setValueConstraint(ByteArrayWrapper.class)
.create(GeodeRedisServer.STRING_REGION);
region.registerInterestRegex(".*");
// ...
我怀疑这是您启动Geode服务器的方式。当我像这样设置我的Geode集群时,我没有遇到任何问题:
gfsh>start locator --name=loc1
gfsh>start server --name=serv1 --redis-port=1111 --redis-bind-address=localhost
gfsh>start server --name=serv2 --server-port=40405 --redis-port=2222 --redis-bind-address=localhost
描述区域向我显示RedisStrings是在两台服务器上创建的:
gfsh>describe region --name=/ReDiS_StRiNgS .......................................................... Name : ReDiS_StRiNgS Data Policy : partition Hosting Members : serv2 serv1 Non-Default Attributes Shared By Hosting Members Type | Name | Value ------ | ----------- | --------- Region | size | 0 | data-policy | PARTITION
然后从我的测试程序中,我通过Redis(Jedis客户端)插入数据,然后从Geode成功读回数据:
public void putRedis() {
Jedis jedis = new Jedis("localhost", 1111);
for (int i=0; i<10; i++) {
jedis.set("foo"+i, "bar"+i);
}
}
public void readGeode() {
ClientCacheFactory clientCacheFactory = new ClientCacheFactory();
clientCacheFactory.addPoolLocator("localhost", 10334);
ClientCache client = clientCacheFactory.create();
Region redisStrings = client.createClientRegionFactory(ClientRegionShortcut.PROXY).create("ReDiS_StRiNgS");
for (int i=0; i<10; i++) {
System.out.println(redisStrings.get(Coder.stringToByteArrayWrapper("foo"+i)));
}
}