Java Camel Redis自动将字符串前置到键
我正在骆驼应用程序中使用Redis组件。一个问题是它会自动将字符串前置到键。例如,假设我在我的骆驼应用程序中运行以下命令:Java Camel Redis自动将字符串前置到键,java,redis,apache-camel,Java,Redis,Apache Camel,我正在骆驼应用程序中使用Redis组件。一个问题是它会自动将字符串前置到键。例如,假设我在我的骆驼应用程序中运行以下命令: from("direct://path/to/store/in/redis") .setHeader(RedisConstants.COMMAND, constant("SET")) .setHeader(RedisConstants.KEY, constant("key")) .set
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"
在这里,您可以看到它在键前面加了\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*"
对此有何想法?以下两篇文章帮助我解决了这个问题: 因此,我通过将RedisTemplate中的DefaultRedisSerializer设置为StringRedisSerializer来修复它 由于我使用Guice进行依赖项/bean注入,因此我将以下内容添加到我的GuiceCamelModule中:
如果您单独使用骆驼组件(即没有绝地武士),请使用以下代码 pom.xml
org.apache.camel.springboot
骆驼弹簧redis起动器
org.springframework.boot
spring启动程序数据redis
RedisConfiguration.java
import org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.data.redis.connection.莴苣.LettuceConnectionFactory;
导入org.springframework.data.redis.core.RedisTemplate;
导入org.springframework.data.redis.serializer.jackson2json重新序列化器;
导入org.springframework.data.redis.serializer.StringRedisSerializer;
@配置
公共类配置{
@Bean(“redisTemplate”)
公共RedisTemplate RedisTemplate(LettuceConnectionFactory connectionFactory){
RedisTemplate=新RedisTemplate();
模板.setConnectionFactory(connectionFactory);
setKeySerializer(新的StringRedisSerializer());
setHashKeySerializer(新的StringRedisSerializer());
setHashValueSerializer(新的Jackson2JsonRedisSerializer(Object.class));
返回模板;
}
}
Route.java
.setHeader(RedisConstants.COMMAND,常量(“SETEX”))
.setHeader(RedisConstants.KEY,简单(${body.parentId}”))
.setHeader(RedisConstants.VALUE,简单(“${body}”))
.setHeader(RedisConstants.TIMEOUT,常量(100))
.to(“spring redis://”+redisHost+:“+redisPort+”?redisTemplate=#redisTemplate”);
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;
}
}
"spring-redis://localhost:6379?redisTemplate=#redisTemplateBean"