【发布时间】:2020-04-04 22:42:19
【问题描述】:
我有使用 Jedis 连接工厂连接到 Redis 集群的 Spring Boot 应用程序:
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());
redisClusterConfiguration.setPassword(redisProperties.getPassword());
jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration);
并从 application.yml 中读取节点列表:
spring:
redis:
host: 127.0.0.1
port: 6379
timeout: 300s
cluster:
nodes: 127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382
现在我们想切换到 Elasticache,因为无论如何我们都在 AWS 上托管我们的 Redis 集群。 这很容易完成。如果可以使用 AmazonElastiCache 库。 然后我们可以使用 AWS 凭证连接到 Elasticache 集群,将可用节点拉到列表中并将其传递给 Jedis,而不是在 application.yml 中硬编码它们,例如:
//get cache cluster nodes using AWS api
private List<String> getClusterNodes(){
AmazonElastiCache client = AmazonElastiCacheClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
DescribeCacheClustersRequest describeCacheClustersRequest = new DescribeCacheClustersRequest();
describeCacheClustersRequest.setShowCacheNodeInfo(true);
List<CacheCluster> cacheClusterList = client.describeCacheClusters(describeCacheClustersRequest).getCacheClusters();
List<String> nodeList = new ArrayList<>();
try {
for (CacheCluster cacheCluster : cacheClusterList) {
for(CacheNode cacheNode :cacheCluster.getCacheNodes()) {
String nodeAddr = cacheNode.getEndpoint().getAddress() + ":" +cacheNode.getEndpoint().getPort();
nodeList.add(nodeAddr);
}
}
}
catch(Exception e) {
e.printStackTrace();
}
return nodeList;
}
但 DevOps 团队表示,他们无法在所有实验室中配置 AWS 访问,他们有这样做的理由。此外,我们需要通过 URL 连接到特定的集群,而不是连接到 AWS 并拉取所有可用的集群。
所以我尝试将 Elasticache 集群 url 作为独立的和作为 application.yml 配置中的集群直接传递给 Jedis。 在这两种情况下都建立了连接,但是当 App 尝试写入 Elasticache 时,它会出现 MOVED 异常:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.data.redis.ClusterRedirectException: Redirect: slot 1209 to 10.10.10.011:6379.; nested exception is redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 1209 10.10.10.102:6379
据我了解,这意味着 App 尝试写入 Elasticache 中的一个节点,但无法连接。
那么问题是有没有办法只使用 Elasticache 集群 URL 从 Spring Boot 应用程序连接到 Elasticache Redis 集群?
我知道如果使用 Elasticache Memecache 是可行的。 Jedis驱动也不是硬性要求。
谢谢。
【问题讨论】:
标签: amazon-web-services spring-boot redis jedis amazon-elasticache