Redisson编码器修改以及统一设置Key的前缀

2024-08-08 13:35:37 浏览数 (2)

Redisson集成进入到项目后,默认使用的是

<code>org.redisson.codec.MarshallingCodec</code>编码器,此编码器跟操作系统的编码有关,会导致不同的服务器存储的内容编码不一致,直接报错,因此需要选择其他的编码器.Redisson默认的编码器如下:

参考官方文档 https://github.com/redisson/redisson/wiki/4.-Data-serialization

Codec class name

Description

org.redisson.codec.Kryo5Codec

Kryo 5 binary codec(Android compatible) Default codec

org.redisson.codec.KryoCodec

Kryo 4 binary codec

org.redisson.codec.JsonJacksonCodec

Jackson JSON codec.Stores type information in @class field (Android compatible)

org.redisson.codec.TypedJsonJacksonCodec

Jackson JSON codec which doesn't store type id (@class field) during encoding and doesn't require it for decoding

org.redisson.codec.AvroJacksonCodec

Avro binary json codec

org.redisson.codec.ProtobufCodec

Protobuf codec

org.redisson.codec.SmileJacksonCodec

Smile binary json codec

org.redisson.codec.CborJacksonCodec

CBOR binary json codec

org.redisson.codec.MsgPackJacksonCodec

MsgPack binary json codec

org.redisson.codec.IonJacksonCodec

Amazon Ion codec

org.redisson.codec.SerializationCodec

JDK Serialization binary codec (Android compatible)

org.redisson.codec.LZ4Codec

LZ4 compression codec.Uses Kryo5Codec for serialization by default

org.redisson.codec.LZ4CodecV2

LZ4 Apache Commons compression codec.

Uses Kryo5Codec for serialization by default |

| org.redisson.codec.SnappyCodecV2 | Snappy compression codec based on snappy-java project.

Uses Kryo5Codec for serialization by default |

| org.redisson.codec.MarshallingCodec | JBoss Marshalling binary codec

Deprecated! |

| org.redisson.client.codec.StringCodec | String codec |

| org.redisson.client.codec.LongCodec | Long codec |

| org.redisson.client.codec.ByteArrayCodec | Byte array codec |

| org.redisson.codec.CompositeCodec | Allows to mix different codecs as one |

项目中,采用的是redisson-spring-boot-starter集成,也引入了spring-data-redis,参考官方配置https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter

本项目为了不打破原有的redis配置,并未采用spring.redis.redisson这种配置,而是手动增加了redisson中提供的额外配置接口,实现此接口并注册成为bean后,可以直接设置额外的redisson配置

代码语言:java复制
import org.redisson.config.Config;
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
import org.springframework.stereotype.Component;

@Component
public class YourRedissonAutoConfigurationCustomizer implements RedissonAutoConfigurationCustomizer {

	@Override
	public void customize(Config cfg) {
		//cfg.setCodec(new org.redisson.codec.FstCodec());
		cfg.setCodec(new org.redisson.codec.SerializationCodec());
	}

}

包括NameMapper也可以通过此方法来配置。原理可以查看<code>RedissonAutoConfiguration</code>实现代码

0 人点赞