我正在尝试使用AWS Java API获取负载均衡器列表。
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());
}
该调用有效,但是仅列出了新的应用程序负载平衡器。我看不到任何经典的负载均衡器。我的凭据不受限制。
如何获得经典的负载均衡器?
最佳答案
看来有两个用于Elastic Load Balancing的API。 AmazonElasticLoadBalancingClient的javadoc提供了一个提示:
本参考资料涵盖2015-12-01 API,该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));
}
}
关于java - describeLoadBalancers没有显示经典的负载均衡器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45718859/