gpt4 book ai didi

java - 如何使用 Redis 适配器写入 Geode,然后使用 Geode 客户端读取/响应事件?

转载 作者:IT王子 更新时间:2023-10-29 06:09:37 27 4
gpt4 key购买 nike

我正在尝试执行以下操作:

  1. 使用 redis 连接器将一些数据放入 geode
  2. 使用 CacheListener 对一些关键的创建/更新事件作出 react
  3. 使用 Geode 客户端及其区域上的 entrySet 方法读取该数据。

我已经无法从我的 Geode 客户端访问 redis 数据。我必须执行以下操作:

region.get(Coder.stringToByteArrayWrapper("key"));

我在使 region.entrySet() 工作时也遇到了很多麻烦。首先,它对 ClientRegionShortcut.PROXY 根本不起作用,而且它似乎只有 50% 的时间与 ClientRegionShortcut.CACHING_PROXY 一起工作。

这是我用来测试这个的代码(注意我使用 lettuce 作为 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)

所以你有完整的免责声明,我测试这个的原因是我想使用 kafka-connect-redis 将来自 kafka 主题的数据放入 geode 中。避免自己编写 geode kafka 连接器。

编辑:由于@Swapnil,50% 的问题已得到解决,但现在我又遇到了使 entrySet 和事件通知工作的问题。似乎除非我强行获取我使用 redis 客户端放置的 key ,否则我不会收到任何 EntryEvent 通知。

最佳答案

我没有收到事件的问题是因为我没有对我感兴趣的键感兴趣。

添加 region.registerInterestRegex(".*"); 允许标记对每个键的兴趣。它需要使用 .setPoolSubscriptionEnabled(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(".*");

// ...

关于java - 如何使用 Redis 适配器写入 Geode,然后使用 Geode 客户端读取/响应事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45420357/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com