谷歌云内存存储(Redis),实例刚启动时无法连接到redis

Posted

技术标签:

【中文标题】谷歌云内存存储(Redis),实例刚启动时无法连接到redis【英文标题】:Google Cloud Memory Store (Redis), can't connect to redis when instance is just started 【发布时间】:2019-07-01 10:26:54 【问题描述】:

当我的实例刚刚启动时,我无法连接到 redis。

我用:

runtime: java
env: flex

runtime_config:  
  jdk: openjdk8

我遇到了以下异常:

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out

RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

java.net.SocketTimeoutException: connect timed out

2-3 分钟后,它运行顺畅

我是否需要在我的代码中添加一些检查或者我应该如何正确修复它?

附言 我也使用spring boot,配置如下

@Value("$spring.redis.host")
private String redisHost;

@Bean
JedisConnectionFactory jedisConnectionFactory() 
    // https://cloud.google.com/memorystore/docs/redis/quotas
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, 6379);
    return new JedisConnectionFactory(config);


@Bean
public RedisTemplate<String, Object> redisTemplate(
        @Autowired JedisConnectionFactory jedisConnectionFactory
) 
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer(newObjectMapper()));
    return template;

在 pom.xml 中

    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.1.2.RELEASE</version>

【问题讨论】:

我要骂java了,但是你的问题不是从你的java服务器开始的吗?如果您的 memstore 已经启动,则连接它应该没有任何问题,一个好的测试是在您启动另一个实例时将第二个实例连接到 memstore 并查看是否有任何问题。 memstore 已经启动。我想,也许这是我运行 JVM 的 VM 实例的问题,由于某种原因,我无法建立与 redis 的连接。虚拟机刚启动时会不会是虚拟机的网络问题? 从来没有遇到过一些php服务器的问题。您还有其他连接依赖项吗?它们是在实例启动时启动并运行还是它们也有一些超时? 【参考方案1】:

我解决这个问题的方法如下:总之,我添加了“ping”方法,它尝试从Redis中设置和获取值;如果可能,则应用程序已准备就绪。

实施:

首先,您需要更新app.yaml添加以下内容:

readiness_check:
path: "/readiness_check"
check_interval_sec: 5
timeout_sec: 4
failure_threshold: 2
success_threshold: 2
app_start_timeout_sec: 300

第二,在你的休息控制器中:

@GetMapping("/readiness_check")
public ResponseEntity<?> readiness_check() 

    if (!cacheConfig.ping()) 
        return ResponseEntity.notFound().build();
    

    return ResponseEntity.ok().build();

第三,CacheConfig类:

public boolean ping() 
    long prefix = System.currentTimeMillis();
    try 
        redisTemplate.opsForValue().set("readiness_check_" + prefix, Boolean.TRUE, 100, TimeUnit.SECONDS);
        Boolean val = (Boolean) redisTemplate.opsForValue().get("readiness_check_" + prefix);
        return Boolean.TRUE.equals(val);
     catch (Exception e) 
        LOGGER.info("ping failed for " + System.currentTimeMillis());
        return false;
    


附: 另外,如果有人需要 CacheConfig 的完整实现:​​

@Configuration
public class CacheConfig 

    private static final Logger LOGGER = Logger.getLogger(CacheConfig.class.getName());

    @Value("$spring.redis.host")
    private String redisHost;

    private final RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public CacheConfig(@Lazy RedisTemplate<String, Object> redisTemplate) 
        this.redisTemplate = redisTemplate;
    

    @Bean
    JedisConnectionFactory jedisConnectionFactory(
            @Autowired JedisPoolConfig poolConfig
    ) 
        // https://cloud.google.com/memorystore/docs/redis/quotas
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, 6379);

        JedisClientConfiguration clientConfig = JedisClientConfiguration
                .builder()
                .usePooling()
                .poolConfig(poolConfig)
                .build();

        return new JedisConnectionFactory(config, clientConfig);
    

    @Bean
    public RedisTemplate<String, Object> redisTemplate(
            @Autowired JedisConnectionFactory jedisConnectionFactory
    ) 
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer(newObjectMapper()));
        return template;
    

    /**
     * Example: https://github.com/PengliuIBM/pws_demo/blob/1becdca1bc19320c2742504baa1cada3260f8d93/redisData/src/main/java/com/pivotal/wangyu/study/springdataredis/config/RedisConfig.java
     */
    @Bean
    redis.clients.jedis.JedisPoolConfig jedisPoolConfig() 
        final redis.clients.jedis.JedisPoolConfig poolConfig = new redis.clients.jedis.JedisPoolConfig();

        // Maximum active connections to Redis instance
        poolConfig.setMaxTotal(16);
        // Number of connections to Redis that just sit there and do nothing
        poolConfig.setMaxIdle(16);
        // Minimum number of idle connections to Redis - these can be seen as always open and ready to serve
        poolConfig.setMinIdle(8);

        // Tests whether connection is dead when returning a connection to the pool
        poolConfig.setTestOnBorrow(true);
        // Tests whether connection is dead when connection retrieval method is called
        poolConfig.setTestOnReturn(true);
        // Tests whether connections are dead during idle periods
        poolConfig.setTestWhileIdle(true);

        return poolConfig;
    

    public boolean ping() 
        long prefix = System.currentTimeMillis();
        try 
            redisTemplate.opsForValue().set("readiness_check_" + prefix, Boolean.TRUE, 100, TimeUnit.SECONDS);
            Boolean val = (Boolean) redisTemplate.opsForValue().get("readiness_check_" + prefix);
            return Boolean.TRUE.equals(val);
         catch (Exception e) 
            LOGGER.info("ping failed for " + System.currentTimeMillis());
            return false;
        
    

【讨论】:

以上是关于谷歌云内存存储(Redis),实例刚启动时无法连接到redis的主要内容,如果未能解决你的问题,请参考以下文章

停止和启动深度学习谷歌云虚拟机实例导致 tensorflow 停止识别 GPU

谷歌云如何在使用 Python api 创建实例时启动服务?

谷歌云sql中的Mysql连接

谷歌云存储功能在订阅者连接时发送已发送的消息

谷歌云存储报告下载无法访问

谷歌云平台计算引擎