通过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模式

相关推荐
2602_9599609236 分钟前
谢飞机面试大厂:Spring Boot、JVM、Redis、Kafka、微服务与音视频搜索场景求生实录
java·jvm·spring boot·redis·面试题
深念Y1 小时前
SSD 寿命洁癖:编译过程不入盘的原则与实践
缓存·io·内存·编译·ssd·读取·写入
智购科技无人售货机厂家2 小时前
2026自动售货机实时库存管理系统:从Redis缓存到持久化的数据架构演进~YH
redis·缓存·架构
晓晓_za8986682 小时前
Geo 优化源码蒸馏词机制:地域词库构建与匹配逻辑详解
运维·tcp/ip·spring·缓存·ci/cd
nvd112 小时前
LiteLLM 如何使用 Redis:从部署位置到精确响应缓存
redis·缓存·gateway
xier_ran2 小时前
【infra之路】GPU 存储层次总结:L1 / L2 / HBM
java·网络·数据库
山甫aa3 小时前
Javaweb---MyBatis增删改查
java·数据库·后端·mybatis·springboot
码哥字节4 小时前
我把Redis双活塞进Spring,业务代码零改动
redis·spring boot ai 编程·factorybean