全部产品
云市场

Java SDK 使用示例

更新时间:2020-01-17 11:13:23

准备

安装Java SDK

概要: 本文介绍如何使用ECI Java SDK以及阿里云核心库。

您可以自行下载aliyun-java-sdk-eci;也可以使用Maven坐标来使用SDK(推荐使用)。

Java SDK 的Maven地址:https://search.maven.org/search?q=a:aliyun-java-sdk-eci

通过使用Maven坐标来使用SDK,只需在 Maven pom.xml 添加以下依赖项即可:

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-eci</artifactId>
  4. <version>1.0.11</version> <!-- 注:这里是示例版本号(可用)-->
  5. </dependency>

使用示例

公共部分

  1. import com.alibaba.fastjson.JSON;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.aliyuncs.AcsRequest;
  4. import com.aliyuncs.AcsResponse;
  5. import com.aliyuncs.DefaultAcsClient;
  6. import com.aliyuncs.IAcsClient;
  7. import com.aliyuncs.RpcAcsRequest;
  8. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupFromTemplateRequest;
  9. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupFromTemplateResponse;
  10. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.*;
  11. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest;
  12. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.DnsConfig.*;
  13. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.Volume.*;
  14. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.Container.*;
  15. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupResponse;
  16. import com.aliyuncs.eci.model.v20180808.DeleteContainerGroupRequest;
  17. import com.aliyuncs.eci.model.v20180808.DeleteContainerGroupResponse;
  18. import com.aliyuncs.eci.model.v20180808.DescribeContainerGroupPriceRequest;
  19. import com.aliyuncs.eci.model.v20180808.DescribeContainerGroupsRequest;
  20. import com.aliyuncs.eci.model.v20180808.DescribeContainerGroupsResponse;
  21. import com.aliyuncs.eci.model.v20180808.ExecContainerCommandRequest;
  22. import com.aliyuncs.eci.model.v20180808.UpdateContainerGroupRequest;
  23. import com.aliyuncs.eci.model.v20180808.UpdateContainerGroupResponse;
  24. import com.aliyuncs.exceptions.ClientException;
  25. import com.aliyuncs.http.FormatType;
  26. import com.aliyuncs.http.HttpResponse;
  27. import com.aliyuncs.profile.DefaultProfile;
  28. import com.aliyuncs.profile.IClientProfile;
  29. import com.aliyuncs.regions.ProductDomain;
  30. import java.util.Arrays;
  31. import java.util.Collections;
  32. public class EciTest20180808 {
  33. /**
  34. * 默认请求参数的填充
  35. *
  36. * @param request RpcAcsRequest
  37. */
  38. private static void fillParam(RpcAcsRequest request) {
  39. request.setAcceptFormat(FormatType.JSON);
  40. request.setVersion(EciTestConstant.VERSION);
  41. ProductDomain productDomain = new ProductDomain(EciTestConstant.PRODUCT, EciTestConstant.PRODUCT_DOMAIN);
  42. request.setProductDomain(productDomain);
  43. }
  44. /**
  45. * IAcsClient send the request
  46. * aliyun-sdk-core 版本要求:4.2.5
  47. * @param request
  48. * @param <T>
  49. * @return AcsResponse 返回请求对应的AcsResponse对象
  50. */
  51. private static <T extends AcsResponse> T doAction(AcsRequest<T> request) {
  52. DefaultAcsClient client = null;
  53. IClientProfile iClientProfile = DefaultProfile.getProfile(EciTestConstant.REGION_ID, EciTestConstant.AK_ID,
  54. EciTestConstant.AK_SECRET);
  55. try {
  56. DefaultProfile.addEndpoint("cn-hangzhou", EciTestConstant.REGION_ID, EciTestConstant.PRODUCT,
  57. EciTestConstant.PRODUCT_DOMAIN);
  58. } catch (ClientException e) {
  59. e.printStackTrace();
  60. }
  61. client = new DefaultAcsClient(iClientProfile);
  62. AcsResponse response = null;
  63. try {
  64. response = client.getAcsResponse(request);
  65. } catch (ClientException e) {
  66. e.printStackTrace();
  67. }
  68. return (T)response;
  69. }
  70. /**
  71. * IAcsClient send the request
  72. * @param request
  73. * @return 返回json格式
  74. */
  75. private static HttpResponse doAction2(RpcAcsRequest request) {
  76. IAcsClient iAcsClient = null;
  77. IClientProfile iClientProfile = DefaultProfile.getProfile(EciTestConstant.REGION_ID, EciTestConstant.AK_ID,
  78. EciTestConstant.AK_SECRET);
  79. try {
  80. DefaultProfile.addEndpoint("cn-hangzhou", EciTestConstant.REGION_ID, EciTestConstant.PRODUCT,
  81. EciTestConstant.PRODUCT_DOMAIN);
  82. } catch (ClientException e) {
  83. e.printStackTrace();
  84. }
  85. iAcsClient = new DefaultAcsClient(iClientProfile);
  86. HttpResponse response = null;
  87. try {
  88. response = iAcsClient.doAction(request);
  89. System.out.println("responseContent:" + new String(response.getHttpContent()));
  90. } catch (ClientException e) {
  91. e.printStackTrace();
  92. }
  93. return response;
  94. }
  95. }

