弹性伸缩实例管理
初始化客户端
参数获取可参照准备工作中token认证方式。
Copied!
String user = "replace-with-your-username";
String password = "replace-with-your-password";
String projectId = "replace-with-your-projectId";
String userDomainId = "replace-with-your-domainId";
String authUrl = "https://iam.example.com/v3";
OSClientV3 osclient = OSFactory.builderV3().endpoint(authUrl)
.credentials(user, password, Identifier.byId(userDomainId))
.scopeToProject(Identifier.byId(projectId)).authenticate();
查询弹性伸缩组中的实例列表
根据输入条件过滤查询弹性伸缩组中实例信息。查询结果分页显示。若不加过滤条件默认查询组内最多20条实例信息。
Copied!
public void listInstances(String groupId) {
List< ? extends ScalingGroupInstance> listInstances = osclient.autoScaling().groupInstances().list(groupId);
if (null != listInstances) {
System.out.println("List all scalingInstances success, size is = " + listInstances.size());
} else {
System.out.println("get scalingInstances failed");
}
}
说明
:可根据实例在伸缩组中的生命周期状态,实例健康状态,实例保护状态,起始行号,记录条数进行条件过滤查询。更多信息请参考API Explorer。
批量操作实例
批量移出伸缩组中的实例或批量添加伸缩组外的实例
Copied!
public void batchOperateInstances(String groupId) throws InterruptedException {
ArrayList<String> instanceIds = Lists.newArrayList("******");
ActionResponse resp = osclient.autoScaling().groupInstances().batchRemove(groupId, instanceIds, false);
if (resp.isSuccess()) {
System.out.println("batchAdd scalingInstances success");
} else {
System.out.println("batchAdd scalingInstances failed");
}
TimeUnit.MINUTES.sleep(5);
ActionResponse resp1 = osclient.autoScaling().groupInstances().batchAdd(groupId, instanceIds, false);
if (resp1.isSuccess()) {
System.out.println("batchRemove scalingInstances success");
} else {
System.out.println("batchRemove scalingInstances failed");
}
}
说明
:
- 1、还可以批量对伸缩组中的实例设置或取消其实例保护属性。批量将伸缩组中的实例转入或移出备用状态。具体使用请参考API Explorer。
- 2、单次最多批量操作实例个数为10。批量添加后实例数不能大于伸缩组的最大实例数,批量移出后实例数不能小于伸缩组的最小实例数。
- 3、当伸缩组处于INSERVICE状态且没有伸缩活动时,才能添加实例。
- 4、当伸缩组没有伸缩活动时,才能移出实例。
- 5、向伸缩组中添加实例时,必须保证实例所在的可用区包含于伸缩组的可用区内。
- 6、实例处于INSERVICE状态时才可以进行移出、设置或取消实例保护属性等操作。
- 7、当伸缩组发生自动缩容活动时,设置了实例保护的实例不会被移出伸缩组。
- 8、批量移出弹性伸缩组中的实例时,若该实例加入伸缩组时绑定的监听器和伸缩组本身的监听器相同,会解绑定实例和监听器。若该实例加入伸缩组时绑定的监听器和伸缩组本身的监听器不同,会保留实例和监听器的绑定关系。
移除弹性伸缩组实例
从弹性伸缩组中移出一个指定实例
Copied!
public void deleteInstance() {
String instanceId = "******";
ActionResponse resp = osclient.autoScaling().groupInstances().delete(instanceId, false);
if (resp.isSuccess()) {
System.out.println("Delete scalingInstance success");
} else {
System.out.println("Delete scalingInstance failed");
}
}
说明
:
- 1、实例处于INSERVICE且移出后实例数不能小于伸缩组的最小实例数时才可以移出。当伸缩组没有伸缩活动时,才能移出实例。
- 2、更多信息请参考API Explorer。
版本说明
本示例基于华为云SDK V1.0版本开发,高版本示例正在开发中。
功能介绍
弹性伸缩实例指弹性伸缩组中承担业务流量的ECS实例。本示例介绍了实例的基本操作,您还可以对实例进行实例保护、实例备用操作。
准备工作
弹性伸缩实例管理
初始化客户端
参数获取可参照准备工作中token认证方式。
String user = "replace-with-your-username"; String password = "replace-with-your-password"; String projectId = "replace-with-your-projectId"; String userDomainId = "replace-with-your-domainId"; String authUrl = "https://iam.example.com/v3"; // endpointUrl OSClientV3 osclient = OSFactory.builderV3().endpoint(authUrl) .credentials(user, password, Identifier.byId(userDomainId)) .scopeToProject(Identifier.byId(projectId)).authenticate();
查询弹性伸缩组中的实例列表
根据输入条件过滤查询弹性伸缩组中实例信息。查询结果分页显示。若不加过滤条件默认查询组内最多20条实例信息。
// List all scalingInstances public void listInstances(String groupId) { List< ? extends ScalingGroupInstance> listInstances = osclient.autoScaling().groupInstances().list(groupId); if (null != listInstances) { System.out.println("List all scalingInstances success, size is = " + listInstances.size()); } else { System.out.println("get scalingInstances failed"); } }
说明
:可根据实例在伸缩组中的生命周期状态,实例健康状态,实例保护状态,起始行号,记录条数进行条件过滤查询。更多信息请参考API Explorer。批量操作实例
批量移出伸缩组中的实例或批量添加伸缩组外的实例
// Batch operate scalingInstances public void batchOperateInstances(String groupId) throws InterruptedException { ArrayList<String> instanceIds = Lists.newArrayList("******"); // Batch remove scalingInstances ActionResponse resp = osclient.autoScaling().groupInstances().batchRemove(groupId, instanceIds, false); if (resp.isSuccess()) { System.out.println("batchAdd scalingInstances success"); } else { System.out.println("batchAdd scalingInstances failed"); } TimeUnit.MINUTES.sleep(5); // Batch add scalingInstances ActionResponse resp1 = osclient.autoScaling().groupInstances().batchAdd(groupId, instanceIds, false); if (resp1.isSuccess()) { System.out.println("batchRemove scalingInstances success"); } else { System.out.println("batchRemove scalingInstances failed"); } }
说明
:移除弹性伸缩组实例
从弹性伸缩组中移出一个指定实例
// Delete a scalingInstance public void deleteInstance() { String instanceId = "******"; ActionResponse resp = osclient.autoScaling().groupInstances().delete(instanceId, false); if (resp.isSuccess()) { System.out.println("Delete scalingInstance success"); } else { System.out.println("Delete scalingInstance failed"); } }
说明
:参考
更多信息请参考弹性伸缩 AS。