我想从你的Spring Boot 应用程序连接到Azure Redis。但我得到连接错误io.lettuce.core.RedisException: Cannot obtain initial Redis Cluster topology
,如果我看到详细消息,我可以看到Cannot retrieve cluster partitions from [rediss://********************************************@myurl.redis.cache.windows.net:6380?timeout=3s]
我按照以下代码建立连接。
在pom.xml中。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
然后在应用程序yml.
spring:
cache:
type: redis
redis:
ssl: false
host: qortex-platform-dev-backoffice-portal.redis.cache.windows.net
port: 6380
password: 8RP9lcB7fbiX0ceCwNyF8dOe9pJ33w6YbAzCa
配置就像。
@Bean
public RedisClusterConfiguration defaultRedisConfig() {
RedisClusterConfiguration configuration = new RedisClusterConfiguration();
configuration.addClusterNode(new RedisNode("myurl.redis.cache.windows.net",6380));
configuration.setPassword(RedisPassword.of(redisPassword));
return configuration;
}
@Bean
@Primary
public RedisConnectionFactory redisConnectionFactory(RedisClusterConfiguration defaultRedisConfig) {
ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
.enablePeriodicRefresh(Duration.ofMinutes(10L))
.enableAllAdaptiveRefreshTriggers()
.build();
LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
.useSsl().and()
.commandTimeout(Duration.ofMillis(3000))
.clientOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build())
.build();
return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);
}
@Bean
public RedisTemplate<String, SessionPayload> template(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, SessionPayload> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
在逻辑上。
SessionPayload sessionPayloadFetched = (SessionPayload) template.opsForHash().get(HASH_KEY, sessionPayload.getUserId());
为什么我会收到此错误?
1条答案
按热度按时间gk7wooem1#
确保您已经在本地安装了Redis集群。
这应该可以解决错误。