使用 SDK for Java 2.x 的 Amazon ECS 示例 - Amazon SDK for Java 2.x
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 SDK for Java 2.x 的 Amazon ECS 示例

以下代码示例向您展示了如何在 Amazon ECS 中使用来执行操作和实现常见场景。 Amazon SDK for Java 2.x

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景 是展示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示了如何使用 CreateCluster

适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.ExecuteCommandConfiguration; import software.amazon.awssdk.services.ecs.model.ExecuteCommandLogging; import software.amazon.awssdk.services.ecs.model.ClusterConfiguration; import software.amazon.awssdk.services.ecs.model.CreateClusterResponse; import software.amazon.awssdk.services.ecs.model.EcsException; import software.amazon.awssdk.services.ecs.model.CreateClusterRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateCluster { public static void main(String[] args) { final String usage = """ Usage: <clusterName>\s Where: clusterName - The name of the ECS cluster to create. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String clusterName = args[0]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); String clusterArn = createGivenCluster(ecsClient, clusterName); System.out.println("The cluster ARN is " + clusterArn); ecsClient.close(); } public static String createGivenCluster(EcsClient ecsClient, String clusterName) { try { ExecuteCommandConfiguration commandConfiguration = ExecuteCommandConfiguration.builder() .logging(ExecuteCommandLogging.DEFAULT) .build(); ClusterConfiguration clusterConfiguration = ClusterConfiguration.builder() .executeCommandConfiguration(commandConfiguration) .build(); CreateClusterRequest clusterRequest = CreateClusterRequest.builder() .clusterName(clusterName) .configuration(clusterConfiguration) .build(); CreateClusterResponse response = ecsClient.createCluster(clusterRequest); return response.cluster().clusterArn(); } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考CreateCluster中的。

以下代码示例演示了如何使用 CreateService

适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.AwsVpcConfiguration; import software.amazon.awssdk.services.ecs.model.NetworkConfiguration; import software.amazon.awssdk.services.ecs.model.CreateServiceRequest; import software.amazon.awssdk.services.ecs.model.LaunchType; import software.amazon.awssdk.services.ecs.model.CreateServiceResponse; import software.amazon.awssdk.services.ecs.model.EcsException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateService { public static void main(String[] args) { final String usage = """ Usage: <clusterName> <serviceName> <securityGroups> <subnets> <taskDefinition> Where: clusterName - The name of the ECS cluster. serviceName - The name of the ECS service to create. securityGroups - The name of the security group. subnets - The name of the subnet. taskDefinition - The name of the task definition. """; if (args.length != 5) { System.out.println(usage); System.exit(1); } String clusterName = args[0]; String serviceName = args[1]; String securityGroups = args[2]; String subnets = args[3]; String taskDefinition = args[4]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); String serviceArn = createNewService(ecsClient, clusterName, serviceName, securityGroups, subnets, taskDefinition); System.out.println("The ARN of the service is " + serviceArn); ecsClient.close(); } public static String createNewService(EcsClient ecsClient, String clusterName, String serviceName, String securityGroups, String subnets, String taskDefinition) { try { AwsVpcConfiguration vpcConfiguration = AwsVpcConfiguration.builder() .securityGroups(securityGroups) .subnets(subnets) .build(); NetworkConfiguration configuration = NetworkConfiguration.builder() .awsvpcConfiguration(vpcConfiguration) .build(); CreateServiceRequest serviceRequest = CreateServiceRequest.builder() .cluster(clusterName) .networkConfiguration(configuration) .desiredCount(1) .launchType(LaunchType.FARGATE) .serviceName(serviceName) .taskDefinition(taskDefinition) .build(); CreateServiceResponse response = ecsClient.createService(serviceRequest); return response.service().serviceArn(); } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考CreateService中的。

以下代码示例演示了如何使用 DeleteService

适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.DeleteServiceRequest; import software.amazon.awssdk.services.ecs.model.EcsException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteService { public static void main(String[] args) { final String usage = """ Usage: <clusterName> <serviceArn>\s Where: clusterName - The name of the ECS cluster. serviceArn - The ARN of the ECS service. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String clusterName = args[0]; String serviceArn = args[1]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); deleteSpecificService(ecsClient, clusterName, serviceArn); ecsClient.close(); } public static void deleteSpecificService(EcsClient ecsClient, String clusterName, String serviceArn) { try { DeleteServiceRequest serviceRequest = DeleteServiceRequest.builder() .cluster(clusterName) .service(serviceArn) .build(); ecsClient.deleteService(serviceRequest); System.out.println("The Service was successfully deleted"); } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考DeleteService中的。

以下代码示例演示了如何使用 DescribeClusters

适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.DescribeClustersRequest; import software.amazon.awssdk.services.ecs.model.DescribeClustersResponse; import software.amazon.awssdk.services.ecs.model.Cluster; import software.amazon.awssdk.services.ecs.model.EcsException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeClusters { public static void main(String[] args) { final String usage = """ Usage: <clusterArn> \s Where: clusterArn - The ARN of the ECS cluster to describe. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String clusterArn = args[0]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); descCluster(ecsClient, clusterArn); } public static void descCluster(EcsClient ecsClient, String clusterArn) { try { DescribeClustersRequest clustersRequest = DescribeClustersRequest.builder() .clusters(clusterArn) .build(); DescribeClustersResponse response = ecsClient.describeClusters(clustersRequest); List<Cluster> clusters = response.clusters(); for (Cluster cluster : clusters) { System.out.println("The cluster name is " + cluster.clusterName()); } } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考DescribeClusters中的。

以下代码示例演示了如何使用 DescribeTasks

适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.DescribeTasksRequest; import software.amazon.awssdk.services.ecs.model.DescribeTasksResponse; import software.amazon.awssdk.services.ecs.model.EcsException; import software.amazon.awssdk.services.ecs.model.Task; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListTaskDefinitions { public static void main(String[] args) { final String usage = """ Usage: <clusterArn> <taskId>\s Where: clusterArn - The ARN of an ECS cluster. taskId - The task Id value. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String clusterArn = args[0]; String taskId = args[1]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); getAllTasks(ecsClient, clusterArn, taskId); ecsClient.close(); } public static void getAllTasks(EcsClient ecsClient, String clusterArn, String taskId) { try { DescribeTasksRequest tasksRequest = DescribeTasksRequest.builder() .cluster(clusterArn) .tasks(taskId) .build(); DescribeTasksResponse response = ecsClient.describeTasks(tasksRequest); List<Task> tasks = response.tasks(); for (Task task : tasks) { System.out.println("The task ARN is " + task.taskDefinitionArn()); } } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考DescribeTasks中的。

以下代码示例演示了如何使用 ListClusters

适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.ListClustersResponse; import software.amazon.awssdk.services.ecs.model.EcsException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListClusters { public static void main(String[] args) { Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); listAllClusters(ecsClient); ecsClient.close(); } public static void listAllClusters(EcsClient ecsClient) { try { ListClustersResponse response = ecsClient.listClusters(); List<String> clusters = response.clusterArns(); for (String cluster : clusters) { System.out.println("The cluster arn is " + cluster); } } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考ListClusters中的。

以下代码示例演示了如何使用 UpdateService

适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.EcsException; import software.amazon.awssdk.services.ecs.model.UpdateServiceRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class UpdateService { public static void main(String[] args) { final String usage = """ Usage: <clusterName> <serviceArn>\s Where: clusterName - The cluster name. serviceArn - The service ARN value. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String clusterName = args[0]; String serviceArn = args[1]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); updateSpecificService(ecsClient, clusterName, serviceArn); ecsClient.close(); } public static void updateSpecificService(EcsClient ecsClient, String clusterName, String serviceArn) { try { UpdateServiceRequest serviceRequest = UpdateServiceRequest.builder() .cluster(clusterName) .service(serviceArn) .desiredCount(0) .build(); ecsClient.updateService(serviceRequest); System.out.println("The service was modified"); } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考UpdateService中的。