实例资源配置以及AccessKey(密钥)

  1. public class EciTestConstant {
  2. public final static String PRODUCT = "Eci";
  3. public final static String PRODUCT_DOMAIN = "eci.aliyuncs.com";
  4. public final static String VERSION = "2018-08-08";
  5. public final static String AK_ID = "xxx";
  6. public final static String AK_SECRET = "xxx";
  7. public final static String REGION_ID = "cn-hangzhou";
  8. /**
  9. * 相同VPC下的交换机和安全组
  10. */
  11. public final static String VSWITCH_ID = "vsw-bp14bcjec9fyayjnf33w8";
  12. public final static String SECURITY_GROUP_ID = "sg-bp1d93bdq9cbqxp19rbs";
  13. /**
  14. * 将阿里云 NAS 服务作为 NFSVolume 挂载测试
  15. * 注:需要和ECI指定的交换机和安全组在同一个VPC下
  16. */
  17. public final static String NAS_SERVER = "0a1bf4a604-rah31.cn-hangzhou.nas.aliyuncs.com";
  18. public final static String NAS_PATH = "/";
  19. }

CreateContainerGroup

  1. public static CreateContainerGroupResponse createContainerGroup() {
  2. CreateContainerGroupRequest createContainerGroupRequest = new CreateContainerGroupRequest();
  3. fillParam(createContainerGroupRequest);
  4. createContainerGroupRequest.setActionName("CreateContainerGroup");//默认已经填充
  5. createContainerGroupRequest.setRegionId(EciTestConstant.REGION_ID);
  6. createContainerGroupRequest.setSecurityGroupId(EciTestConstant.SECURITY_GROUP_ID);
  7. createContainerGroupRequest.setVSwitchId(EciTestConstant.VSWITCH_ID);
  8. createContainerGroupRequest.setContainerGroupName("liu-test");
  9. //Tag
  10. Tag tag = new Tag();
  11. tag.setKey("java-sdk-eci");
  12. tag.setValue("v1");
  13. /**
  14. * volumes
  15. * volume1: ConfigFileVolume
  16. */
  17. Volume volume1 = new Volume();
  18. volume1.setName("default-volume1-config");
  19. volume1.setType("ConfigFileVolume");
  20. ConfigFileVolumeConfigFileToPath configFileToPath = new ConfigFileVolumeConfigFileToPath();
  21. configFileToPath.setPath("eci/container-config");
  22. configFileToPath.setContent("eci-config");
  23. volume1.setConfigFileVolumeConfigFileToPaths(Collections.singletonList(configFileToPath));
  24. /**
  25. * volumes
  26. * volume2: NFSVolume
  27. */
  28. Volume volume2 = new Volume();
  29. volume2.setType("NFSVolume");
  30. volume2.setName("default-volume2-nfs");
  31. volume2.setNFSVolumePath(EciTestConstant.NAS_PATH);
  32. volume2.setNFSVolumeServer(EciTestConstant.NAS_SERVER);
  33. volume2.setNFSVolumeReadOnly(false);
  34. /**
  35. * volumes
  36. * volume3: EmptyDirVolume
  37. */
  38. Volume volume3 = new Volume();
  39. volume3.setType("EmptyDirVolume");
  40. volume3.setName("default-volume3-empty");
  41. //securityContext
  42. SecurityContext securityContext = new SecurityContext();
  43. securityContext.setReadOnlyRootFilesystem(true);
  44. securityContext.setRunAsUser(1337L);
  45. Capability capability = new Capability();
  46. capability.setAdds(Arrays.asList("NET_ADMIN"));
  47. securityContext.setCapability(capability);
  48. /**
  49. * initContainer busyBox
  50. */
  51. Container initContainer = new Container();
  52. initContainer.setName("centos-liu-init");
  53. initContainer.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0");
  54. initContainer.setMemory(2F);
  55. initContainer.setCpu(1.0F);
  56. initContainer.setImagePullPolicy("Always");
  57. //initContainer.setSecurityContext(securityContext);
  58. createContainerGroupRequest.setInitContainers(Arrays.asList(initContainer));
  59. /**
  60. * container1 share data with container2 through EmptyDirVolume
  61. */
  62. Container container = new Container();
  63. container.setName("nginx-liu");
  64. container.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/nginx:alpine");
  65. container.setMemory(2F);
  66. container.setCpu(1F);
  67. container.setImagePullPolicy("Always");
  68. VolumeMount volumeMount = new VolumeMount();
  69. volumeMount.setName("default-volume3-empty");
  70. volumeMount.setMountPath("/usr/share/nginx/html");
  71. volumeMount.setReadOnly(false);
  72. container.setVolumeMounts(Collections.singletonList(volumeMount));
  73. //probe
  74. ContainerProbe readinessProbe = new ContainerProbe();
  75. readinessProbe.setTcpSocketPort(80);
  76. readinessProbe.setInitialDelaySeconds(5);
  77. readinessProbe.setPeriodSeconds(3);
  78. readinessProbe.setFailureThreshold(3);
  79. readinessProbe.setSuccessThreshold(3);
  80. readinessProbe.setTimeoutSeconds(10);
  81. ContainerProbe livenessProbe = new ContainerProbe();
  82. livenessProbe.setTcpSocketPort(80);
  83. livenessProbe.setInitialDelaySeconds(5);
  84. livenessProbe.setPeriodSeconds(3);
  85. livenessProbe.setFailureThreshold(3);
  86. livenessProbe.setSuccessThreshold(1);
  87. livenessProbe.setTimeoutSeconds(10);
  88. container.setReadinessProbe(readinessProbe);
  89. container.setLivenessProbe(livenessProbe);
  90. //container.setSecurityContext(securityContext);
  91. /**
  92. * container 2 centOS mount both NFSVolume(NAS) and EmptyDirVolume
  93. */
  94. Container container2 = new Container();
  95. container2.setName("centos-liu");
  96. container2.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0");
  97. container2.setMemory(2F);
  98. container2.setCpu(1.0F);
  99. container2.setImagePullPolicy("Always");
  100. VolumeMount volumeMount2 = new VolumeMount();
  101. volumeMount2.setName("default-volume2-nfs");
  102. volumeMount2.setMountPath("/pod-data");
  103. volumeMount2.setReadOnly(false);
  104. VolumeMount volumeMount3 = new VolumeMount();
  105. volumeMount3.setName("default-volume3-empty");
  106. volumeMount3.setMountPath("/usr/share");
  107. volumeMount3.setReadOnly(false);
  108. container2.setVolumeMounts(Arrays.asList(volumeMount2, volumeMount3));
  109. //copy the file by shell command in Container2
  110. container2.setCommands(Arrays.asList("/bin/sh"));
  111. container2.setArgs(Arrays.asList("-c", "while true; do cp /pod-data/index.html /usr/share/; sleep 100; done"));
  112. //dns config
  113. DnsConfig dnsConfig = new DnsConfig();
  114. dnsConfig.setNameServers(Arrays.asList("8.8.8.8"));
  115. dnsConfig.setSearches(Arrays.asList("ns.kubelete.local"));
  116. DnsConfigOption option = new DnsConfigOption();
  117. option.setName("name");
  118. option.setValue("value");
  119. dnsConfig.setOptions(Arrays.asList(option));
  120. HostAliase hostAliase = new HostAliase();
  121. hostAliase.setIp("196.128.0.10");
  122. hostAliase.setHostnames(Arrays.asList("test1.com", "test2.com"));
  123. createContainerGroupRequest.setDnsConfig(dnsConfig);
  124. createContainerGroupRequest.setHostAliases(Arrays.asList(hostAliase));
  125. createContainerGroupRequest.setTags(Collections.singletonList(tag));
  126. createContainerGroupRequest.setVolumes(Arrays.asList(volume1, volume2, volume3));
  127. createContainerGroupRequest.setContainers(Arrays.asList(container, container2));
  128. return doAction(createContainerGroupRequest);
  129. }

