SpringBoot Redis 远程主机

Sar*_*eva 6 java redis lettuce spring-boot

我正在制作一个在后端使用 Spring Boot、MySQL 和 Redis 并在前端使用 Angular 的应用程序。我想将它部署到 Heroku,这样我就可以使用我的前端,但我似乎无法为 Redis 配置远程 URL。为此,我在 Heroku 上安装了 Redis To Go 附加组件,并准备好远程 URL。我只是不知道如何配置环境变量来访问它而不是 localhost 和默认端口 6379。

我在 application.properties 中添加了以下几行,但它仍然不起作用:

spring.redis.url= #URL
spring.redis.host= #HOSTNAME
spring.redis.password= #PASSWORD
spring.redis.port = #PORT
Run Code Online (Sandbox Code Playgroud)

我不断收到以下错误:

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 on localhost:6379; nested exception is com.lambdaworks.redis.RedisConnectionException: Unable to connect to localhost/127.0.0.1:6379
Run Code Online (Sandbox Code Playgroud)

有什么办法可以配置它来访问远程 url 而不是 localhost?

我使用的是生菜而不是 Jedis,我的 HttpSessionConfig 文件是:

@EnableRedisHttpSession
public class HttpSessionConfig {

    @Bean
    public LettuceConnectionFactory connectionFactory() {
        return new LettuceConnectionFactory();
    }

}
Run Code Online (Sandbox Code Playgroud)

Jul*_*nel 6

我遇到了类似的问题,我是这样解决的:

如果你这样定义 bean:

@Bean
public LettuceConnectionFactory connectionFactory() {
    return new LettuceConnectionFactory();
}
Run Code Online (Sandbox Code Playgroud)

您不允许 Spring 从application.properties.

要使其工作,请执行以下操作:

  1. 删除此 bean 定义:
@Bean
public LettuceConnectionFactory connectionFactory() {
    return new LettuceConnectionFactory();
}
Run Code Online (Sandbox Code Playgroud)
  1. 以这种方式定义 RedisTemplate bean:
@Bean
public RedisTemplate<String, Object> deliveryRedisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    return template;
}
Run Code Online (Sandbox Code Playgroud)
  1. 在 中定义以下值application.properties
spring.redis.database=<replace-me>
spring.redis.host=<replace-me>
spring.redis.port=<replace-me>
spring.redis.timeout=<replace-me>
Run Code Online (Sandbox Code Playgroud)


小智 5

我对您面临的完全相同的问题感到沮丧,并且 Spring Boot 文档或示例无法解决我们面临的问题。未使用您的配置条目的原因是您正在使用无参数构造函数创建 LettuceConnectionFactory 的新实例。深入研究源代码/字节代码,您可以看到构造函数完全忽略了 spring.redis.host 和 spring.redis.port 值,并将它们硬编码到 localhost:6379。

你应该做的是:

  • 使用 LettuceConnectionFactory(hostname, port) 构造函数。
  • 不要定义连接工厂。去掉 connectionFactory() 的整个 @Bean 条目,Spring 将自动连接所有内容并使用您的配置条目。

也作为旁注;为了让它与 AWS Elasticache 一起工作(通过 VPN 本地),我必须添加到该配置类中:

@Bean
public static ConfigureRedisAction configureRedisAction() {
    return ConfigureRedisAction.NO_OP;
}
Run Code Online (Sandbox Code Playgroud)


bku*_*iak 1

您需要在取决于自动配置或定义自定义连接模板之间进行选择。

第一种方法是删除,然后应用文件HttpSessionConfig中的 redis 属性。application.properties由于您对类路径有 spring-redis-data-session 依赖性,因此将隐式创建生菜连接。

第二种解决方案是将连接属性定义为主机、端口、密码LettuceConnectionFactory

但是建议使用自动配置。