redis - 创建名称为 'enableRedisKeyspaceNotificationsInitializer' 的 bean 时出错

标签 redis spring-session azure-redis-cache

我能够使用以下 Spring Session 配置连接到 Azure Cache for Redis:

<bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
    <constructor-arg index="0" value="xxxxxxxxxxxxxxxx"/>
</bean>

<bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
    <property name="hostName" value="acmedev.redis.cache.windows.net"/>
    <property name="port" value="6380"/>
    <property name="password" ref="redisPassword"/>
</bean>

<context:annotation-config/>
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

<bean class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
    <constructor-arg index="0" ref="redisStandaloneConfiguration"/>
</bean>

我的应用成功连接:

[lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at acmedev.redis.cache.windows.net:6380: Success

然后应用程序挂起一段时间,我最终得到了这个错误

11:22:54.712 [lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.protocol.CommandHandler - [channel=0xcf902cd8, /10.1.200.58:53533 -> acmedev.redis.cache.windows.net/52.240.141.200:6380, chid=0x1] Storing exception in connectionError
2020-02-19 11:22:54,713  WARN (org.springframework.context.support.AbstractApplicationContext:558) || - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to acmedev.redis.cache.windows.net:6380
11:22:54.719 [RMI TCP Connection(3)-127.0.0.1] DEBUG io.lettuce.core.RedisClient - Initiate shutdown (100, 100, MILLISECONDS)


[lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.protocol.CommandHandler - [channel=0xcf902cd8, /10.1.200.58:53533 -> acmedev.redis.cache.windows.net/52.240.141.200:6380, chid=0x1] Unexpected exception during request: java.io.IOException: An existing connection was forcibly closed by the remote host
java.io.IOException: An existing connection was forcibly closed by the remote host
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
    at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1133)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    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)

当我使用在 localhost 上运行的 redis 时,这些相同的 bean 工作得很好。

我在这里做错了什么?

最佳答案

首先 RedisHttpSessionConfiguration 尝试(默认)enable键空间通知。但这仅适用于不安全的实例。

文档表单类 ConfigureNotifyKeyspaceEventsAction 解释为什么它只适用于本地主机:

This strategy will not work if the Redis instance has been properly secured. Instead, the Redis instance should be configured externally and a Bean of type ConfigureRedisAction#NO_OP should be exposed.

并且还解释了应该如何配置它以使用安全的 Redis 实例。

只需使用方法:RedisHttpSessionConfiguration#setConfigureRedisAction 设置 ConfigureRedisAction#NO_OP 然后例如在你的 redis 实例中调用:config set notify-keyspace-events Egx

关于redis - 创建名称为 'enableRedisKeyspaceNotificationsInitializer' 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60306331/

相关文章:

redis - 使用 socket.io-redis 保存聊天记录

redis - Spring session 代币

spring - FindByIndexNameSessionRepository “RedisConnectionFactory is required” 配置错误

asp.net - Azure Redis 缓存自动清除 PubSub channel

Azure Redis 缓存 + Azure 上的 WordPress

azure - 使用 Azure Redis 缓存配置 Gitlab 时出现错误

django - Redis 到 Socket.IO

django - 我在哪里放置代码以将数据从数据库推送到 django-redis 的 redis?

redis - 删除Redis中的多个键

spring-security - 如何在 spring boot 应用程序中使用 spring security 和 Redis 获取当前经过身份验证的用户 Principal