redis 使用lettuce 啟動(dòng)內(nèi)存泄漏錯(cuò)誤的解決方案
redis 使用 lettuce 出現(xiàn)
LEAK: hashedwheelTimer.release() was not called before it's garbage-collected. Enable advanced leak
內(nèi)存泄漏。其實(shí)是內(nèi)存不夠大導(dǎo)致。
找到eclispe 中window->preferences->Java->Installed JRE ,點(diǎn)擊右側(cè)的Edit 按鈕,在編輯界面中的 “Default VM Arguments ”選項(xiàng)中,填入如下值即可。
-Xms64m -Xmx128m
內(nèi)存調(diào)的足夠大可解決。
另一種方法無法解決。不知道是不是方法設(shè)置不對(duì)的問題
1、打開eclipse配置文件eclipse.ini,更改把-Xmx(其數(shù)值代表jvm可以使用的最大內(nèi)存數(shù))
2. 運(yùn)行java程序時(shí),選擇run->run configuration->arguments,輸入-Xms100M -Xmx800M(-Xms代表jvm啟動(dòng)時(shí)分配的內(nèi)存大小,-Xmx代表可最大分配多少內(nèi)存)。
補(bǔ)充:Redis連接池Lettuce踩坑記錄
一、引言
近期項(xiàng)目頻繁部署不同測(cè)試環(huán)境,在搭建運(yùn)行環(huán)境與部署項(xiàng)目過程中踩到了不少Redis的坑。項(xiàng)目是基于SpringBoot2.1.12,SpringBoot2.1.X集成jar包Spring-data-redis-start 使用Lettuce作為Redis連接池。
SpringBoot1.x默認(rèn)采用Jedis作為redis客戶端連接池。
SpringBoot2.x,spring-data-redis 默認(rèn)采用Lettuce作為redis客戶端驅(qū)動(dòng)連接池。
二、踩坑場(chǎng)景
運(yùn)行環(huán)境Redis集群中某個(gè)master節(jié)點(diǎn)不穩(wěn)定連接不上,導(dǎo)致SpringBoot應(yīng)用連接Redis報(bào)錯(cuò),報(bào)錯(cuò)連接timeout。
三、解決方案
基于Spring-data-redis重寫RedisConnectionFactory Bean。需要設(shè)置“取消校驗(yàn)集群節(jié)點(diǎn)的成員關(guān)系”:.validateClusterNodeMembership(false)。
1、redis配置
spring:
redis:
cluster:
nodes:
- ${redis.host.cluster} #redis集群ip-port
password: ${redis.password}
timeout: 5000 #連接超時(shí)時(shí)間
lettuce:
pool:
max-active: 10 #連接池最大連接數(shù)
max-wait: -1 #連接池最大阻塞時(shí)間
max-idle: 5 #連接池中最大空閑連接
min-idle: 1 #連接池中最小空閑連接
redis:
cluster:
enabled: true
2、Config配置類
@Data
@Component
@ConditionalOnProperty(name = "redis.cluster.enabled", havingValue = "true", matchIfMissing = false)
public class RedisConfig {
@Autowired
RedisProperties redisProperties;
// 在構(gòu)建LettuceConnectionFactory時(shí),如果不使用內(nèi)置的destroyMethod,可能會(huì)導(dǎo)致Redis連接早于其它Bean被銷毀
@Bean(destroyMethod = "destroy")
public RedisConnectionFactory newLettuceConnectionFactory() {
// 配置用于開啟自適應(yīng)刷新和定時(shí)刷新。如自適應(yīng)刷新不開啟,Redis集群變更時(shí)將會(huì)導(dǎo)致連接異常
ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
.enablePeriodicRefresh(Duration.ofSeconds(60))// 開啟周期刷新(默認(rèn)60秒)
.enableAdaptiveRefreshTriggers(RefreshTrigger.ASK_REDIRECT,RefreshTrigger.UNKNOWN_NODE)// 開啟自適應(yīng)刷新
.build();
ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()
.topologyRefreshOptions(clusterTopologyRefreshOptions)//拓?fù)渌⑿?
.disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
.autoReconnect(true)
.socketOptions(SocketOptions.builder().keepAlive(true).build())
.validateClusterNodeMembership(false)// 取消校驗(yàn)集群節(jié)點(diǎn)的成員關(guān)系
.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()需要設(shè)置Password,不然SpringBoot啟動(dòng)報(bào)認(rèn)證錯(cuò)誤:
“io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication required”
如下:
另外,lettuce-core jar包版本不能過低,避免部分配置項(xiàng)無法支持,以上配置為lettuce-core-5.18.RELEASE.jar。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
解決Spring導(dǎo)出可以運(yùn)行的jar包問題
最近需要解決Maven項(xiàng)目導(dǎo)入可執(zhí)行的jar包的問題,如果項(xiàng)目不包含Spring,那么使用mvn assembly:assembly即可,這篇文章主要介紹了Spring導(dǎo)出可以運(yùn)行的jar包,需要的朋友可以參考下2023-03-03
Spring ApplicationListener的使用詳解
這篇文章主要介紹了Spring ApplicationListener的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
IDEA 單元測(cè)試報(bào)錯(cuò):Class not found:xxxx springb
這篇文章主要介紹了IDEA 單元測(cè)試報(bào)錯(cuò):Class not found:xxxx springboot的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot詳解如何進(jìn)行整合Druid數(shù)據(jù)源
Druid是阿里開發(fā)的一款開源的數(shù)據(jù)源,被很多人認(rèn)為是Java語言中最好的數(shù)據(jù)庫連接池,本文主要介紹了SpringBoot整合Druid數(shù)據(jù)源的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Java實(shí)現(xiàn)的漢語拼音工具類完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的漢語拼音工具類,結(jié)合完整實(shí)例形式分析了java基于pinyin4j包實(shí)現(xiàn)編碼轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11

