【问题标题】:Spring-data-redis ping works, key is there, no data returnedspring-data-redis ping 有效,key 有,没有返回数据
【发布时间】:2016-06-17 10:02:08
【问题描述】:

这是我第一个使用 spring-data-redis 的应用程序,我认为我很好地理解了这些概念(过去我曾多次使用 JdbcTemplate 和 RDBMS-es)。这就是正在发生的事情......

我已经使用 JedisConnectionFactory 设置了 RedisTemplate,并且能够成功 ping Redis 服务器。但是,我无法从服务器获得最简单的数据响应,而且恐怕我错过了一些基本的东西,到目前为止我还无法从文档中推测出来。

这是我的 bean.xml 文件的 Redis 部分:

<!-- Redis DAO stuff -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.url}" p:port="${redis.port}" p:database="0" />

这是我的 RedisDAO 类中的相关代码部分:

@Autowired
private RedisTemplate<String, Object> template;

public String getTestVal() {
    logger.debug("getTestVal() function called++++++++++++++++++++");
    template.getConnectionFactory().getConnection().select(0);
    logger.debug("22222222222222");
    String pingResult = template.getConnectionFactory().getConnection().ping();
    logger.debug("333333333333333");
    logger.debug("REDIS PING RESULT: " + pingResult);
    logger.debug("444444444444444");
    logger.debug("HasKey Result: " + template.hasKey("akey"));
    logger.debug("555555555555555");
    //NAC;P3_TZ_380002878
    Object testVal = template.opsForValue().get("akey");
    logger.debug("TestVal returned from REdis: " + testVal);
    return null;
}

这里是日志文件的相关输出:

13:48:21.505 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - getTestVal() function called++++++++++++++++++++
13:48:21.678 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - 22222222222222
13:48:21.801 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - 333333333333333
13:48:21.801 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - REDIS PING RESULT: PONG
13:48:21.801 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - 444444444444444
13:48:21.808 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:48:21.936 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:48:21.937 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - HasKey Result: false
13:48:21.937 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - 555555555555555
13:48:21.940 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:48:22.009 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:48:22.009 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - TestVal returned from REdis: null

注意从 Redis 返回的 TestVal 是 null,但是如果我对该服务器的 0(零)数据库使用 redis-cli,我会得到以下响应:

127.0.0.1:6379> get akey
"yo mama"

“哟妈妈”是我期待的价值。


@mp911de 我在这里回复是因为评论回复不够长,无法显示详细信息。 当我将代码更改为以下(并且没有其他更改)时,恐怕我仍然遇到同样的问题......
public String getTestVal() {
    template.setDefaultSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    logger.debug("getTestVal() function called++++++++++++++++++++");
    template.getConnectionFactory().getConnection().select(0);
    logger.debug("22222222222222");
    String pingResult =  template.getConnectionFactory().getConnection().ping();
    logger.debug("333333333333333");
    logger.debug("REDIS PING RESULT: " + pingResult);
    logger.debug("444444444444444");
    logger.debug("HasKey Result: " + template.hasKey("akey"));
    logger.debug("555555555555555");
    Object testVal = template.opsForValue().get("akey");
    logger.debug("TestVal returned from REdis: " + testVal);

    return null;
}

注意:添加 StringRedisSerializer 作为 RedisTemplate 的默认序列化器。感谢您的努力。

【问题讨论】:

    标签: spring redis spring-data


    【解决方案1】:

    RedisOperations 使用 serializerstranslate Java objects 到 Redis 数据结构值中。序列化程序默认为JdkSerializationRedisSerializer。 JDK 序列化程序将您的 String 对象转换为与 ASCII 或 UTF-8 不兼容的 Java 序列化表示。查看文档,您可能对StringRedisSerializer 感兴趣。序列化器需要在RedisTemplate中设置:

    @Bean
    RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
    
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
    
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
    

    【讨论】:

    • 除了@mp911de 的帮助之外,我还发现了这个question/answer 也很有帮助。关键变化似乎是在 xml bean 配置文件中设置序列化程序。在那之后就像一个魅力。
    【解决方案2】:

    是的,你需要一个 redisTemplate 的序列化器来返回数据。有了 Jedis,您就不需要它了。

    @Bean(name = "redisTemplate")
    public RedisTemplate<String, String> redisTemplate() {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 2023-04-05
      • 1970-01-01
      • 2018-10-25
      • 2017-01-05
      • 1970-01-01
      • 2017-01-01
      • 2022-06-21
      • 2016-11-17
      相关资源
      最近更新 更多