使用SSL在AWS上使用Spring-boot-starter-data-redis的启动错误

时间:2019-07-15 19:36:21

标签: spring-boot spring-data spring-data-redis

因此,今天遇到了一个有趣的问题,搞砸了spring-boot-starter-data-rest。我的用例是在Redis上存储一些具有一定到期时间的数据,以便在经过该时间后立即将其清除。我完成了所有配置,可以在本地Redis上正常运行。当尝试使用安全连接(SSL)在AWS上使用它时,这成为一个问题,并且是一个示例错误:

           at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
           at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
           at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
           at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
           at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
           at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
           at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
           at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
           at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
           at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
           at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
           at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
           at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
           at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
           at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
           at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
           at com.springbootapp.config.Application.main(Application.java:16)
           ... 6 more
   Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: ERR Unsupported CONFIG parameter: notify-keyspace-events; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR Unsupported CONFIG parameter: notify-keyspace-events
           at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:44)
           at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:36)
           at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37)
           at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37)
           at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:210)
           at org.springframework.data.redis.connection.jedis.JedisConnection.setConfig(JedisConnection.java:633)
           at org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction.configure(ConfigureNotifyKeyspaceEventsAction.java:63)
           at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration$EnableRedisKeyspaceNotificationsInitializer.afterPropertiesSet(RedisHttpSessionConfiguration.java:167)
           at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
           at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
           ... 22 more
   Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Unsupported CONFIG parameter: notify-keyspace-events
           at redis.clients.jedis.Protocol.processError(Protocol.java:117)
           at redis.clients.jedis.Protocol.process(Protocol.java:151)
           at redis.clients.jedis.Protocol.read(Protocol.java:205)
           at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297)
           at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:196)
           at redis.clients.jedis.Jedis.configSet(Jedis.java:2575)
           at org.springframework.data.redis.connection.jedis.JedisConnection.setConfig(JedisConnection.java:631)
           ... 26 more

我没有使用@EnabledRedisSession,它具有依赖性,因为我不需要它。

事实证明,那里的大多数示例都是针对此的。

https://stackoverflow.com/questions/36177688/embedded-redis-cant-start

我添加了spring redis会话依赖性并尝试了

@Bean
public static ConfigureRedisAction configureRedisAction() {
    return ConfigureRedisAction.NO_OP;
}

@EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_DEMAND)

将其更改为on_demand,出现以下错误。

下面显示的我的redisContainer失败。

@EnableAsync
@Configuration
@EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_DEMAND)
public class RedisConfiguration {

    private DeloreanProperties deloreanProperties;

    private ApplicationEventPublisher applicationEventPublisher;

    private RedisConfigProperties redisConfigProperies;

    private Environment environment;

    @Autowired
    public void setDeloreanProperties(DeloreanProperties deloreanProperties) {
        this.deloreanProperties = deloreanProperties;
    }

    @Autowired
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    @Autowired
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Autowired
    public void setRedisConfigProperies(RedisConfigProperties redisConfigProperies) {
        this.redisConfigProperies = redisConfigProperies;
    }

    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
        jedisClientConfiguration.connectTimeout(Duration.ofSeconds(60));
        jedisClientConfiguration.usePooling();

        if (Arrays.stream(environment.getActiveProfiles()).anyMatch(env -> (env.equalsIgnoreCase("deployed")))) {
            jedisClientConfiguration.useSsl();
        }

        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(redisConfigProperies.getHost());
        configuration.setPort(redisConfigProperies.getPort());
        configuration.setPassword(redisConfigProperies.getPassword());
        return new JedisConnectionFactory(configuration, jedisClientConfiguration.build());
    }

    @Bean
    public RedisTemplate<?, ?> redisTemplate() {
        RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory());
        return template;
    }

    @Bean
    public RedisMessageListenerContainer redisContainer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory());
        container.setTaskExecutor(Executors.newFixedThreadPool(deloreanProperties.getListenExecutorFixedThreadPool()));
        return container;
    }

    @Bean
    public MessageListener messageListener() {
        KeyExpirationEventMessageListener messageListener = new KeyExpirationEventMessageListener(redisContainer());
        messageListener.setApplicationEventPublisher(applicationEventPublisher);
        return messageListener;
    }
}

2 个答案:

答案 0 :(得分:0)

事实证明,人们对此无能为力。这是info block in the documentation

如果尚未设置键空间通知消息侦听器,则它们会更改Redis中的notify-keyspace-events设置。现有设置不会被覆盖,因此您必须正确设置这些设置(或将其保留为空)。请注意,在AWS ElastiCache上已禁用CONFIG,并且启用侦听器会导致错误。

答案 1 :(得分:0)

感谢Mota问题,因为我的团队遇到了同样的问题,并且您发布的文档非常有用。

经过研究,我发现JIRA issue与他们在此Git PR中解决问题的地方有关

如您之前所述,问题是侦听器尝试获取和修改Redis节点的某些参数,并且AWS Elasticache不允许使用CONFIG命令。

  • 在其代码comment之后,您可以通过在@EnableRedisRepositories中的keyspaceNotificationsConfigParameter初始化空字符串来禁用侦听器运行这些CONFIG命令
  • 记住还要创建一个新的Elasticache parameter group,在其中将notify-keyspace-events设置为AKE,并使用新的参数组更新Redis节点

完成上述更改后,我们没有看到任何问题,并且侦听器正在按预期工作。