<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>3.0.3</version>
</dependency>
public class KryoRedisSerializer implements RedisSerializer<Object> {
Kryo kryo = new Kryo();
public KryoRedisSerializer() {
kryo.setInstantiatorStrategy(new SerializingInstantiatorStrategy());
}
@Override
public byte[] serialize(Object object) {
if (object == null) {
return new byte[0];
}
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Output output = new Output(outputStream);
kryo.writeClassAndObject(output, object);
return output.toBytes();
} catch (IOException e) {
throw new SerializationException("Failed Serialization", e);
}
}
@Override
public Object deserialize(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
try (Input input = new Input(bytes)) {
return kryo.readClassAndObject(input);
}
}
}
@Bean
public RedisClusterTemplate<String, ?> redisClusterTemplate() {
RedisClusterTemplate<String, ?> redisTemplate = new RedisClusterTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(stringSerializer());
redisTemplate.setHashKeySerializer(stringSerializer());
redisTemplate.setHashValueSerializer(kryoRedisSerializer());
redisTemplate.setValueSerializer(kryoRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public RedisSerializer<String> stringSerializer() {
return new StringRedisSerializer();
}
@Bean
public RedisSerializer<Object> kryoRedisSerializer() {
return new KryoRedisSerializer();
}
@Test
public void test() {
String key = "11:12";
Mod mod = new Mod();
mod.setA(2);
redisClusterTemplate.opsForValue().set(key, mod);
System.out.println(redisClusterTemplate.opsForValue().get(key));
System.out.println("size : " + redisClusterTemplate.opsForValue().size(key));
}
public class Mod implements Serializable {
private static final long serialVersionUID = 2750816192200570448L;
private Integer a;
public Mod() {
}
public Integer getA() {
return a;
}
public void setA(Integer a) {
this.a = a;
}
}
@Test
public void test() {
String key = "11:12";
// Mod mod = new Mod();
// mod.setA(2);
// redisClusterTemplate.opsForValue().set(key, mod);
System.out.println(redisClusterTemplate.opsForValue().get(key));
System.out.println("size : " + redisClusterTemplate.opsForValue().size(key));
}
public class Mod implements Serializable {
private static final long serialVersionUID = 2750816192200570448L;
private Integer a;
private Integer b;
public Mod() {
}
public Integer getA() {
return a;
}
public void setA(Integer a) {
this.a = a;
}
public Integer getB() {
return b;
}
public void setB(Integer b) {
this.b = b;
}
}
여기에 코드 입com.esotericsoftware.kryo.KryoException: Buffer underflow.Serialization trace:b (us.band.ds.feed.core.commons.nbasearc.NbaseArcTest$Mod)
at com.esotericsoftware.kryo.io.Input.require(Input.java:199) at com.esotericsoftware.kryo.io.Input.readVarInt(Input.java:373) at com.esotericsoftware.kryo.Kryo.readReferenceOrNull(Kryo.java:808) at com.esotericsoftware.kryo.Kryo.readObjectOrNull(Kryo.java:757) at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:132) at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:551) at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:790) at us.band.ds.abuser.core.redis.KryoRedisSerializer.deserialize(KryoRedisSerializer.java:46) at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:315) at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:55) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:191) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153) at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:88) at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43) at us.band.ds.feed.core.commons.nbasearc.NbaseArcTest.test(NbaseArcTest.java:134) 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:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)Please read https://github.com/EsotericSoftware/kryo#compatibility
Cheers,
Martin
--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users
---
You received this message because you are subscribed to the Google Groups "kryo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kryo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
FieldSerializer (during deserialization) reads data from the input according to the current fields of the class (determined via reflection). If the class was changed there's a mismatch between available/serialized data and consumed data.
Cheers,
Martin