AWS Elasticache Jedis使用凭据

问题描述 投票:0回答:1

我需要连接到我的Elasticache中的redis实例。据我所知,从Amazon Elasticache Redis cluster - Can't get Endpoint,我可以从中得到端点。现在假设我得到了端点并且我使用这个端点创建了一个JedisClient(因为我使用了java)然后我如何提供AWS IAM凭证?我将使用IAM策略保护ElastiCache。如何确保没有其他应用程序连接到此redis?

jedis amazon-elasticache
1个回答
0
投票
static AWSCredentials credentials = null;
static {
    try {
        //credentials = new ProfileCredentialsProvider("default").getCredentials();
        credentials = new SystemPropertiesCredentialsProvider().getCredentials();
    } catch (Exception e) {
        System.out.println("Got exception..........");
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/Users/USERNAME/.aws/credentials), and is in valid format.", e);
    }       
}

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    AmazonElastiCache elasticacheClient = AmazonElastiCacheClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_1).build();
    DescribeCacheClustersRequest dccRequest = new DescribeCacheClustersRequest();
    dccRequest.setShowCacheNodeInfo(true);

    DescribeCacheClustersResult clusterResult = elasticacheClient.describeCacheClusters(dccRequest);

    List<CacheCluster> cacheClusters = clusterResult.getCacheClusters();
    List<String> clusterNodes = new ArrayList <String> ();
    try {
        for (CacheCluster cacheCluster : cacheClusters) {
            for (CacheNode cacheNode : cacheCluster.getCacheNodes()) {
                String addr = cacheNode.getEndpoint().getAddress();
                int port = cacheNode.getEndpoint().getPort();
                String url =  addr + ":" + port;
                if(<ReplicationGroup Name>.equalsIgnoreCase(cacheCluster.getReplicationGroupId()))
                    clusterNodes.add(url);  
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    LettuceConnectionFactory redisConnectionFactory = new LettuceConnectionFactory(new RedisClusterConfiguration(clusterNodes));
    redisConnectionFactory.setUseSsl(true);
    redisConnectionFactory.afterPropertiesSet();
    return redisConnectionFactory;
}
© www.soinside.com 2019 - 2024. All rights reserved.