Redis

关注公众号 jb51net

关闭
首页 > 数据库 > Redis > redis spring boot异常退出

redis在spring boot中异常退出的问题解决方案

作者:简诚

这篇文章主要介绍了redis在spring boot中异常退出的问题解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

问题:

Exception in thread "rtsp-consumer-3" org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1689)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1597)
    at org.springframework.data.redis.connection.lettuce.LettuceConnection.doGetAsyncDedicatedConnection(LettuceConnection.java:1006)
    at org.springframework.data.redis.connection.lettuce.LettuceConnection.getOrCreateDedicatedConnection(LettuceConnection.java:1069)
    at org.springframework.data.redis.connection.lettuce.LettuceConnection.getAsyncDedicatedConnection(LettuceConnection.java:990)
    at org.springframework.data.redis.connection.lettuce.LettuceStreamCommands.getAsyncDedicatedConnection(LettuceStreamCommands.java:395)
    at org.springframework.data.redis.connection.lettuce.LettuceStreamCommands.xReadGroup(LettuceStreamCommands.java:346)
    at org.springframework.data.redis.connection.DefaultedRedisConnection.xReadGroup(DefaultedRedisConnection.java:592)
    at org.springframework.data.redis.core.DefaultStreamOperations$4.inRedis(DefaultStreamOperations.java:310)
    at org.springframework.data.redis.core.DefaultStreamOperations$RecordDeserializingRedisCallback.doInRedis(DefaultStreamOperations.java:387)
    at org.springframework.data.redis.core.DefaultStreamOperations$RecordDeserializingRedisCallback.doInRedis(DefaultStreamOperations.java:382)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:222)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:189)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96)
    at org.springframework.data.redis.core.DefaultStreamOperations.read(DefaultStreamOperations.java:305)
    at com.ruoyi.vedioFrame.utils.RedisStreamOperations.readGroup(RedisStreamOperations.java:70)
    at com.ruoyi.vedioFrame.service.impl.StreamConsumerService.consumeFrames(StreamConsumerService.java:200)
    at com.ruoyi.vedioFrame.service.impl.StreamConsumerService.lambda$null$0(StreamConsumerService.java:108)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
    at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:330)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:216)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.lambda$getConnection$1(StandaloneConnectionProvider.java:115)
    at java.util.Optional.orElseGet(Optional.java:267)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:115)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1595)
    ... 19 more
Caused by: io.netty.channel.AbstractChannel$AnnotatedSocketException: Address already in use: no further information: localhost/127.0.0.1:6379
Caused by: java.net.BindException: Address already in use: no further information
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.lang.Thread.run(Thread.java:745) 

解决

根据代码和现象分析,根本原因可能是处理时间过长导致Redis消息积压触发超时异常。以下是详细分析和解决方案:

🔍 问题根源

同步阻塞模型

Thread.sleep(1000)加剧阻塞

异常未妥善处理

🛠️ 解决方案

1. 异步处理 + 提前ACK(关键步骤)

将消息处理与ACK分离,处理前立即ACK避免消息积压,异步处理业务逻辑:

ExecutorService executor = Executors.newCachedThreadPool(); // 全局线程池
while (!Thread.currentThread().isInterrupted()) {
    List<MapRecord<String, Object, Object>> records = redisStreamOperations.readGroup(...);
    if (records != null) {
        for (MapRecord<String, Object, Object> record : records) {
            // 提前ACK释放消息
            redisStreamOperations.ack(streamKey, groupName, record.getId());
            // 提交异步任务处理业务
            executor.submit(() -> {
                try {
                    String strdata = (String) record.getValue().get("frameData");
                    byte[] frameData = Base64.getDecoder().decode(strdata);
                    BufferedImage image = ImageIO.read(new ByteArrayInputStream(frameData));
                    frameProcessor.process(image, streamId, commonQueues, regions);
                } catch (Exception e) {
                    log.error("处理帧失败: {}", e.getMessage());
                }
            });
        }
    }
    // 移除Thread.sleep(1000) 提升吞吐量
}

2. 调整Redis消费者组参数

增加XGROUP CREATE时的BLOCKIDLE超时:

XGROUP CREATE stream:1234 mygroup $ MKSTREAM BLOCK 5000 IDLE 30000

3. 优化Redis连接配置

确保RedisTemplate的读写超时大于业务处理最长时间:

@Bean
public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory());
    // 设置超时时间(示例:10秒)
    RedisConnectionFactory factory = template.getConnectionFactory();
    if (factory instanceof LettuceConnectionFactory) {
        ((LettuceConnectionFactory) factory).setTimeout(Duration.ofSeconds(10));
    }
    return template;
}

4. 监控Pending队列

定期检查Pending消息数量,异常时重置消费者:

// 在循环中添加检查
Long pending = redisTemplate.opsForStream().pending(streamKey, groupName);
if (pending > 1000) { // 阈值根据业务设定
    redisTemplate.opsForStream().destroyGroup(streamKey, groupName);
    redisTemplate.opsForStream().createGroup(streamKey, groupName);
}

5. 添加熔断机制

当外部接口连续失败时,暂停消费避免雪崩:

// 全局熔断器
CircuitBreaker breaker = CircuitBreaker.ofDefaults("frameProcessor");
// 处理逻辑改为
breaker.executeSupplier(() -> {
    frameProcessor.process(...);
    return null;
});

