我在我的 Camel 应用程序中使用 Redis 组件。一个问题是它会自动将字符串添加到键中。例如,假设我在我的 Camel 应用程序中运行以下命令:
from("direct://path/to/store/in/redis")
.setHeader(RedisConstants.COMMAND, constant("SET"))
.setHeader(RedisConstants.KEY, constant("key"))
.setHeader(RedisConstants.VALUE, constant("value"))
.to(spring-redis://localhost:6379);
然后,如果我打开我的命令行 Redis 客户端并运行以下命令以列出数据库中的所有键:
> keys *
它返回:
1) "\xac\xed\x00\x05t\x00\x03key"
在这里您可以看到它在 key 前面添加了 \xac\xed\x00\x05t\x00\x03,我不确定它到底在哪里做的。
如果我只使用 GET 和 SET Redis 命令,这不会成为问题,因为出于某种原因,它会在这些命令的键前添加相同的字符串,因此不会出现键不匹配。但是,如果我尝试通过 Camel 应用程序执行不同的 Redis 命令,例如 KEYS,如下所示:
from("direct://some/other/path/to/redis")
.setHeader(RedisConstants.COMMAND, constant("KEYS"))
.setHeader(RedisConstants.PATTERN, constant("*"))
.to(spring-redis://localhost:6379);
它在星号前面添加了一个略有不同的字符串,这导致查询不返回任何内容,因为没有与该模式的匹配项。也就是说,
> KEYS *
命令在 Redis 中转换为如下内容:
> KEYS "\xac\xed\x00\x05t\x00\x05t*"
对此有什么想法吗?
最佳答案
以下两篇文章帮助我解决了这个问题:
Redis serialization prefixed with extra string
Weird redis key with spring data Jedis
所以我通过将 RedisTemplate 中的 DefaultRedisSerializer 设置为 StringRedisSerializer 来修复它。
因为我使用 Guice 进行依赖项/bean 注入(inject),所以我将以下内容添加到我的 GuiceCamelModule:
public class GuiceCamelTestModule extends CamelModuleWithMatchingRoutes {
...
@Provides
@JndiBind("redisTemplateBean")
Object provideRedisTemplateBean() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.afterPropertiesSet();
RedisTemplate<?, ?> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
template.setDefaultSerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
我的路由 URI 如下所示:
"spring-redis://localhost:6379?redisTemplate=#redisTemplateBean"
关于java - Camel Redis 自动将字符串添加到键中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402717/