如何在Spring数据redis中实时更新redis密码和连接



我想通过以下代码更新redistemplate以使用新密码连接redis。但我犯了错误。有人能帮忙吗?谢谢

LettuceConnectionFactory factory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
factory.getSentinelConfiguration().setPassword(newPassword);
factory.initConnection();

错误:org.springframework.data.redis.RedisConnectionFailureException:无法连接redis;嵌套异常为io.lettuce.core.RedisConnectionException:无法连接到10.32.216.24:6379网址:org.springframework.data.redis.connection.莴苣.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1112)~[spring-data-redis-2.1.15.RELEASE.jar:2.1.15.RELEASE]网址:org.springframework.data.redis.connection.莴苣.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1091)~[spring-data-redis-2.1.15.RELEASE.jar:2.1.15.RELEASE]网址:org.springframework.data.redis.connection.莴苣.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:872)~[spring-data-redis-2.1.15.RELEASE.jar:2.1.15.RELEASE]网址:org.springframework.data.redis.connection.莴苣.LettuceConnectionFactory.initConnection(LettuceConnectionFactory.java:413)~[spring-data-redis-2.1.15.RELEASE.jar:2.1.15.RELEASE]网址:com.xxx.oss.ntcapp.annotation.ResetRedisAct.resetRedisPassword(ResetRRedisAct.java:62)~[ntcapp-common-20.0.4.27.jar:?]网址:com.xxx.oss.ntcapp.annotation.ResetRedisAct.resetDisConnectionFactory(ResetRRedisAct.java:51)~[ntcap-common-20.0.4.27.jar:?]在sun.reflect.GeneratedMethodAccessor167.reinvoke(未知源)~[?:?]在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[?:1.8.0_242]位于java.lang.reflect.Method.ioke(Method.java:498)~[?:1.8.0_242]网址:org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspen JAdvice.java:644)~[spring-aop-5.1.13.RELEASE.jar:5.1.13.REASE]网址:org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod在org.springframework.aop.aspectj.AspectJAroundAdvice.ioke(AspectJAroundAdvice.java:70)~[spring-aop-5.1.13.RELEASE.jar:5.1.13.REASE]在org.springframework.aop.framework.ReflectiveMethodInvocation.prough(ReflectiveMethodInvocation.java:175)~[spring-aop-5.1.13.RELEASE.jar:5.1.13.REASE]位于org.springframework.aop.einterceptor.ExposeInvocationInterceptor.ioke(ExposeInvocationInterceptor.java:93)~[spring-aop-5.1.13.RELEASE.jar:5.1.13.REASE]在org.springframework.aop.framework.ReflectiveMethodInvocation.prough(ReflectiveMethodInvocation.java:186)~[spring-aop-5.1.13.RELEASE.jar:5.1.13.REASE]网址:org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.entercept(CglibAoProxy.java:689)~[spring-aop-5.1.13.RELEASE.jar:5.1.13.REASE]网址:com.xxx.oss.ntcapp.common.utils.RedisTemplateUtil$$EnhancerBySpringCGLIB$$3090c68.listSize()~[ntcapp-common-20.0.4.27.jar:?]网址:com.xxx.oss.ntcapp.config.SchedulerConfig.handleFmNotification(SchedulerConfig.java:156)~[ntcap-starter-20.0.4.27.jar:?]在sun.reflect.GeneratedMethodAccessor178.reinvoke(未知源)~[?:?]在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[?:1.8.0_242]位于java.lang.reflect.Method.ioke(Method.java:498)~[?:1.8.0_242]在org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)~[spring-context-5.1.13.RELEASE.jar:5.1.13.RRELEASE]在org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunable.java:54)[spring-context-5.1.13.RELEASE.jar:5.1.13.RRELEASE]在java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)[?:1.8.0_242]位于java.util.concurrent.FFutureTask.runAndReset(FutureTask.java:308)[?:1.8.0_242]在java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)[?:1.8.0_242]位于java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)[?:1.8.0_242]位于java.util.concurrent.ThreadPoolExecutiator.runWorker(ThreadPoolExecutiator.java:1149)[?:1.8.0_242]位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)[?:1.8.0_242]在java.lang.Thread.run(线程.java:748)[?:1.8.0_242]由:io.lettuce.core.RedisConnectionException引起:无法连接到10.32.216.24:6379位于io.leract.core.RRedisConnectionException.create(RedisConnection Exception.java:78)~[lettuce-core-5..1.8.RELEASE.jar:?]位于io.leract.core.RRedisConnectionException.create(RedisConnection Exception.java:56)~[lettuce-core-5..1.8.RELEASE.jar:?]位于io.leract.core.AbstractRedisClient.getConnection(AbstractRedisCClient.java:235)~[lettuce-core-5..1.8.RELEASE.jar:?]位于io.retact.core.RRedisClient.connect(RedisClient.java:204)~[lettuce-core-5..1.8.RELEASE.jar:?]网址:org.springframework.data.redis.connection.莴苣.StandaloneConnectionProviderLambda$getConnection$1(StandaloneConnectionProvider.java:113)~[spring-data-redis-2.1.15.RELEASE.jar:2.1.15.RELEASE]位于java.util.Optional.orElseGet(Optional.java:267)~[?:1.8.0_242]网址:org.springframework.data.redis.connection.莴苣.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:113)~[spring-data-redis-2.1.15.RELEASE.jar:2.1.15.RELEASE]网址:org.springframework.data.redis.connection.莴苣.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1110)~[spring-data-redis-2.1.15.RELEASE.jar:2.1.15.RELEASE]…还有29个由:io.lettuce.core.RedisCommandExecutionException引起:ERR无效密码在io.leract.core.ExceptionFactory.createExecutionException(ExceptionFactory.java:135)~[lettuce-core-5..1.8.RELEASE.jar:?]在io.leract.core.ExceptionFactory.createExecutionException(ExceptionFactory.java:108)~[lettuce-core-5..1.8.RELEASE.jar:?]位于io.leract.core.procol.AsyncCommand.completeResult(AsyncCommand.java:120)~[lettuce-core-5..1.8.RELEASE.jar:?]位于io.leract.core.procol.AsyncCommand.complete(AsyncCommand.java:111)~[lettuce-core-5..1.8.RELEASE.jar:?]在io.leract.core.procol.CommandWrapper.complete(CommandWrapper.java:59)~[lettuce-core-5..1.8.RELEASE.jar:?]位于io.leract.core.procol.CommandHandler.complete(CommandHandler.java:646)~[lettuce-core-5..1.8.RELEASE.jar:?]位于io.leract.core.procol.CommandHandler.decode(CommandHandler.java:604)~[lettuce-core-5..1.8.RELEASE.jar:?]位于io.leract.core.procol.CommandHandler.channelRead(CommandHandler.java:556)~[lettuce-core-5..1.8.RELEASE.jar:?]位于io.nety.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandler Content.java:377)~[nety-transport-4.145.Final.jar:4.1.45.Final]位于io.nety.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandler Content.java:363)~[nety-transport-4.145.Final.jar:4.1.45.Final]位于io.nety.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandler Content.java:355)~[nety-transport-4.145.Final.jar:4.1.45.Final]在io.nety.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPiperine.java:1410)~[nety-transport-4.1.45.Final.jar:4.1.45.Final]位于io.nety.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandler Content.java:377)~[nety-transport-4.145.Final.jar:4.1.45.Final]位于io.nety.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandler Content.java:363)~[nety-transport-4.145.Final.jar:4.1.45.Final]在io.nety.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPiperine.java:919)~[nety-transport-4.1.45.Final.jar:4.1.45.Final]在io.nety.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)~[Nety-transport-4.145.Final.jar:4.1.45.Final]在io.nety.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)~[nety-transport-4.145.Final.jar:4.1.45.Final]在io.nety.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)~[nety-transport-4.145.Final.jar:4.1.45.Final]在io.nety.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)~[nety-transport-4.145.Final.jar:4.1.45.Final]在io.nety.channel.nio.NioEventLoop.run(NioEventLoop.java:493)~[nety-transport-4.1.45.Final.jar:4.1.45.Final]位于io.nety.util.concurrent.SingleThreadEventExecutiator$4.run(SingleThreadEventExecutor.java:989)~[nety-common-4.1.45.Final.jar:4.1.45.Final]位于io.nety.util.internal.ThreadExecutiorMap$2.run(ThreadExecutiorMap.java:74)~[nety-common-4.1.45.Final.jar:4.1.45.Final]位于io.nety.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)~[nety-common-4.1.45.Final.jar:4.1.45.Final]…还有1个

使用以下代码解决:

LettuceConnectionFactory factory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
if (null != factory.getSentinelConfiguration()) {
factory.getSentinelConfiguration().setPassword(newPassword);
}
factory.afterPropertiesSet();
factory.initConnection();
factory.validateConnection();

最新更新