通过Lettuce实现PB3格式对象在Redis中的存储与查询

1. Java实现代码(Lettuce客户端)

java 复制代码
import io.lettuce.core.RedisURI;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.sync.RedisCommands;
import com.google.protobuf.InvalidProtocolBufferException;
import your.package.UserPB; // 替换为您的PB3生成类

public class RedisStorage {
    private static final String HOST = "your-redis-host";
    private static final int PORT = 6379;
    private static final String PASSWORD = "your-password";
    private static final int DATABASE = 0;

    public static void main(String[] args) throws InvalidProtocolBufferException {
        // 创建Redis连接
        RedisURI uri = RedisURI.builder()
                .withHost(HOST)
                .withPort(PORT)
                .withPassword(PASSWORD.toCharArray())
                .withDatabase(DATABASE)
                .build();

        RedisClient client = RedisClient.create(uri);
        StatefulRedisConnection<String, byte[]> connection = client.connect(new PB3RedisCodec());
        RedisCommands<String, byte[]> sync = connection.sync();
            
            // 创建PB3对象
            UserPB.User user = UserPB.User.newBuilder()
                    .setId(1)
                    .setName("xxx")
                    .setEmail("xxx@example.com")
                    .build();
            
            // 序列化存储
            byte[] userBytes = user.toByteArray();
            sync.set("user:1", userBytes);
            
            // 查询反序列化
            byte[] storedBytes = sync.get("user:1");
            UserPB.User storedUser = UserPB.User.parseFrom(storedBytes);
            
            System.out.println("Retrieved User: " + storedUser);
					
					connection.close();
        
    }
}
  • 自定义 RedisCodec
java 复制代码
import io.lettuce.core.codec.RedisCodec;

import java.nio.ByteBuffer;

public class PB3RedisCodec implements RedisCodec<String, byte[]> {
    @Override
    public String decodeKey(ByteBuffer bytes) {
        return bytes.toString();
    }

    @Override
    public byte[] decodeValue(ByteBuffer bytes) {
        return toByteArray(bytes);
    }

    @Override
    public ByteBuffer encodeKey(String key) {
        return ByteBuffer.wrap(key.getBytes());
    }

    @Override
    public ByteBuffer encodeValue(byte[] value) {
        return ByteBuffer.wrap(value);
    }

    public static byte[] toByteArray(ByteBuffer buffer) {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        buffer.rewind(); // 恢复原始状态
        return bytes;
    }
}
  • 使用连接池配置
java 复制代码
GenericObjectPool<StatefulRedisConnection<String, byte[]>> pool = 
    ConnectionPoolSupport.createGenericPool(
        () -> client.connect(new PB3RedisCodec()), 
        new GenericObjectPoolConfig()
);

2. 关键配置说明

  1. PB3序列化

    java 复制代码
    // 序列化
    byte[] data = protoObject.toByteArray();
    
    // 反序列化
    ProtoClass obj = ProtoClass.parseFrom(data);
  2. Lettuce连接池优化

    java 复制代码
    // 连接池配置
    RedisURI uri = RedisURI.builder()
            .withHost("host")
            .withPassword("pass".toCharArray())
            .withTimeout(Duration.ofSeconds(10))
            .build();
    
    RedisClient client = RedisClient.create(uri);
    StatefulRedisConnection<String, byte[]> connection = client.connect();

3. 替代方案(Python实现)

若环境限制Java使用,可使用Python方案:

python 复制代码
import redis
from user_pb2 import User  # 需先编译.proto文件

# 连接Redis Labs
r = redis.Redis(
    host='your-redis-host',
    port=6379,
    password='your-password',
    decode_responses=False
)

# 序列化存储
user = User(id=1, name="John Doe", email="john.doe@example.com")
r.set("user:1", user.SerializeToString())

# 查询反序列化
stored_bytes = r.get("user:1")
stored_user = User.FromString(stored_bytes)
print(f"Retrieved User: {stored_user}")

4. 常见问题处理

  1. 依赖安装

    bash 复制代码
    # Java项目
    <dependency>
        <groupId>io.lettuce</groupId>
        <artifactId>lettuce-core</artifactId>
        <version>6.3.0.RELEASE</version>
    </dependency>
    
    # Python项目
    pip install redis protobuf
  2. 连接问题排查

    • 检查防火墙设置(开放6379端口)
    • 验证密码正确性
    • 测试网络连通性:telnet host 6379
  3. 性能优化

    • 使用连接池(Lettuce自动管理)
    • 启用压缩选项(PB3支持)
    • 设置合理超时时间(建议10-30秒)

注意:生产环境建议:

  • 使用SSL/TLS加密连接
  • 配置Redis访问白名单
  • 启用持久化存储(RDB/AOF)
  • 监控内存使用情况(免费套餐有30MB限制)

netty中实现Protobuf协议编解码与传输
Protobuf序列化机制的核心原理
lettuce支持的redis模式

相关推荐
llilian_1629 分钟前
IRIG-B码产生器立足用户痛点,提供精准授时解决方案
大数据·数据库·功能测试·单片机·嵌入式硬件·测试工具
zuoerjinshu6 小时前
sql实战解析-sum()over(partition by xx order by xx)
数据库·sql
NocoBase7 小时前
【2.0 教程】第 1 章:认识 NocoBase ,5 分钟跑起来
数据库·人工智能·开源·github·无代码
Hoshino.418 小时前
基于Linux中的数据库操作——下载与安装(1)
linux·运维·数据库
Oueii10 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
未来龙皇小蓝10 小时前
【MySQL-索引调优】11:Group by相关概念
数据库·mysql·性能优化
2401_8318249611 小时前
使用Fabric自动化你的部署流程
jvm·数据库·python
njidf11 小时前
Python日志记录(Logging)最佳实践
jvm·数据库·python
twc82911 小时前
大模型生成 QA Pairs 提升 RAG 应用测试效率的实践
服务器·数据库·人工智能·windows·rag·大模型测试
@我漫长的孤独流浪11 小时前
Python编程核心知识点速览
开发语言·数据库·python