You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Lettuce本地连接Google Cloud Redis Memoryspace实例

本地Java应用连接Google Cloud Memorystore Redis实例失败的解决方法

问题背景

在Google Cloud上拥有Redis Memorystore实例,通过Google Cloud Shell执行以下命令可成功连接:

gcloud container clusters get-credentials CLUSTER_NAME --zone CLUSTER_ZONE --project PROJECT_ID
kubectl run -i --tty redisbox --image=gcr.io/google_containers/redis:v1 -- sh
redis-cli -h HOST-IP 

但本地Java应用使用以下配置类连接时:

@Configuration
public class RedisConfig {

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

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

    @Bean
    public RedisStandaloneConfiguration redisStandaloneConfiguration() {
        return new RedisStandaloneConfiguration(redisHost, redisPort);
    }

    @Bean
    public ClientOptions clientOptions(){
        return ClientOptions.builder()
                .disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
                .autoReconnect(true)
                .build();
    }

    @Bean
    LettucePoolingClientConfiguration lettucePoolConfig(ClientOptions options, ClientResources dcr){
        return LettucePoolingClientConfiguration.builder()
                .poolConfig(new GenericObjectPoolConfig())
                .clientOptions(options)
                .clientResources(dcr)
                .build();
    }

    @Bean
    public RedisConnectionFactory connectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration,
                                                    LettucePoolingClientConfiguration lettucePoolConfig) {
        return new LettuceConnectionFactory(redisStandaloneConfiguration, lettucePoolConfig);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}

执行redisTemplate.getConnectionFactory().getConnection().ping();时出现错误:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to (my host)/<unresolved>:6379

解决步骤

1. 明确Memorystore的网络访问限制

Google Cloud Memorystore Redis默认仅允许同VPC内的资源访问,本地机器不在GCP VPC范围内,直接使用私有IP无法连通,这是核心问题。需通过以下方式打通网络:

  • 长期方案:配置Cloud VPN或Cloud Interconnect,将本地网络与GCP VPC对接
  • 临时调试方案:使用GCP IAP创建端口转发隧道

2. 配置本地端口转发(临时调试)

执行以下命令在本地与Memorystore实例间建立隧道:

gcloud compute start-iap-tunnel INSTANCE_NAME 6379 --local-host-port=localhost:6379 --zone=INSTANCE_ZONE --project=PROJECT_ID

替换INSTANCE_NAME、INSTANCE_ZONE、PROJECT_ID为你的实际资源信息。

隧道建立后,修改Java应用配置:

spring.data.redis.host=localhost
spring.data.redis.port=6379

3. 调整Java配置适配网络/SSL要求

  • 若Memorystore实例启用了SSL,需在Lettuce配置中添加SSL支持:
    @Bean
    LettucePoolingClientConfiguration lettucePoolConfig(ClientOptions options, ClientResources dcr){
        return LettucePoolingClientConfiguration.builder()
                .poolConfig(new GenericObjectPoolConfig())
                .clientOptions(options)
                .clientResources(dcr)
                .useSsl() // 启用SSL
                .build();
    }
    
  • 确保配置中的spring.data.redis.host为Memorystore的私有IP,而非公网IP(Memorystore无公网IP)

4. 验证网络连通性

在本地执行telnet localhost 6379(端口转发场景)或telnet MEMORYSTORE_PRIVATE_IP 6379(VPN连通场景),确认端口可正常访问。若连接失败,优先排查网络问题,再检查Java配置。

内容的提问来源于stack exchange,提问作者Radu Suciu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 19:10:29