我使用AWS Java SDK来自动缩放ec2实例。以下是代码段。
AmazonAutoScaling client = AmazonAutoScalingClientBuilder.standard().build();
SetDesiredCapacityRequest request = new SetDesiredCapacityRequest().withAutoScalingGroupName("my-auto-scaling-group").withDesiredCapacity(2)
.withHonorCooldown(true);
SetDesiredCapacityResult response = client.setDesiredCapacity(request);
我将withHonorCooldown()设置为true。这意味着在AWS启动Ec2实例之前,后续的自动扩展请求将被阻止。所以我的用例是,我已经同时向AWS发出了100个自动调度请求,因为大多数请求都失败了。所以我的要求是检查特定AWS自动缩放组中当前是否有活动正在进行中。
if(!client.isAutoscaleInprogress())
client.setDesiredCapacity(request)
boolean isScalingActivityInProgress(){
AmazonAutoScaling client = AmazonAutoScalingClientBuilder.standard().build();
DescribeScalingActivitiesRequest request = new DescribeScalingActivitiesRequest().withAutoScalingGroupName("my-auto-scaling-group");
DescribeScalingActivitiesResult response = client .describeScalingActivities(request);
Activity activity=response.getActivities().get(0);
return !ScalingActivityStatusCode.Successful.name().equalsIgnoreCase(activity.getStatusCode()
}
注意:activity.getStatusCode()返回自动缩放组的当前活动状态。