UpdateContainerGroup

  1. public static UpdateContainerGroupResponse updateContainerGroup(String containerGroupId) {
  2. UpdateContainerGroupRequest updateContainerGroupRequest = new UpdateContainerGroupRequest();
  3. fillParam(updateContainerGroupRequest);
  4. updateContainerGroupRequest.setActionName("UpdateContainerGroup");//默认已经填充
  5. updateContainerGroupRequest.setRegionId(EciTestConstant.REGION_ID);
  6. updateContainerGroupRequest.setContainerGroupId(containerGroupId);
  7. //Tag
  8. Tag tag = new Tag();
  9. tag.setKey("java-sdk-eci");
  10. tag.setValue("v2");
  11. /**
  12. * volumes
  13. * volume1: ConfigFileVolume
  14. */
  15. Volume volume1 = new Volume();
  16. volume1.setName("default-volume1-config");
  17. volume1.setType("ConfigFileVolume");
  18. ConfigFileVolumeConfigFileToPath configFileToPath = new ConfigFileVolumeConfigFileToPath();
  19. configFileToPath.setPath("eci/container-config");
  20. configFileToPath.setContent("eci-config");
  21. volume1.setConfigFileVolumeConfigFileToPaths(Collections.singletonList(configFileToPath));
  22. /**
  23. * volumes
  24. * volume2: NFSVolume
  25. */
  26. Volume volume2 = new Volume();
  27. volume2.setType("NFSVolume");
  28. volume2.setName("default-volume2-nfs");
  29. volume2.setNFSVolumePath(EciTestConstant.NAS_PATH);
  30. volume2.setNFSVolumeServer(EciTestConstant.NAS_SERVER);
  31. volume2.setNFSVolumeReadOnly(false);
  32. /**
  33. * volumes
  34. * volume3: EmptyDirVolume
  35. */
  36. Volume volume3 = new Volume();
  37. volume3.setType("EmptyDirVolume");
  38. volume3.setName("default-volume3-empty");
  39. //securityContext
  40. SecurityContext securityContext = new SecurityContext();
  41. securityContext.setReadOnlyRootFilesystem(true);
  42. securityContext.setRunAsUser(1337L);
  43. Capability capability = new Capability();
  44. capability.setAdds(Arrays.asList("NET_ADMIN"));
  45. securityContext.setCapability(capability);
  46. /**
  47. * initContainer busyBox
  48. */
  49. Container initContainer = new Container();
  50. initContainer.setName("centos-liu-init");
  51. initContainer.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0");
  52. initContainer.setMemory(2F);
  53. initContainer.setCpu(1.0F);
  54. initContainer.setImagePullPolicy("Always");
  55. //initContainer.setSecurityContext(securityContext);
  56. updateContainerGroupRequest.setInitContainers(Arrays.asList(initContainer));
  57. /**
  58. * container1 share data with container2 through EmptyDirVolume
  59. */
  60. Container container = new Container();
  61. container.setName("nginx-liu");
  62. container.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/nginx:alpine");
  63. container.setMemory(1F);
  64. container.setCpu(0.5F);
  65. container.setImagePullPolicy("Always");
  66. VolumeMount volumeMount = new VolumeMount();
  67. volumeMount.setName("default-volume3-empty");
  68. volumeMount.setMountPath("/usr/share/nginx/html");
  69. volumeMount.setReadOnly(false);
  70. container.setVolumeMounts(Collections.singletonList(volumeMount));
  71. //probe
  72. ContainerProbe readinessProbe = new ContainerProbe();
  73. readinessProbe.setTcpSocketPort(80);
  74. readinessProbe.setInitialDelaySeconds(5);
  75. readinessProbe.setPeriodSeconds(3);
  76. readinessProbe.setFailureThreshold(3);
  77. readinessProbe.setSuccessThreshold(3);
  78. readinessProbe.setTimeoutSeconds(10);
  79. ContainerProbe livenessProbe = new ContainerProbe();
  80. livenessProbe.setTcpSocketPort(80);
  81. livenessProbe.setInitialDelaySeconds(5);
  82. livenessProbe.setPeriodSeconds(3);
  83. livenessProbe.setFailureThreshold(3);
  84. livenessProbe.setSuccessThreshold(1);
  85. livenessProbe.setTimeoutSeconds(10);
  86. container.setReadinessProbe(readinessProbe);
  87. container.setLivenessProbe(livenessProbe);
  88. //container.setSecurityContext(securityContext);
  89. /**
  90. * container 2 centOS mount both NFSVolume(NAS) and EmptyDirVolume
  91. */
  92. Container container2 = new Container();
  93. container2.setName("centos-liu");
  94. container2.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0");
  95. container2.setMemory(1F);
  96. container2.setCpu(0.5F);
  97. container2.setImagePullPolicy("Always");
  98. VolumeMount volumeMount2 = new VolumeMount();
  99. volumeMount2.setName("default-volume2-nfs");
  100. volumeMount2.setMountPath("/pod-data");
  101. volumeMount2.setReadOnly(false);
  102. VolumeMount volumeMount3 = new VolumeMount();
  103. volumeMount3.setName("default-volume3-empty");
  104. volumeMount3.setMountPath("/usr/share");
  105. volumeMount3.setReadOnly(false);
  106. container2.setVolumeMounts(Arrays.asList(volumeMount2, volumeMount3));
  107. //copy the file by shell command in Container2
  108. container2.setCommands(Arrays.asList("/bin/sh"));
  109. container2.setArgs(Arrays.asList("-c", "while true; do cp /pod-data/index.html /usr/share/; sleep 100; done"));
  110. //dns config
  111. DnsConfig dnsConfig = new DnsConfig();
  112. dnsConfig.setNameServers(Arrays.asList("8.8.8.8"));
  113. dnsConfig.setSearches(Arrays.asList("ns.kubelete.local"));
  114. DnsConfigOption option = new DnsConfigOption();
  115. option.setName("name");
  116. option.setValue("value");
  117. dnsConfig.setOptions(Arrays.asList(option));
  118. updateContainerGroupRequest.setDnsConfig(dnsConfig);
  119. updateContainerGroupRequest.setTags(Collections.singletonList(tag));
  120. updateContainerGroupRequest.setVolumes(Arrays.asList(volume1, volume2, volume3));
  121. updateContainerGroupRequest.setContainers(Arrays.asList(container, container2));
  122. return doAction(updateContainerGroupRequest);
  123. }

