2017-08-16 44 views
1

我試圖使用AWS Java API獲取負載均衡器的列表。describeLoadBalancers不顯示經典的負載均衡器

AmazonElasticLoadBalancing elbClient = AmazonElasticLoadBalancingClient 
      .builder() 
      .withCredentials(new DefaultAWSCredentialsProviderChain()) 
      .withRegion(Regions.EU_WEST_1) 
      .build(); 
    DescribeLoadBalancersResult result = elbClient.describeLoadBalancers(
      new DescribeLoadBalancersRequest()); 
    for (LoadBalancer lb : result.getLoadBalancers()) { 
     System.out.println(lb.getLoadBalancerName()); 
    } 

該調用有效,但僅列出了新的應用程序負載平衡器。我沒有看到任何經典的負載平衡器。我的憑證不受限制。

如何獲取經典負載均衡器的句柄?

回答

2

看起來Elastic Load Balancing有兩個API。 javadoc爲AmazonElasticLoadBalancingClient提供了一個提示:

此參考資料涵蓋2015-12-01 API,它支持應用程序負載平衡器。 2012-06-01 API支持經典負載平衡器。

對於下面的代碼中,註釋掉的代碼不會打印出經典的負載均衡,但註釋掉了代碼將:

/* 
import com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancing; 
import com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancingClientBuilder; 
import com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersRequest; 
import com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult; 
*/ 
import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancing; 
import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClientBuilder; 
import com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersRequest; 
import com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersResult; 
import org.junit.Test; 

public class AwsTestIT 
{ 
    /* 
    @Test 
    public void testGetLoadBalancers() 
    { 
     AmazonElasticLoadBalancing amazonElasticLoadBalancingClient = AmazonElasticLoadBalancingClientBuilder 
      .defaultClient(); 
     DescribeLoadBalancersResult result = 
      amazonElasticLoadBalancingClient.describeLoadBalancers(new DescribeLoadBalancersRequest()); 
     result.getLoadBalancers().stream().forEach(loadBalancer -> System.out 
      .println("loadBalancer = " + loadBalancer)); 
    } 
    */ 

    @Test 
    public void testGetLoadBalancers() 
    { 
     AmazonElasticLoadBalancing amazonElasticLoadBalancingClient = AmazonElasticLoadBalancingClientBuilder 
      .defaultClient(); 
     DescribeLoadBalancersResult result = 
      amazonElasticLoadBalancingClient.describeLoadBalancers(new DescribeLoadBalancersRequest()); 
     result.getLoadBalancerDescriptions().stream().forEach(loadBalancer -> System.out 
      .println("loadBalancer = " + loadBalancer)); 
    } 
} 
+1

謝謝你,我做了它的工作使用舊的AWS SDK,像這一個:https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-elasticloadbalancing/1.11.158 – stivlo

2

有2個獨立的API:一個用於經典ELB,一個用於ALB。

您正在使用的那個可能是「v2」API,只會返回ALB。

您需要使用「v1」API檢索經典ELB。

例如,在AWS CLI中,有aws elbaws elbv2