redis 使用lettuce 启动内存泄漏错误的解决方案

2025-05-29 0 58

redis 使用 lettuce 出现

LEAK: hashedwheelTimer.release() was not called before it's garbage-collected. Enable advanced leak

内存泄漏。其实是内存不够大导致。

找到eclispe 中window->preferences->Java->Installed JRE ,点击右侧的Edit 按钮,在编辑界面中的 “Default VM Arguments ”选项中,填入如下值即可。

-Xms64m -Xmx128m

内存调的足够大可解决。

另一种方法无法解决。不知道是不是方法设置不对的问题

1、打开eclipse配置文件eclipse.ini,更改把-Xmx(其数值代表jvm可以使用的最大内存数)

2. 运行java程序时,选择run->run configuration->arguments,输入-Xms100M -Xmx800M(-Xms代表jvm启动时分配的内存大小,-Xmx代表可最大分配多少内存)。

补充:Redis连接池Lettuce踩坑记录

一、引言

近期项目频繁部署不同测试环境,在搭建运行环境与部署项目过程中踩到了不少Redis的坑。项目是基于SpringBoot2.1.12,SpringBoot2.1.X集成jar包Spring-data-redis-start 使用Lettuce作为Redis连接池。

SpringBoot1.x默认采用Jedis作为redis客户端连接池。

SpringBoot2.x,spring-data-redis 默认采用Lettuce作为redis客户端驱动连接池。

二、踩坑场景

运行环境Redis集群中某个master节点不稳定连接不上,导致SpringBoot应用连接Redis报错,报错连接timeout。

三、解决方案

基于Spring-data-redis重写RedisConnectionFactory Bean。需要设置“取消校验集群节点的成员关系”:.validateClusterNodeMembership(false)。

1、redis配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
spring:

redis:

cluster:

nodes:

- ${redis.host.cluster} #redis集群ip-port

password: ${redis.password}

timeout: 5000 #连接超时时间

lettuce:

pool:

max-active: 10 #连接池最大连接数

max-wait: -1 #连接池最大阻塞时间

max-idle: 5 #连接池中最大空闲连接

min-idle: 1 #连接池中最小空闲连接

redis:

cluster:

enabled: true

2、Config配置类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40
@Data

@Component

@ConditionalOnProperty(name = "redis.cluster.enabled", havingValue = "true", matchIfMissing = false)

public class RedisConfig {

@Autowired

RedisProperties redisProperties;

// 在构建LettuceConnectionFactory时,如果不使用内置的destroyMethod,可能会导致Redis连接早于其它Bean被销毁

@Bean(destroyMethod = "destroy")

public RedisConnectionFactory newLettuceConnectionFactory() {

// 配置用于开启自适应刷新和定时刷新。如自适应刷新不开启,Redis集群变更时将会导致连接异常

ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()

.enablePeriodicRefresh(Duration.ofSeconds(60))// 开启周期刷新(默认60秒)

.enableAdaptiveRefreshTriggers(RefreshTrigger.ASK_REDIRECT,RefreshTrigger.UNKNOWN_NODE)// 开启自适应刷新

.build();

ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()

.topologyRefreshOptions(clusterTopologyRefreshOptions)//拓扑刷新

.disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)

.autoReconnect(true)

.socketOptions(SocketOptions.builder().keepAlive(true).build())

                 .validateClusterNodeMembership(false)// 取消校验集群节点的成员关系

.build();

LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()

.clientOptions(clusterClientOptions)

.readFrom(ReadFrom.SLAVE_PREFERRED)

.build();

return new LettuceConnectionFactory(getClusterConfiguration(), clientConfig);

}

private RedisClusterConfiguration getClusterConfiguration() {

RedisProperties.Cluster clusterProperties = redisProperties.getCluster();

RedisClusterConfiguration config = new RedisClusterConfiguration(clusterProperties.getNodes());

if (clusterProperties.getMaxRedirects() != null) {

config.setMaxRedirects(clusterProperties.getMaxRedirects());

}

if (redisProperties.getPassword() != null) {

config.setPassword(RedisPassword.of(redisProperties.getPassword()));

}

return config;

}

}

注意:

RedisClusterConfiguration getClusterConfiguration()需要设置Password,不然SpringBoot启动报认证错误:

“io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication required”

如下:

另外,lettuce-core jar包版本不能过低,避免部分配置项无法支持,以上配置为lettuce-core-5.18.RELEASE.jar。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持快网idc。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/weixin_43965939/article/details/102956902

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 redis 使用lettuce 启动内存泄漏错误的解决方案 https://www.kuaiidc.com/108110.html

相关文章

发表评论
暂无评论