복붙노트

[SPRING] spring-data-redis redisTemplate 예외

SPRING

spring-data-redis redisTemplate 예외

get () 메서드를 호출 할 때 예외가 발생했습니다.

여기에 코드가있다.

@Service("RedisService")
public class RedisServiceImpl implements RedisService {

@Autowired
RedisTemplate<String, Long> redisTemplate;

@Override
public Long get(String key) {
    return redisTemplate.opsForValue().get(key);
}

@Override
public Long incrBy(String key, long increment) {
    return redisTemplate.opsForValue().increment(key, increment);
}

incrBy 메서드를 사용할 때 예외는 없지만 오류 만 메서드를 가져옵니다. stacktrace가 있습니다 ---

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
    at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:38)
    at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:58)
    at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:1)
    at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:40)
    at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:198)
    at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:50)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:162)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:133)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
    at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:42)
    at net.daum.air21.bot.common.service.RedisServiceImpl.get(RedisServiceImpl.java:29)
    at net.daum.air21.bot.user.service.SeraCoffeeServiceImpl.getCurrentCount(SeraCoffeeServiceImpl.java:41)

해결법

  1. ==============================

    1.기본적으로 RedisTemplate은 JdkSerializationRedisSerializer를 사용하므로 "설정"을 수행하면 Redis에서 다음과 같이 긴 모양을 만듭니다.

    기본적으로 RedisTemplate은 JdkSerializationRedisSerializer를 사용하므로 "설정"을 수행하면 Redis에서 다음과 같이 긴 모양을 만듭니다.

    "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x00\x00\x00\x00\x04"
    

    Redis는 항상 해당 작업에서 Long을 반환하므로 IncrBy가 작동하므로 RedisTemplate은 결과를 deserialize하지 않습니다. 그러나 "get"의 결과는 위와 같은 형식을 예상하는 역 직렬화 과정을 거칩니다.

    RedisTemplate에서 다른 값 직렬기를 사용하여이 문제를 해결할 수 있습니다.

    redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
    

    또는 spring-data-redis와 함께 제공되는 RedisAtomicLong 클래스를 사용해보십시오.

  2. ==============================

    2.비트 좌절 - RedisAtomicLong에 대한 팁 주셔서 감사합니다 ..하지만 여기에 문자열 키, 문자열 필드 및 긴 값으로 HashOps를 사용하기위한 솔루션입니다 - 용이 한 구성을 위해 Spring Boot를 사용합니다

    비트 좌절 - RedisAtomicLong에 대한 팁 주셔서 감사합니다 ..하지만 여기에 문자열 키, 문자열 필드 및 긴 값으로 HashOps를 사용하기위한 솔루션입니다 - 용이 한 구성을 위해 Spring Boot를 사용합니다

    @SpringBootApplication
    @Configuration
    public class DemoApplication {
    
        public static void main(String[] args) {
            final ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
            final DacRepository dacRepository  = context.getBean(DacRepository.class);
            dacRepository.incrKeyExample();
        }
    
    
        @Bean
        public RedisTemplate<String, Long> getLongRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
             final RedisTemplate<String,Long> redisTemplate = new RedisTemplate<String, Long>();
             redisTemplate.setConnectionFactory(redisConnectionFactory);
             redisTemplate.setHashValueSerializer(new GenericToStringSerializer<Long>(Long.class));
             redisTemplate.setKeySerializer(new StringRedisSerializer());
             redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
             redisTemplate.setHashKeySerializer(new StringRedisSerializer());
             redisTemplate.afterPropertiesSet();
             return redisTemplate;
        }
    }
    
    import java.util.Set;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.HashOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Repository;
    
    @Repository
    
    public class DacRepository {
    
        private final HashOperations<String, String, Long> hashOps;
    
        @Autowired
        private final RedisTemplate<String, Long> redisTemplate;
    
        @Autowired
        public DacRepository(RedisTemplate<String, Long> redisTemplate) {
            this.redisTemplate = redisTemplate;
            hashOps = redisTemplate.opsForHash();
    
        }
    
    
        public void incrKeyExample() {
            final Set<String> keys = this.redisTemplate.keys("*");
            for(String key: keys) {
                System.out.println("key: "+ key);
            }
    
            final String key = "deal-1";
            final String field = "view";
            final Long value = 1L;
            hashOps.put(key, field, value);
            final Long delta = 1L;
            hashOps.increment(key, field, delta);
            Long val = hashOps.get("deal-1", "view");
            System.out.println("Value = "+val);
        }
    
    }
    
    
    
    
    server.port=9001
    spring.redis.host=localhost
    spring.redis.port=6379
    spring.redis.pool.max-idle=8
    spring.redis.pool.min-idle=0
    spring.redis.pool.max-active=8
    spring.redis.pool.max-wait=-1
    
  3. from https://stackoverflow.com/questions/17162725/spring-data-redis-redistemplate-exception by cc-by-sa and MIT license