当前位置: 首页 > wzjs >正文

重庆建设工程交易信息网站上海工商网上办事大厅电话

重庆建设工程交易信息网站,上海工商网上办事大厅电话,施工企业安全生产考核评定等级分为,企业网站建设验收一、Lettuce核心优势与Spring Boot集成背景 Lettuce特性 基于Netty的非阻塞I/O模型,支持同步/异步/响应式编程线程安全:共享单连接实现多线程并发操作,性能衰减低原生支持Redis集群、哨兵、主从架构,自动重连机制保障高可用Spring…

一、Lettuce核心优势与Spring Boot集成背景

  1. Lettuce特性

    • 基于Netty的非阻塞I/O模型,支持同步/异步/响应式编程
    • 线程安全:共享单连接实现多线程并发操作,性能衰减低
    • 原生支持Redis集群、哨兵、主从架构,自动重连机制保障高可用
    • Spring Boot 2.x默认Redis客户端,替代Jedis
  2. 适用场景对比

    场景Lettuce适用性Jedis适用性
    高并发低延迟✅ 异步非阻塞,性能更优❌ 同步阻塞,连接池压力大
    分布式锁/数据结构需结合Redisson原生不支持
    简单CRUD操作✅ 内置序列化支持✅ 轻量易用

二、Spring Boot集成Lettuce全流程

1. 依赖配置
<!-- Spring Boot默认集成Lettuce -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 连接池支持(可选) -->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId>
</dependency>
2. 基础配置(application.yml)
spring:redis:host: 127.0.0.1port: 6379password: yourpasswordlettuce:pool:max-active: 20    # 最大连接数(高并发场景建议50+)max-idle: 10      # 最大空闲连接min-idle: 5       # 最小空闲连接(防突发流量)max-wait: 100ms   # 获取连接最大等待时间
3. 序列化定制(核心配置类)
@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// Key序列化template.setKeySerializer(new StringRedisSerializer());  // Value序列化为JSONtemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());  return template;}
}

序列化方案对比

  • StringRedisSerializer:纯字符串操作
  • Jackson2JsonRedisSerializer:对象序列化(需无参构造)
  • JdkSerializationRedisSerializer:二进制存储(兼容性差)

三、高级功能实现

1. 哨兵/集群模式
# 哨兵模式配置
spring:redis:sentinel:master: mymaster     # 主节点名称nodes: 192.168.1.1:26379,192.168.1.2:26379lettuce:pool: max-active: 30    # 集群需增大连接池
2. 读写分离(主从架构)

通过自定义LettuceClientConfigurationBuilder实现:

@Bean
public LettuceConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("master-host", 6379);LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().readFrom(ReadFrom.REPLICA_PREFERRED)  // 优先从副本读取.build();return new LettuceConnectionFactory(config, clientConfig);
}
3. 响应式编程(WebFlux集成)
@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {return new ReactiveRedisTemplate<>(factory, RedisSerializationContext.string());
}

响应式操作示例:

reactiveTemplate.opsForValue().set("key", "value").subscribe();  // 非阻塞写入

四、性能优化策略

  1. 连接池调优

    • max-active:根据QPS计算(公式:QPS * avg_time)
    • min-idle:保持预热连接,应对突发流量
    • 禁用连接池:短连接场景可设置spring.redis.lettuce.pool.enabled=false
  2. 序列化优化

    • 避免JDK序列化(体积大且跨语言差),优先使用JSON或Protobuf
    • 大Value场景启用压缩:
      template.setValueSerializer(new CompressionRedisSerializer(new Jackson2JsonRedisSerializer<>(Object.class)));
      
  3. 批处理与Pipeline

    List<Object> results = redisTemplate.executePipelined((RedisCallback<Object>) connection -> {for (int i =0; i <1000; i++) {connection.stringCommands().set(("key"+i).getBytes(), value.getBytes());}return null;
    });
    

五、常见问题排查

  1. 连接泄漏

    • 现象:RedisConnectionFailureException: Cannot get Jedis connection
    • 解决:检查finally块是否关闭连接,或使用execute(SessionCallback)自动释放
  2. 序列化异常

    • 现象:SerializationException: Could not read JSON
    • 解决:确保实体类有无参构造器,或切换为GenericJackson2JsonRedisSerializer
  3. 高延迟

    • 启用慢查询日志:
      redis-cli config set slowlog-log-slower-than 5000  # 记录>5ms操作
      redis-cli slowlog get                              # 查看日志
      

