Просмотр исходного кода

fix lettuce客户端心跳配置

skyline 2 лет назад
Родитель
Сommit
9806c6565b

+ 2 - 0
admin/src/main/resources/application-dev.yml

@@ -79,6 +79,8 @@ spring:
       password: 123456
       database: 10
       lettuce:
+        pool:
+          min-idle: 1
         cluster:
           refresh:
             adaptive: true

+ 2 - 0
admin/src/main/resources/application-prod.yml

@@ -79,6 +79,8 @@ spring:
       password: OPYCJ3mJpaU4IsP7ZeIyaus6FtHjLYmhNDGteuRc1gIFCAm7wi
       database: 10
       lettuce:
+        pool:
+          min-idle: 1
         cluster:
           refresh:
             adaptive: true

+ 50 - 0
common/src/main/java/com/kym/common/config/LettuceClientConfig.java

@@ -0,0 +1,50 @@
+package com.kym.common.config;
+
+import io.lettuce.core.resource.ClientResources;
+import io.lettuce.core.resource.NettyCustomizer;
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelDuplexHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.timeout.IdleStateEvent;
+import io.netty.handler.timeout.IdleStateHandler;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author skyline
+ * @description redis lettuce心跳配置
+ * @date 2023-09-14 13:28
+ */
+@Configuration
+public class LettuceClientConfig {
+    @Bean
+    public ClientResources clientResources() {
+
+        NettyCustomizer nettyCustomizer = new NettyCustomizer() {
+
+            @Override
+            public void afterChannelInitialized(Channel channel) {
+                channel.pipeline().addLast(
+                        //此处事件必须小于超时时间
+                        new IdleStateHandler(40, 0, 0));
+                channel.pipeline().addLast(new ChannelDuplexHandler() {
+                    @Override
+                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
+                        if (evt instanceof IdleStateEvent) {
+                            ctx.disconnect();
+                        }
+                    }
+                });
+            }
+
+            @Override
+            public void afterBootstrapInitialized(Bootstrap bootstrap) {
+
+            }
+
+        };
+
+        return ClientResources.builder().nettyCustomizer(nettyCustomizer).build();
+    }
+}

+ 2 - 0
miniapp/src/main/resources/application-dev.yml

@@ -79,6 +79,8 @@ spring:
       password: 123456
       database: 10
       lettuce:
+        pool:
+          min-idle: 1
         cluster:
           refresh:
             adaptive: true

+ 2 - 0
miniapp/src/main/resources/application-prod.yml

@@ -79,6 +79,8 @@ spring:
       password: OPYCJ3mJpaU4IsP7ZeIyaus6FtHjLYmhNDGteuRc1gIFCAm7wi
       database: 10
       lettuce:
+        pool:
+          min-idle: 1
         cluster:
           refresh:
             adaptive: true