In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍"protobuf在java redis中怎么使用",在日常操作中,相信很多人在protobuf在java redis中怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"protobuf在java redis中怎么使用"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
下面首先介绍在idea 中 使用插件 根据.proto 文件生成 .java 文件
1. 首先在此module 中编写一个.proto 文件
2 pom 引用插件
org.xolstice.maven.plugins protobuf-maven-plugin 0.5.0 com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier} grpc-java io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} compile
点击 protobuf:complie 后
在项目target文件下 会生成一个 Java文件
将此文件复制到你项目源码目录即可使用。完毕后 pom 插件 注释掉。防止每次编译都会生成Java文件 导致项目报错。
2 关于spring Boot 如何集成redis 不再赘述 直接上关于RedisSerializer
类编写
我们现在要使用protobuf 作为redis 存储 序列化 工具。
故 需要编写一个RedisSerializer 实现类:
为了通用,故定义成泛型:
@Slf4jpublic class ProtobufRedisSerializer implements RedisSerializer { private MessageLite messageLite; private Class type; public ProtobufRedisSerializer(T messageLite, Class type) { this.messageLite = messageLite; this.type = type; } @Override public byte[] serialize(T t) throws SerializationException { return t.toByteArray(); } @Override public T deserialize(byte[] bytes) throws SerializationException { try { if (Objects.isNull(bytes) || bytes.length == 0) { return null; } MessageLite messageLite = this.messageLite.getParserForType().parseFrom(bytes); return type.cast(messageLite); } catch (InvalidProtocolBufferException e) { log.error("ProtobufRedisSerializer error", e); } return null; }}
核心代码如上:
附上RedisTenplateConfig
@Beanpublic RedisTemplate recordRedisTemplate( RedisConnectionFactory connectionFactory) { RedisTemplate recordRedisTemplate = new RedisTemplate(); recordRedisTemplate.setConnectionFactory(connectionFactory); ProtobufRedisSerializer protobufRedisSerializer = new ProtobufRedisSerializer( DynamicRecord.getDefaultInstance(), DynamicRecord.class); recordRedisTemplate.setValueSerializer(protobufRedisSerializer); recordRedisTemplate.setHashValueSerializer(protobufRedisSerializer); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); recordRedisTemplate.setKeySerializer(stringRedisSerializer); recordRedisTemplate.setHashKeySerializer(stringRedisSerializer); return recordRedisTemplate;}
到此,关于"protobuf在java redis中怎么使用"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.