我有一个Spring Boot应用程序,它使用Jedis Connection Factory连接到Redis集群:
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集群,获取可用节点并将其放入列表中,而不是硬编码在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连接到特定的Elasticache集群,而不是连接到AWS并获取所有可用集群。
所以我尝试直接将Elasticache集群URL传递给Jedis作为独立项和应用程序.yml配置中的集群。在两种情况下都建立了连接,但当应用程序尝试写入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
据我所理解,这意味着应用程序尝试写入 Elasticache 中的一个节点,但未能连接。
所以问题是是否有一种方法可以使用仅 Elasticache 集群 URL 从 Spring Boot 应用程序连接到 Elasticache Redis 集群?
我知道如果使用 Elasticache Memecache 是可行的。此外,Jedis驱动程序不是硬性要求。
谢谢。