全部产品
Search
文档中心

:Java SDK使用示例

更新时间:Mar 14, 2024

本文为您介绍资源编排服务ROS(Resource Orchestration Service)的Java SDK安装方法和示例代码。以查询可用地域列表、创建资源栈、查询资源栈、删除资源栈等操作为例,帮助您快速掌握资源编排接口的使用方法。

安装Java SDK

  1. 下载及安装Java SDK。

    说明
    • Java SDK的下载和使用说明,请参见SDK概览

    • 建议您使用JRE 1.8及以上版本。

  2. pom.xml中,添加依赖包。

    <dependencies>
        <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>ros20190910</artifactId>
          <version>2.2.20</version>
        </dependency>
        <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>tea-openapi</artifactId>
          <version>0.2.8</version>
        </dependency>
        <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>tea-console</artifactId>
          <version>0.0.1</version>
        </dependency>
        <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>tea-util</artifactId>
          <version>0.2.16</version>
        </dependency>
        <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>tea</artifactId>
          <version>1.1.14</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>darabonba-env</artifactId>
            <version>0.1.1</version>
         </dependency>
      	<dependency>
        		<groupId>com.aliyun</groupId>
        		<artifactId>credentials-java</artifactId>
        		<version>0.2.11</version>
    		</dependency>
      </dependencies>
  3. 初始化SDK。

    1. 导入相关的软件包。

      import com.aliyun.tea.*;
      import com.aliyun.ros20190910.*;
      import com.aliyun.ros20190910.models.*;
      import com.aliyun.teaopenapi.*;
      import com.aliyun.teaopenapi.models.*;
      import com.aliyun.darabonba.env.*;
      import com.aliyun.credentials.*;
      import java.util.*;
    2. 初始化SDK客户端对象。

      public static com.aliyun.ros20190910.Client createClient() throws Exception {
              // 使用默认凭证初始化Credentials Client
              com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
              Config config = new Config()
                      // 配置云产品服务接入地址(endpoint)
                      .setEndpoint("ros.aliyuncs.com")
                      // 使用Credential配置凭证
                      .setCredential(credentialClient);
              return new com.aliyun.ros20190910.Client(config);
          }

示例

  • 查询可用地域列表示例

    通过调用DescribeRegions接口查询可用地域列表。关于DescribeRegions接口的详情,请参见DescribeRegions

    public static void describeRegions(Client client) throws Exception {
            DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
            // 复制代码运行后,请自行打印API的返回值。
            DescribeRegionsResponse resp = client.describeRegions(describeRegionsRequest);
        }
  • 创建资源栈示例

    通过调用CreateStack接口创建资源栈。关于CreateStack接口的详情,请参见CreateStack

    在本示例中,指定了以下参数:

    • regionId:资源栈所属的地域ID。

    • stackName:资源栈名称。同一阿里云账号中资源栈名称不能重复。

    • timeoutInMinutes:创建资源栈的超时时间,单位为分钟。如果在指定时间内不能完成创建,则资源栈创建失败。

    • templateBody:模板的主体结构。

    • parameters:创建的资源栈所需要的参数。需要同时指定ParameterKey和ParameterValue。

    参数设置的实例代码如下所示。

    String stackName = "MyStack";
    Long timeoutInMinutes = 40L;
    String templateBody = "{\n" +
                          "  \"ROSTemplateFormatVersion\": \"2015-09-01\",\n" +
                          "  \"Parameters\": {\n" +
                          "    \"VpcName\": {\n" +
                          "      \"Type\": \"String\",\n" +
                          "      \"Description\": \"Vpc Name\",\n" +
                          "      \"Label\": \"Vpc Name\"\n" +
                          "    },\n" +
                          "    \"CidrBlock\": {\n" +
                          "      \"Type\": \"String\",\n" +
                          "      \"Description\": \"Vpc CidrBlock\",\n" +
                          "      \"Label\": \"Vpc CidrBlock\"\n" +
                          "    }\n" +
                          "  },\n" +
                          "  \"Resources\": {\n" +
                          "    \"Vpc\": {\n" +
                          "      \"Type\": \"ALIYUN::ECS::VPC\",\n" +
                          "      \"Properties\": {\n" +
                          "        \"CidrBlock\": {\n" +
                          "          \"Ref\": \"CidrBlock\"\n" +
                          "        },\n" +
                          "        \"VpcName\": {\n" +
                          "          \"Ref\": \"VpcName\"\n" +
                          "        }\n" +
                          "      }\n" +
                          "    }\n" +
                          "  }\n" +
                          "}";
    CreateStackRequest.CreateStackRequestParameters parameters0 = new CreateStackRequest.CreateStackRequestParameters()
                    .setParameterKey("VpcName")
                    .setParameterValue("TestVpc");
    CreateStackRequest.CreateStackRequestParameters parameters1 = new CreateStackRequest.CreateStackRequestParameters()
                    .setParameterKey("CidrBlock")
                    .setParameterValue("192.168.0.0/16");
    
    List parameters = java.util.Arrays.asList(parameters0, parameters1);

    参数设置的示例代码如下所示。

    public static void createStack(Client client, String regionId, String stackName, String templateBody, Long timeoutInMinutes, List parameters) throws Exception {
        CreateStackRequest createStackRequest = new CreateStackRequest()
                .setTemplateBody(templateBody)
                .setStackName(stackName)
                .setRegionId(regionId)
                .setTimeoutInMinutes(timeoutInMinutes)
                .setParameters(parameters);
         // 复制代码运行后,请自行打印API的返回值。
         CreateStackResponse resp = client.createStack(createStackRequest);
         com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
    }
  • 查询资源栈示例

    通过调用GetStack接口查询资源栈信息。关于GetStack接口的详情,请参见GetStack

    在本示例中,指定了以下参数:

    • stackId:资源栈ID。

    • regionId:资源栈所属的地域ID。

    参数设置的示例代码如下:

public static void getStack(Client client, String stackId, String regionId) throws Exception {        
    GetStackRequest getStackRequest = new GetStackRequest()            
        .setStackId(stackId)            
        .setRegionId(regionId);    
        // 复制代码运行后,请自行打印API的返回值。    
    GetStackResponse resp = client.getStack(getStackRequest);    
    com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
}
  • 删除资源栈示例

    通过调用DeleteStack接口删除资源栈。关于DeleteStack接口的详情,请参见DeleteStack

    在本示例中,指定了以下参数:

    • regionId:资源栈所属的地域ID。

    • stackId:资源栈ID。

  • 参数设置的示例代码如下所示。

public static void deleteStack(Client client, String stackId, String regionId) throws Exception {    
    DeleteStackRequest deleteStackRequest = new DeleteStackRequest()
                .setStackId(stackId)
                .setRegionId(regionId);
    // 复制代码运行后,请自行打印API的返回值。
    DeleteStackResponse resp = client.deleteStack(deleteStackRequest);
    com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
}
  • 完整示例

    以下为查询可用地域列表、创建资源栈、查询资源栈和删除资源栈的完整操作示例。

    说明

    调用接口前,需配置环境变量,通过环境变量读取访问凭证。更多信息,请参见配置访问凭证

    AccessKey ID和AccessKey Secret的环境变量名:ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET

    package com.aliyun.sample;
    
    import com.aliyun.ros20190910.Client;
    import com.aliyun.tea.*;
    import com.aliyun.ros20190910.*;
    import com.aliyun.ros20190910.models.*;
    import com.aliyun.teaopenapi.*;
    import com.aliyun.teaopenapi.models.*;
    import com.aliyun.darabonba.env.*;
    import java.util.*;
    
    
    
    public class Sample {
    
        /**
         * 初始化账号Client
         * @return Client
         * @throws Exception
         */
        private static String REGIONID = "Region Id";
    
        public static com.aliyun.ros20190910.Client createClient() throws Exception {
            // 使用默认凭证初始化Credentials Client。
            com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
            Config config = new Config()
                    // 配置云产品服务接入地址(endpoint)
                    .setEndpoint("ros.aliyuncs.com")
                    // 使用Credential配置凭证
                    .setCredential(credentialClient);
            return new com.aliyun.ros20190910.Client(config);
        }
    
        public static void describeRegions(Client client) throws Exception {
            DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
            // 复制代码运行后,请自行打印API的返回值。
            DescribeRegionsResponse resp = client.describeRegions(describeRegionsRequest);
        }
    
        public static void getStack(Client client, String stackId, String regionId,  Boolean wait) throws Exception {
            GetStackRequest getStackRequest = new GetStackRequest()
                    .setStackId(stackId)
                    .setRegionId(regionId);
            // 复制代码运行后,请自行打印API的返回值。
            GetStackResponse response = client.getStack(getStackRequest);
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
            GetStackResponseBody body = response.body;
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
            if (wait) {
                if (com.aliyun.teautil.Common.equalString(body.status, "CREATE_IN_PROGRESS")) {
                    com.aliyun.teaconsole.Client.log("正在创建中......");
                    com.aliyun.teautil.Common.sleep(30000);
                    Sample.getStack(client, stackId, regionId, wait);
                } else if (com.aliyun.teautil.Common.equalString(body.status, "CREATE_FAILED")) {
                    com.aliyun.teaconsole.Client.log("创建失败, 信息:" + body.statusReason + ", requestId:" + body.requestId + "");
                } else if (com.aliyun.teautil.Common.equalString(body.status, "CREATE_COMPLETE")) {
                    com.aliyun.teaconsole.Client.log("创建成功");
                    com.aliyun.teaconsole.Client.log("ID:" + body.stackId + "");
                }
    
            } else {
                com.aliyun.teaconsole.Client.log("ID:" + body.stackId + "");
                com.aliyun.teaconsole.Client.log("状态:" + body.status + "");
            }
        }
    
    
        public static void deleteStack(Client client, String stackId, String regionId) throws Exception {
            DeleteStackRequest deleteStackRequest = new DeleteStackRequest()
                    .setStackId(stackId)
                    .setRegionId(regionId);
            // 复制代码运行后,请自行打印API的返回值。
            DeleteStackResponse resp = client.deleteStack(deleteStackRequest);
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
        }
    
        public static String createStack(Client client, String regionId, String stackName, String templateBody, Long timeoutInMinutes, java.util.List parameters) throws Exception {
            CreateStackRequest createStackRequest = new CreateStackRequest()
                .setTemplateBody(templateBody)
                .setStackName(stackName)
                .setRegionId(regionId)
                .setTimeoutInMinutes(timeoutInMinutes)
                .setParameters(parameters);
            // 复制代码运行后,请自行打印API的返回值。
            CreateStackResponse resp = client.createStack(createStackRequest);
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
            return resp.body.stackId;
        }
    
        public static void main(String[] args_) throws Exception {
            String stackName = "MyStack";
            String parameter =" {\"ParameterKey\": \"VpcName\", \"ParameterValue\": \"test_vpc\"}";
            Long timeoutInMinutes = 40L;
            String templateBody = "{\n" +
                            "  \"ROSTemplateFormatVersion\": \"2015-09-01\",\n" +
                            "  \"Parameters\": {\n" +
                            "    \"VpcName\": {\n" +
                            "      \"Type\": \"String\",\n" +
                            "      \"Description\": \"Vpc Name\",\n" +
                            "      \"Label\": \"Vpc Name\"\n" +
                            "    },\n" +
                            "    \"CidrBlock\": {\n" +
                            "      \"Type\": \"String\",\n" +
                            "      \"Description\": \"Vpc CidrBlock\",\n" +
                            "      \"Label\": \"Vpc CidrBlock\"\n" +
                            "    }\n" +
                            "  },\n" +
                            "  \"Resources\": {\n" +
                            "    \"Vpc\": {\n" +
                            "      \"Type\": \"ALIYUN::ECS::VPC\",\n" +
                            "      \"Properties\": {\n" +
                            "        \"CidrBlock\": {\n" +
                            "          \"Ref\": \"CidrBlock\"\n" +
                            "        },\n" +
                            "        \"VpcName\": {\n" +
                            "          \"Ref\": \"VpcName\"\n" +
                            "        }\n" +
                            "      }\n" +
                            "    }\n" +
                            "  }\n" +
                            "}";
            com.aliyun.ros20190910.Client client = Sample.createClient();
    
            Sample.describeRegions(client);
            CreateStackRequest.CreateStackRequestParameters parameters0 = new CreateStackRequest.CreateStackRequestParameters()
                    .setParameterKey("VpcName")
                    .setParameterValue("TestVpc");
            CreateStackRequest.CreateStackRequestParameters parameters1 = new CreateStackRequest.CreateStackRequestParameters()
                    .setParameterKey("CidrBlock")
                    .setParameterValue("192.168.0.0/16");
    
            List parameters = java.util.Arrays.asList(parameters0, parameters1);
            String stackId = Sample.createStack(client, REGIONID, stackName, templateBody, timeoutInMinutes, parameters);
            Sample.getStack(client, stackId, REGIONID, true);
            Sample.deleteStack(client, stackId, REGIONID);
        }
    }