Spring Boot 为什么Azure Redis和Sping Boot 之间出现连接错误?

68de4m5k  于 2022-12-23  发布在  Spring
关注(0)|答案(1)|浏览(259)

我想从你的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());

为什么我会收到此错误?

gk7wooem

gk7wooem1#

确保您已经在本地安装了Redis集群。

$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ tar xzf redis-5.0.5.tar.gz
$ cd redis-5.0.5
$ make //The binaries that are now compiled are available in the src directory
$ src/redis-server

这应该可以解决错误。

相关问题