六、扩展场景实践

  1. 分布式锁(Redisson整合)

    @Bean
    public RedissonClient redissonClient() {Config config = new Config();config.useSingleServer().setAddress("redis://127.0.0.1:6379");return Redisson.create(config);
    }
    // 使用示例
    RLock lock = redissonClient.getLock("myLock");
    lock.lock();
    try { /* 业务逻辑 */ } finally { lock.unlock(); }
    
  2. 多数据源配置
    创建多个RedisTemplate实例并指定不同连接工厂:

    @Bean(name = "secondaryTemplate")
    public RedisTemplate<String, Object> secondaryTemplate(@Qualifier("secondaryFactory") RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);return template;
    }
    

最佳实践总结

  1. 生产环境必配连接池(防连接风暴)
  2. ETL类作业启用Pipeline(提升吞吐量3-5倍)
  3. 监控指标:关注redis.commands的P99延迟与连接池活跃数
  4. 版本升级:Spring Boot 3.x需配合Lettuce 6.x+,支持RESP3协议

参考配置

代码示例与完整配置可参考:示例项目

Lettuce使用详解


在这里插入图片描述


文章转载自:

http://n6Cmrtoa.Lwswm.cn
http://FaZFjvmS.Lwswm.cn
http://Dydt2jAj.Lwswm.cn
http://Z5KHKBD1.Lwswm.cn
http://4vZURZ6f.Lwswm.cn
http://BwWmiJRC.Lwswm.cn
http://6EHtA3j9.Lwswm.cn
http://6lZTgP8u.Lwswm.cn
http://WczGdDwI.Lwswm.cn
http://4pMiJKVq.Lwswm.cn
http://T17OdlQ2.Lwswm.cn
http://CIvVkxqL.Lwswm.cn
http://XbkkAG8S.Lwswm.cn
http://DtlKGolf.Lwswm.cn
http://bvxLesf6.Lwswm.cn
http://D9EFgpNt.Lwswm.cn
http://PBbTX3gU.Lwswm.cn
http://oLRmE9Kv.Lwswm.cn
http://vpBDWxiK.Lwswm.cn
http://nDkxVFLQ.Lwswm.cn
http://rd1CSW91.Lwswm.cn
http://CywQpKLq.Lwswm.cn
http://s5JLr8NR.Lwswm.cn
http://iyQtUuRh.Lwswm.cn
http://gwlMJ5QE.Lwswm.cn
http://loIN81C8.Lwswm.cn
http://3xrJP6Yn.Lwswm.cn
http://a66fKmYo.Lwswm.cn
http://YcCMPlXo.Lwswm.cn
http://EhGcsljR.Lwswm.cn
http://www.dtcms.com/wzjs/723259.html

相关文章:

  • wordpress下载链接百度网站排名优化
  • 晋江网站建设报价可视化手机网站开发工具
  • 成都网站中国机械外协加工网
  • 上海松江品划建设网站住房城乡建设部网站通报
  • 网站建设 企业观点html底部的版权代码
  • 如果使用自己电脑做网站提供网站建设方案ppt
  • 建设网站用新域名还是老域名新网站建设的感想
  • 做微商网站设计网站做营销推广
  • 网页超链接怎么做步骤兰州网站优化推广
  • 做设计接单的网站运营推广计划表
  • wordpress 多站点 404网站建设与维护是什么意思
  • 电商网站里的图片php玩具公司网站源码
  • i5 7500网站开发信息作业网站下载
  • 定制网站建设制作个人优秀网站欣赏
  • 网店装修的主要内容有哪些深圳seo整站优化承接
  • 亚马逊网站建设案例分析网站开发语言开发
  • 备案过的网站换空间公司加盟
  • 帮做网站制作挣钱建网站问题
  • 网站建设蛋蛋28洛阳市有哪些平台公司
  • 做网站开发 用什么软件为什么外包会是简历污点
  • 大足集团网站建设千阳县住房和城乡建设局网站
  • 建设网站南沙区iis 网站压缩
  • 建设电玩网站四川seo整站优化费用
  • 唐山市城乡建设网站网站的市场如何制作
  • 装修网站横幅怎么做东软实训网站开发
  • 网站空间就是主机吗企业网站 html模板下载
  • 详情页制作网站网页游戏在线玩不用登录
  • 网站建设与网页设计论述题淮北网站建设公司
  • 网站建设怎么加音乐网站开发技术简介
  • 网站实名制查询sql数据库的网站迁移