DescribeContainerGroups

  1. public static DescribeContainerGroupsResponse describeContainerGroups(String[] containerGroupIds) {
  2. DescribeContainerGroupsRequest describeContainerGroupsRequest = new DescribeContainerGroupsRequest();
  3. fillParam(describeContainerGroupsRequest);
  4. describeContainerGroupsRequest.setActionName("DescribeContainerGroups");//默认已经填充
  5. describeContainerGroupsRequest.setRegionId(EciTestConstant.REGION_ID);
  6. describeContainerGroupsRequest.setStatus("Running");
  7. describeContainerGroupsRequest.setContainerGroupIds(JSONObject.toJSONString(containerGroupIds));
  8. return doAction(describeContainerGroupsRequest);
  9. }

ExecContainerCommand

  1. public static HttpResponse execContainerCommand(String regionId, String containerGroupId, String containerName, String coomand){
  2. ExecContainerCommandRequest execContainerCommandRequest = new ExecContainerCommandRequest();
  3. execContainerCommandRequest.setRegionId(regionId);
  4. execContainerCommandRequest.setContainerGroupId(containerGroupId);
  5. execContainerCommandRequest.setContainerName(containerName);
  6. execContainerCommandRequest.setCommand(coomand);
  7. return doAction2(execContainerCommandRequest);
  8. }

