文档首页> > 开发指南> Java> AS Java SDK示例

AS Java SDK示例

更新时间: 2018-11-26 14:47

创建伸缩组

伸缩组是具有相同应用场景配置的实例集合。伸缩组内定义了实例最大数量和最小数量等信息。

可以参考以下代码创建弹性伸缩组,其中子网(Network)、安全组(Security Group)、VPC为必选参数,在创建伸缩组之前必须先创建VPC,以及同一个VPC下的子网(Network)、安全组(Security Group)。

private String createScalingGroup() {  
//network
     IdResourceEntity network = new IdResourceEntity();
     network.setId("fd329aab-d33a-436c-abcf-9ccd4082b2e3");  
//securityGroup
     IdResourceEntity securityGroup = new IdResourceEntity();
     securityGroup.setId("57f0a6cd-c427-4e40-a9a2-301ca90893fd");  
//az
     String availabilityZone = "eu-de-01";  
//group
     ASAutoScalingGroupCreate group = ASAutoScalingGroupCreate.builder().groupName("test-4-bill")
           .vpcId("0bbc9614-1209-438f-83bb-572b3ad475ea").networks(Lists.newArrayList(network))
           .configId("834b1d4a-36a7-4713-8ec3-41da28f12957").securityGroups(Lists.newArrayList(securityGroup))
           .maxInstanceNumber(2).minInstanceNumber(1).desireInstanceNumber(1).coolDownTime(800)
           .lbListenerId("4e4f42f1ff004cbdac61f034c9cdfde8")           .lbListenerId("9ece3b458dd14ce6a15b09073855402e")
           .availabilityZones(Lists.newArrayList(availabilityZone))
           .healthPeriodicAuditMethod(HealthPeriodicAuditMethod.NOVA_AUDIT)
           .healthPeriodicAuditTime(15)
           .instanceTerminatePolicy(InstanceTerminatePolicy.OLD_INSTANCE)
           .deletePublicip(true)
           .build();  
//creat group
     ScalingGroupCreate result = osclient.autoScaling().groups().create(group);
     Assert.assertNotNull(result.getGroupId());
     return result.getGroupId();  }

创建伸缩配置

伸缩配置定义了用于创建弹性伸缩组中实例的配置。AutoScaling为某个伸缩组自动添加实例时,会根据配置创建实例。

可以参考以下代码创建弹性伸缩组配置,当使用已存在的云服务器的规格为模板创建弹性伸缩配置时传入instance_id字段,此时flavorRef、imageRef、disk字段不生效。当不传入instance_id字段时flavorRef、imageRef、disk字段为必选。

private String createScalingConfig () {
        String keyname = "KeyPair-0406-as";
        Map<String, String> metaData = Maps.newHashMap();
        metaData.put("key1", "val1");
        metaData.put("key2", "val2");
        Disk disk = Disk.builder().size(40).volumeType(VolumeType.SATA).diskType(DiskType.SYS).build();
//eip
        Bandwidth build = Bandwidth.builder().chargingMode(Bandwidth.ChargingMode.TRAFFIC).shareType(Bandwidth.ShareType.PER).size("100").build();
        Eip eip = Eip.builder().ipType("BGP5").bandwidth(build).build();
        PublicIp publicIp = PublicIp.builder().eip(eip).build();
//instanceConfig
        InstanceConfig instanceConfig = InstanceConfig.builder().instanceId("e926dffb-6fc5-4f8e-b2ff-b2bffe82efb9")
              .flavorRef("c2.medium").imageRef("e215580f-73ad-429d-b6f2-5433947433b0").disks(Lists.newArrayList(disk))
              .keyName(keyname)
              .metadata(metaData)
              .publicIp(publicIp)
              .userData("fegrhtht").build();
//createScalingConfig
        ScalingConfigCreate config = ASAutoScalingConfigCreate.builder().configName("test-config-name")
              .instanceConfig(instanceConfig).build();
        ScalingConfigCreate result = osclient.autoScaling().configs().create(config);
        assertNotNull(result.getConfigId());
        return result.getConfigId();  }

创建伸缩策略

支持三种伸缩策略类型:定时,周期,告警 ,如果选择告警策略,则选择或者创建的告警策略只能关联一个弹性伸缩组。

以下代码提供了创建定时,周期和告警策略,其中周期策略还分为Daily、Weekly和Monthly三种类型。

private void testCreateAutoScalingPolicy() {
     String groupId = "3545d5a1-2d8c-4370-8b95-36f2e8133c24";
//RECURRENCE Daily
   ScheduledPolicy scheduledPolicyDaily = ScheduledPolicy.builder().launchTime("01:21")
           .recurrenceType(RecurrenceType.DAILY).endTime(getEndTime()).recurrenceValue(null).build();
     ScalingPolicyCreateUpdate policyDaily = ASAutoScalingPolicyCreateUpdate.builder().policyName("SDK-policyName")
           .groupId(groupId).policyType(ScalingPolicyType.RECURRENCE).scheduledPolicy(scheduledPolicyDaily).coolDownTime(800)
           .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.ADD).build())
           .build();
     ScalingPolicyCreateUpdate createDaily = osclient.autoScaling().policies().create(policyDaily);
     assertTrue(createDaily != null && !Strings.isNullOrEmpty(createDaily.getPolicyId()));
