【问题标题】:AWS Elasticache Jedis using credentialsAWS Elasticache Jedis 使用凭证
【发布时间】:2018-03-11 04:05:41
【问题描述】:

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

【问题讨论】:

    标签: jedis amazon-elasticache


    【解决方案1】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-29
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 2021-09-09
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多