DescribeContainerGroupPrice

  1. public static HttpResponse describeContainerGroupPrice(String regionId, Float cpu, Float mem){
  2. DescribeContainerGroupPriceRequest describeContainerGroupPriceRequest = new DescribeContainerGroupPriceRequest();
  3. describeContainerGroupPriceRequest.setRegionId(regionId);
  4. describeContainerGroupPriceRequest.setCpu(cpu);
  5. describeContainerGroupPriceRequest.setMemory(mem);
  6. return doAction2(describeContainerGroupPriceRequest);
  7. }

DeleteContainerGroup

  1. public static DeleteContainerGroupResponse deleteContainerGroup(String containerGroupId) {
  2. DeleteContainerGroupRequest deleteContainerGroupRequest = new DeleteContainerGroupRequest();
  3. fillParam(deleteContainerGroupRequest);
  4. deleteContainerGroupRequest.setActionName("DeleteContainerGroup");//默认已经填充
  5. deleteContainerGroupRequest.setContainerGroupId(containerGroupId);
  6. return doAction(deleteContainerGroupRequest);
  7. }

CreateContainerGroupFromTemplate

  1. public static CreateContainerGroupFromTemplateResponse createfromTemplate() {
  2. CreateContainerGroupFromTemplateRequest createContainerGroupFromTemplateRequest = new CreateContainerGroupFromTemplateRequest();
  3. createContainerGroupFromTemplateRequest.setTemplate("{\"ResourceGroupId\": \"rg-aekzh43vbot2abx\", \"RegionId\": \"cn-hangzhou\", \"ContainerGroupName\": \"template-test\", \"Tags\": [{\"key\": \"template\", \"value\": \"template\"}], \"EipInstanceId\": \"\", \"VSwitchId\": \"vsw-bp1jrgfqqy54kg5hcf8bc\", \"SecurityGroupId\": \"sg-bp118knl07ymorgzfhyw\", \"Spec\": {\"DnsPolicy\": \"ClusterFirst\", \"DnsConfig\": {\"Searches\": [\"c7a1790b3f206498382cbba682c257b0e.svc.cluster.local\", \"svc.cluster.local\", \"cluster.local\"]}, \"hostAliases\": [{\"ip\": \"127.0.0.1\", \"hostnames\": [\"www.test2.com\", \"www.test.com\"]}, {\"ip\": \"127.0.0.1\", \"hostnames\": [\"www.test3.com\"]}], \"RestartPolicy\": \"Always\", \"Volumes\": [{\"EmptyDir\": {\"SizeLimit\": \"1024Mi\"}, \"Name\": \"empty-volume\"}], \"InitContainers\": [{\"Image\": \"registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0\", \"Name\": \"busybox\", \"Resources\": {\"Cpu\": 1.35, \"Memory\": 3.67}, \"ImagePullPolicy\": \"Always\"}], \"Containers\": [{\"restartCount\": 5, \"Name\": \"nginx\", \"Env\": [{\"Name\": \"KUBERNETES_SERVICE_HOST\", \"Value\": \"47.xx.32.xx\"}, {\"Name\": \"KUBERNETES_SERVICE_PORT\", \"Value\": \"6443\"}], \"ImagePullPolicy\": \"Always\", \"Image\": \"registry-vpc.cn-hangzhou.aliyuncs.com/liumi/nginx:alpine\", \"VolumeMounts\": [{\"ReadOnly\": false, \"MountPath\": \"/pod/data\", \"SubPath\": \"\", \"Name\": \"empty-volume\"}], \"Ports\": [{\"Protocol\": \"TCP\", \"ContainerPort\": 8080}, {\"Protocol\": \"TCP\", \"ContainerPort\": 80}], \"Resources\": {\"Cpu\": 1.35, \"Memory\": 3.67}, \"securityContextPrivileged\": true}]}, \"Resources\": {\"Cpu\": 2, \"Memory\": 4}, \"ZoneId\": \"cn-hangzhou-h\"}");
  4. return doAction(createContainerGroupFromTemplateRequest);
  5. }