//RECURRENCE1 Weekly
    ScheduledPolicy scheduledPolicyWeekly = ScheduledPolicy.builder().launchTime("01:21")
           .recurrenceType(RecurrenceType.WEEKLY).startTime(getStartTime()).endTime(getEndTime()).recurrenceValue("1,2,3").build();
     ScalingPolicyCreateUpdate policyWeekly = ASAutoScalingPolicyCreateUpdate.builder().policyName("SDK-policyName")
           .groupId(groupId).policyType(ScalingPolicyType.RECURRENCE).scheduledPolicy(scheduledPolicyWeekly).coolDownTime(800)
           .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.ADD).instanceNumber(1).build())
           .build();
     ScalingPolicyCreateUpdate createWeekly = osclient.autoScaling().policies().create(policyWeekly);
     assertTrue(createWeekly != null && !Strings.isNullOrEmpty(createWeekly.getPolicyId()));
//RECURRENCE1 Monthly
     ScheduledPolicy scheduledPolicyMonthly = ScheduledPolicy.builder().launchTime("01:21")
           .recurrenceType(RecurrenceType.MONTHLY).startTime(getStartTime()).endTime(getEndTime()).recurrenceValue("1,2,3,10").build();
     ScalingPolicyCreateUpdate policyMonthly = ASAutoScalingPolicyCreateUpdate.builder().policyName("SDK-policyName")
           .groupId(groupId).policyType(ScalingPolicyType.RECURRENCE).scheduledPolicy(scheduledPolicyMonthly).coolDownTime(800)
           .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.ADD).instanceNumber(1).build())
           .build();
     ScalingPolicyCreateUpdate createMonthly = osclient.autoScaling().policies().create(policyWeekly0);
     assertTrue(createMonthly != null && !Strings.isNullOrEmpty(createMonthly.getPolicyId()));
//SCHEDULED
      ScheduledPolicy scheduledPolicyScheduled = ScheduledPolicy.builder().launchTime("2017-07-24T01:21Z").build();
     ScalingPolicyCreateUpdate policyScheduled = ASAutoScalingPolicyCreateUpdate.builder().policyName("policyTestName")
           .groupId(groupId).policyType(ScalingPolicyType.SCHEDULED).scheduledPolicy(scheduledPolicy1).coolDownTime(800)
     .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.ADD).instanceNumber(1).build())
     .build();
     ScalingPolicyCreateUpdate createScheduled = osclient.autoScaling().policies().create(policyScheduled);
     assertTrue(createScheduled != null && !Strings.isNullOrEmpty(createScheduled.getPolicyId()));
//ALARM
      ScalingPolicyCreateUpdate policyAlarm = ASAutoScalingPolicyCreateUpdate.builder().policyName("policyTestName")
           .groupId(groupId).policyType(ScalingPolicyType.ALARM).alarmId("al1499772396965q7BBl9MpR").coolDownTime(800)
           .scalingPolicyAction(ScalingPolicyAction.builder().operation(Operation.REMOVE).instanceNumber(1).build())
           .build();
     ScalingPolicyCreateUpdate createAlarm = osclient.autoScaling().policies().create(policyAlarm);
     assertTrue(createAlarm!= null && !Strings.isNullOrEmpty(createAlarm.getPolicyId()));  }

创建生命周期挂钩

为伸缩组添加生命周期挂钩,目的是当伸缩组进行伸缩活动时,实例将被生命周期挂钩挂起并置于等待状态(正在加入伸缩组或正在移出伸缩组),实例将保持此状态直至超时时间结束或者用户手动回调。

代码参考:

import com.huawei.openstack4j.openstack.scaling.domain.ASAutoScalingLifecycleHook;
import com.huawei.openstack4j.openstack.scaling.domain.ASAutoScalingLifecycleHookType;

//create lifecycle_hook
public ASAutoScalingLifecycleHook creatLifeHook() {

  String groupId = "36f3ac20-e8cf-4c0c-ab4f-78b092b74192";
  String lifecycleHookName = "test-hook";?
  ASAutoScalingLifecycleHookType lifecycleHookType = ASAutoScalingLifecycleHookType.INSTANCE_LAUNCHING;
  ASAutoScalingDefaultResult defaultResult = ASAutoScalingDefaultResult.ABANDON;
  String notificationTopicUrn = "urn:smn:cn-suzhou2-1:ebac0c927c104c4587687ce375d0b656:as_test";
  String notificationMetadata = "xxxxxxxx";

  ASAutoScalingLifecycleHook lifecycleHook = ASAutoScalingLifecycleHook.builder().lifecycleHookName(lifecycleHookName).lifecycleHookType(lifecycleHookType.defaultResult(defaultResult).defaultTimeout("86400").notificationTopicUrn(notificationTopicUrn).notificationMetadata(notificationMetadata).build();

  ASAutoScalingLifecycleHook hook = osclient.autoScaling().lifecycleHook().create(lifecycleHook, groupId);
  return hook;
 }

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

在文档使用中是否遇到以下问题







请至少选择或填写一项反馈信息

字符长度不能超过100

反馈内容不能为空!

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

跳转到云社区
点我,12·12惊喜等着你哦~