📈 验证方案

日志监控

// 在异步任务中添加耗时日志
long start = System.currentTimeMillis();
frameProcessor.process(...);
log.info("处理耗时: {}ms", System.currentTimeMillis() - start);

确保单次处理时间低于Redis超时阈值。

Redis CLI检查

XINFO GROUPS stream:1234  # 查看消费者组状态
XPENDING stream:1234 mygroup # 查看Pending消息

🌟 关键优化点总结

措施作用
异步处理 + 提前ACK彻底解耦处理耗时与Redis消费
增大Redis超时参数避免服务端主动断开连接
线程池隔离防止外部接口故障拖垮整个消费服务
熔断机制快速失败,保护系统稳定性

通过以上改造,即使frameProcessor.process()调用外部平台接口,也能通过异步化和资源隔离保证Redis消费链路的稳定性。最终只需确保线程池和熔断参数适配业务负载即可。

代码具体添加位置:

以下是针对 优化Redis连接配置监控Pending队列添加熔断机制 的代码具体添加位置的详细说明:

一、优化Redis连接配置

位置:Redis配置类
在Spring Boot的Redis配置类中调整超时参数(通常为RedisConfig.java):

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        // 设置连接超时和读写超时(关键参数)
        lettuceConnectionFactory.setTimeout(Duration.ofSeconds(10));  // 命令超时时间
        lettuceConnectionFactory.setShareNativeConnection(false);    // 禁用共享连接,避免阻塞
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

关键参数说明

二、监控Pending队列

位置:consumeFrames方法内的循环中
在消费消息的主循环中定期检查Pending队列:

private void consumeFrames(String streamId, String groupName, String consumerName,
                           CommonQueues commonQueues, String regions) throws InterruptedException, IOException {
    // ... 其他初始化代码 ...
    int checkPendingInterval = 10; // 每处理10次循环检查一次Pending队列
    int loopCount = 0;
    while (!Thread.currentThread().isInterrupted()) {
        // ... 原有代码读取消息 ...
        // 监控Pending队列的逻辑(添加位置)
        loopCount++;
        if (loopCount % checkPendingInterval == 0) {
            String streamKey = "stream:" + streamId;
            PendingMessages pending = redisStreamOperations.pending(streamKey, groupName);
            if (pending != null && pending.getTotalPendingMessages() > 1000) { // 阈值根据业务调整
                log.warn("检测到Pending消息积压 {} 条,重置消费者组", pending.getTotalPendingMessages());
                redisStreamOperations.destroyGroup(streamKey, groupName);
                redisStreamOperations.createGroup(StreamKey.of(streamKey), groupName);
            }
        }
        // ... 后续处理代码 ...
    }
}

说明

三、添加熔断机制

位置:处理消息的业务逻辑外层
使用Resilience4j熔断器包裹frameProcessor.process()调用:

1. 熔断器配置类

@Configuration
public class CircuitBreakerConfig {
    @Bean
    public CircuitBreaker frameProcessorCircuitBreaker() {
        CircuitBreakerConfig config = CircuitBreakerConfig.custom()
            .failureRateThreshold(50)          // 失败率阈值50%
            .slidingWindowType(SlidingWindowType.COUNT_BASED)
            .slidingWindowSize(10)             // 基于最近10次调用统计
            .minimumNumberOfCalls(5)           // 最少5次调用后开始计算
            .waitDurationInOpenState(Duration.ofSeconds(30)) // 熔断后30秒进入半开状态
            .build();
        return CircuitBreakerRegistry.of(config).circuitBreaker("frameProcessor");
    }
}

2. 在消费代码中使用熔断器

public class YourConsumerClass {
    @Autowired
    private CircuitBreaker frameProcessorCircuitBreaker; // 注入熔断器
    private void consumeFrames(...) {
        // ... 原有代码 ...
        for (MapRecord<String, Object, Object> record : records) {
            redisStreamOperations.ack(...); // 提前ACK
            // 使用熔断器保护处理逻辑(添加位置)
            Try.runRunnable(() -> frameProcessorCircuitBreaker.executeRunnable(() -> {
                String strdata = (String) record.getValue().get("frameData");
                byte[] frameData = Base64.getDecoder().decode(strdata);
                BufferedImage image = ImageIO.read(new ByteArrayInputStream(frameData));
                frameProcessor.process(image, streamId, commonQueues, regions);
            })).onFailure(e -> log.error("处理失败且熔断: {}", e.getMessage()));
        }
        // ... 后续代码 ...
    }
}

熔断逻辑说明

四、代码集成位置总结

优化措施代码位置关键注解
Redis连接配置Redis配置类(如RedisConfig.java调整超时时间和连接池参数
Pending队列监控consumeFrames方法的主循环内定期检查+自动重置消费者组
熔断机制业务处理代码外层(包裹frameProcessor.process依赖熔断器库(如Resilience4j)

五、参数调整建议

Redis超时

Pending队列阈值

熔断器参数

通过以上改造,即使frameProcessor.process()调用外部平台接口,也能通过资源隔离、快速失败和自动恢复机制保障Redis消费链路的稳定性。

到此这篇关于redis在spring boot中异常退出的文章就介绍到这了,更多相关redis spring boot异常退出内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文