ExportContainerGroupTemplate

  1. private static ExportContainerGroupTemplateResponse exportContainerGroupTemplate(String regionId, String containerGroupId) {
  2. ExportContainerGroupTemplateRequest exportContainerGroupTemplateRequest = new ExportContainerGroupTemplateRequest();
  3. exportContainerGroupTemplateRequest.setRegionId(regionId);
  4. exportContainerGroupTemplateRequest.setContainerGroupId(containerGroupId);
  5. return doAction(exportContainerGroupTemplateRequest);
  6. }

CreateImageCache

  1. private static CreateImageCacheResponse createImageCache(String regionId, String imageCacheName) {
  2. CreateImageCacheRequest createImageCacheRequest = new CreateImageCacheRequest();
  3. createImageCacheRequest.setImageCacheName(imageCacheName);
  4. createImageCacheRequest.setVSwitchId(EciTestConstant.VSWITCH_ID);
  5. createImageCacheRequest.setSecurityGroupId(EciTestConstant.SECURITY_GROUP_ID);
  6. createImageCacheRequest.setImages(Arrays.asList("nginx", "busybox"));
  7. createImageCacheRequest.setImageCacheSize(20);
  8. return doAction(createImageCacheRequest);
  9. }

DescribeImageCaches

  1. private static DescribeImageCachesResponse describeImageCaches(String regionId, String imageCacheId) {
  2. DescribeImageCachesRequest describeImageCachesRequest = new DescribeImageCachesRequest();
  3. describeImageCachesRequest.setRegionId(regionId);
  4. describeImageCachesRequest.setImageCacheId(imageCacheId);
  5. return doAction(describeImageCachesRequest);
  6. }

Demo示例

