无法连接到Redis,RedisConnectionException:无法连接到{hostName}:6379/< unresolved>:6379*

bvhaajcl  于 2023-11-16  发布在  Redis
关注(0)|答案(1)|浏览(267)

我尝试在SpringBoot项目中配置redis连接。应用程序启动时没有任何问题,但当我尝试测试任何Redis操作时,特别是当调用实际调用仓库的REST控制器时,我总是遇到相同的错误。
下面是我在pom.xml中使用的版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.6</version>
    <relativePath />
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

字符串
下面是我用来连接Redis的配置类:

@Configuration
@RequiredArgsConstructor
public class RedisConfigurationBuilder {

    private final RedisProperties redisProperties;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        try {
            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
            redisStandaloneConfiguration.setHostName(redisProperties.getHost());
            redisStandaloneConfiguration.setPort(redisProperties.getPort());
            redisStandaloneConfiguration.setUsername(redisProperties.getUsername());
            redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));

            LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().useSsl().and()
                    .commandTimeout(Duration.ofSeconds(2)).build();

            return new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
        } catch (Exception e) {
            throw new RuntimeException("Failed to configure Redis", e);
        }
    }

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}


这是RedisProperties类,它从我的application.yml文件中读取属性:

@Component
@Getter
public class RedisProperties {

    @Value("${spring.data.redis.host}")
    private String host;

    @Value("${spring.data.redis.port}")
    private int port;

    @Value("${spring.data.redis.username}")
    private String username;

    @Value("${spring.data.redis.password}")
    private String password;

}


最后,这是我的应用程序。yml:

spring:
  application:
    name: "application-name"
  data:
    redis:
      host: {hostName}
      port: 6379
      username: {myUsername}
      password: {myPassword}
      ssl: 
        enabled: true


注意事项:在堆栈跟踪中,我将真实的主机名替换为{hostName},只是为了隐藏我连接到的实际服务器。错误消息如下:
org.springframework.data.redis.RedisConnectionFailureException:Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException:Unable to connect to {hostName}:6379/< unresolved >:6379
这是我不断得到的错误的堆栈跟踪:
org.springframework.data.redis.RedisConnectionFailureException:无法连接到Redis;嵌套的异常是io.lettuce.core.RedisConnectionException:无法连接到{hostName}:6379/< unresolved >:6379 at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider. letteException(LettuceConnectionFactory.java:1689)at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1597)at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1383)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.base/java.lang.Thread.run(Thread.java:833)Caused by:io.lettuce.core.RedisConnectionException:Unable to connect to {hostName}/:6379 atio.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:330)at io.lettuce.core.RedisClient.connect(RedisClient.java:216)在org.springframework.data.redis.connection.source.StandaloneConnectionProvider.sourceda$getConnection$1(StandaloneConnectionProvider.java:115)at java.base/java.util.orElseGet(Optional.java:364)at org.springframework.data.redis.connection.lettuce. StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:115)at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1595). 77 common frames omitted Caused by:java.lang.IllegalArgumentException:主机具有端口:{hostName}:6379,位于io.lettuce.core.internal.LettuceAssert.isTrue(LettuceAssert. java:222),位于io.lettuce.core.internal.HostAndPort.of(HostAndPort. java:57),位于io.lettuce.core.SslConnectionBuilder.toHostAndPort(SslConnectionBuilder.java:109),位于io.lettuce.core.SslConnectionBuilder.build(SslConnectionBuilder.java:99),位于

ncecgwcz

ncecgwcz1#

redis密码未设置或错误。我看到java工作目录更改和属性文件未加载时的问题。

相关问题