创建一个ECI实例

  1. package com.aliyuncs.test;
  2. import com.alibaba.fastjson.JSON;
  3. import com.aliyuncs.AcsRequest;
  4. import com.aliyuncs.AcsResponse;
  5. import com.aliyuncs.DefaultAcsClient;
  6. import com.aliyuncs.IAcsClient;
  7. import com.aliyuncs.RpcAcsRequest;
  8. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.*;
  9. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest;
  10. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.Container.*;
  11. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupResponse;
  12. import com.aliyuncs.exceptions.ClientException;
  13. import com.aliyuncs.http.FormatType;
  14. import com.aliyuncs.http.HttpResponse;
  15. import com.aliyuncs.profile.DefaultProfile;
  16. import com.aliyuncs.profile.IClientProfile;
  17. import com.aliyuncs.regions.ProductDomain;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. public class CreateTest {
  21. public static void main(String[] args){
  22. CreateContainerGroupResponse response = createContainerGroup();
  23. System.out.println(JSON.toJSONString(response));
  24. }
  25. public static CreateContainerGroupResponse createContainerGroup() {
  26. CreateContainerGroupRequest createContainerGroupRequest = new CreateContainerGroupRequest();
  27. fillParam(createContainerGroupRequest);
  28. createContainerGroupRequest.setActionName("CreateContainerGroup");//默认已经填充
  29. createContainerGroupRequest.setRegionId(EciTestConstant.REGION_ID);
  30. createContainerGroupRequest.setSecurityGroupId(EciTestConstant.SECURITY_GROUP_ID);
  31. createContainerGroupRequest.setVSwitchId(EciTestConstant.VSWITCH_ID);
  32. createContainerGroupRequest.setContainerGroupName("create-test");
  33. //Tag
  34. Tag tag = new Tag();
  35. tag.setKey("java-sdk-eci");
  36. tag.setValue("create-test");
  37. //volume:EmptyVolume
  38. Volume volume = new Volume();
  39. volume.setType("EmptyDirVolume");
  40. volume.setName("emptyVolume");
  41. //Container
  42. Container container = new Container();
  43. container.setName("test-hxh");
  44. container.setImage("nginx");
  45. container.setCpu(2F);
  46. container.setMemory(4F);
  47. //Container VolumeMount
  48. VolumeMount volumeMount = new VolumeMount();
  49. volumeMount.setName("emptyVolume");
  50. volumeMount.setMountPath("/usr/share/");
  51. container.setVolumeMounts(Collections.singletonList(volumeMount));
  52. createContainerGroupRequest.setTags(Arrays.asList(tag));
  53. createContainerGroupRequest.setVolumes(Arrays.asList(volume));
  54. createContainerGroupRequest.setContainers(Arrays.asList(container));
  55. return doAction(createContainerGroupRequest);
  56. }
  57. /**
  58. * 默认请求参数的填充
  59. *
  60. * @param request RpcAcsRequest
  61. */
  62. private static void fillParam(RpcAcsRequest request) {
  63. request.setAcceptFormat(FormatType.JSON);
  64. request.setVersion(EciTestConstant.VERSION);
  65. ProductDomain productDomain = new ProductDomain(EciTestConstant.PRODUCT, EciTestConstant.PRODUCT_DOMAIN);
  66. request.setProductDomain(productDomain);
  67. }
  68. /**
  69. * IAcsClient send the request
  70. * aliyun-sdk-core 版本要求:4.2.5
  71. * @param request
  72. * @param <T>
  73. * @return AcsResponse 返回请求对应的AcsResponse对象
  74. */
  75. private static <T extends AcsResponse> T doAction(AcsRequest<T> request) {
  76. DefaultAcsClient client = null;
  77. IClientProfile iClientProfile = DefaultProfile.getProfile(EciTestConstant.REGION_ID, EciTestConstant.AK_ID,
  78. EciTestConstant.AK_SECRET);
  79. try {
  80. DefaultProfile.addEndpoint("cn-hangzhou", EciTestConstant.REGION_ID, EciTestConstant.PRODUCT,
  81. EciTestConstant.PRODUCT_DOMAIN);
  82. } catch (ClientException e) {
  83. e.printStackTrace();
  84. }
  85. client = new DefaultAcsClient(iClientProfile);
  86. AcsResponse response = null;
  87. try {
  88. response = client.getAcsResponse(request);
  89. } catch (ClientException e) {
  90. e.printStackTrace();
  91. }
  92. return (T)response;
  93. }
  94. /**
  95. * IAcsClient send the request
  96. * @param request
  97. * @return 返回json格式
  98. */
  99. private static HttpResponse doAction2(RpcAcsRequest request) {
  100. IAcsClient iAcsClient = null;
  101. IClientProfile iClientProfile = DefaultProfile.getProfile(EciTestConstant.REGION_ID, EciTestConstant.AK_ID,
  102. EciTestConstant.AK_SECRET);
  103. try {
  104. DefaultProfile.addEndpoint("cn-hangzhou", EciTestConstant.REGION_ID, EciTestConstant.PRODUCT,
  105. EciTestConstant.PRODUCT_DOMAIN);
  106. } catch (ClientException e) {
  107. e.printStackTrace();
  108. }
  109. iAcsClient = new DefaultAcsClient(iClientProfile);
  110. HttpResponse response = null;
  111. try {
  112. response = iAcsClient.doAction(request);
  113. System.out.println("responseContent:" + new String(response.getHttpContent()));
  114. } catch (ClientException e) {
  115. e.printStackTrace();
  116. }
  117. return response;
  118. }
  119. }

返回数据

  1. {"containerGroupId":"eci-bp1h4oxf96kajk0kdfl1","requestId":"8E93976E-E73B-410F-B048-F6E8488E6D51"}

删除指定ECI实例

  1. package com.aliyuncs.test;
  2. import com.alibaba.fastjson.JSON;
  3. import com.aliyuncs.AcsRequest;
  4. import com.aliyuncs.AcsResponse;
  5. import com.aliyuncs.DefaultAcsClient;
  6. import com.aliyuncs.IAcsClient;
  7. import com.aliyuncs.RpcAcsRequest;
  8. import com.aliyuncs.eci.model.v20180808.DeleteContainerGroupRequest;
  9. import com.aliyuncs.eci.model.v20180808.DeleteContainerGroupResponse;
  10. import com.aliyuncs.exceptions.ClientException;
  11. import com.aliyuncs.http.FormatType;
  12. import com.aliyuncs.http.HttpResponse;
  13. import com.aliyuncs.profile.DefaultProfile;
  14. import com.aliyuncs.profile.IClientProfile;
  15. import com.aliyuncs.regions.ProductDomain;
  16. public class DeleteTest {
  17. public static void main(String[] args){
  18. DeleteContainerGroupResponse response = deleteContainerGroup("eci-bp179a0jcfnfd0jd3bpl");
  19. System.out.println(JSON.toJSONString(response));
  20. }
  21. public static DeleteContainerGroupResponse deleteContainerGroup(String containerGroupId) {
  22. DeleteContainerGroupRequest deleteContainerGroupRequest = new DeleteContainerGroupRequest();
  23. fillParam(deleteContainerGroupRequest);
  24. deleteContainerGroupRequest.setActionName("DeleteContainerGroup");//默认已经填充
  25. deleteContainerGroupRequest.setContainerGroupId(containerGroupId);
  26. return doAction(deleteContainerGroupRequest);
  27. }
  28. /**
  29. * 默认请求参数的填充
  30. *
  31. * @param request RpcAcsRequest
  32. */
  33. private static void fillParam(RpcAcsRequest request) {
  34. request.setAcceptFormat(FormatType.JSON);
  35. request.setVersion(EciTestConstant.VERSION);
  36. ProductDomain productDomain = new ProductDomain(EciTestConstant.PRODUCT, EciTestConstant.PRODUCT_DOMAIN);
  37. request.setProductDomain(productDomain);
  38. }
  39. /**
  40. * IAcsClient send the request
  41. * aliyun-sdk-core 版本要求:4.2.5
  42. * @param request
  43. * @param <T>
  44. * @return AcsResponse 返回请求对应的AcsResponse对象
  45. */
  46. private static <T extends AcsResponse> T doAction(AcsRequest<T> request) {
  47. DefaultAcsClient client = null;
  48. IClientProfile iClientProfile = DefaultProfile.getProfile(EciTestConstant.REGION_ID, EciTestConstant.AK_ID,
  49. EciTestConstant.AK_SECRET);
  50. try {
  51. DefaultProfile.addEndpoint("cn-hangzhou", EciTestConstant.REGION_ID, EciTestConstant.PRODUCT,
  52. EciTestConstant.PRODUCT_DOMAIN);
  53. } catch (ClientException e) {
  54. e.printStackTrace();
  55. }
  56. client = new DefaultAcsClient(iClientProfile);
  57. AcsResponse response = null;
  58. try {
  59. response = client.getAcsResponse(request);
  60. } catch (ClientException e) {
  61. e.printStackTrace();
  62. }
  63. return (T)response;
  64. }
  65. /**
  66. * IAcsClient send the request
  67. * @param request
  68. * @return 返回json格式
  69. */
  70. private static HttpResponse doAction2(RpcAcsRequest request) {
  71. IAcsClient iAcsClient = null;
  72. IClientProfile iClientProfile = DefaultProfile.getProfile(EciTestConstant.REGION_ID, EciTestConstant.AK_ID,
  73. EciTestConstant.AK_SECRET);
  74. try {
  75. DefaultProfile.addEndpoint("cn-hangzhou", EciTestConstant.REGION_ID, EciTestConstant.PRODUCT,
  76. EciTestConstant.PRODUCT_DOMAIN);
  77. } catch (ClientException e) {
  78. e.printStackTrace();
  79. }
  80. iAcsClient = new DefaultAcsClient(iClientProfile);
  81. HttpResponse response = null;
  82. try {
  83. response = iAcsClient.doAction(request);
  84. System.out.println("responseContent:" + new String(response.getHttpContent()));
  85. } catch (ClientException e) {
  86. e.printStackTrace();
  87. }
  88. return response;
  89. }
  90. }

返回数据

  1. {"requestId":"30E08AC3-5BEE-4721-9E9C-BE0EC63AB494"}