本文为您介绍资源编排服务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>tea-openapi</artifactId>
            <version>0.0.19</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>ros20190910</artifactId>
            <version>1.0.1</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.*;
    2. 初始化SDK客户端对象。
      public static com.aliyun.ros20190910.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
              Config config = new Config()
                      // 您的AccessKey ID
                      .setAccessKeyId(accessKeyId)
                      // 您的AccessKey Secret
                      .setAccessKeySecret(accessKeySecret);
              // 访问的域名
              config.endpoint = "ros.aliyuncs.com";
              return new com.aliyun.ros20190910.Client(config);
          }

示例

  • 查询可用地域列表示例

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

    public static void describeRegions(String[] args_) throws Exception {
            java.util.List<String> args = java.util.Arrays.asList(args_);
            com.aliyun.ros20190910.Client client = Sample.createClient("accessKeyId", "accessKeySecret");
            DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
            DescribeRegionsResponse resp = client.describeRegions(describeRegionsRequest);
        }
  • 创建资源栈示例
    通过调用CreateStack接口创建资源栈。关于CreateStack接口的详情,请参见CreateStack
    public static void createStack(String stackName, String regionId) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);    
        CreateStackRequest.CreateStackRequestParameters parameters0 = new CreateStackRequest.CreateStackRequestParameters()            
            .setParameterKey("VpcName")            
            .setParameterValue("TestVpc");    
        CreateStackRequest.CreateStackRequestParameters parameters1 = new CreateStackRequest.CreateStackRequestParameters()            
            .setParameterKey("CidrBlock")            
            .setParameterValue("192.168.0.0/16");    
        CreateStackRequest createStackRequest = new CreateStackRequest()            
            .setTemplateBody("{\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" +                    
                "}")            
            .setStackName("MyStack")            
            .setRegionId("cn-hangzhou")            
            .setTimeoutInMinutes(40L)            
            .setParameters(java.util.Arrays.asList(                    
                parameters0,                    
                parameters1            
            ));    
            // 复制代码运行后,请自行打印API的返回值。    
            client.createStack(createStackRequest);
    }
  • 查询资源栈示例

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

    public static void getStack(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

    public static void deleteStack(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)));
    }
  • 完整示例

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

    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.*;
    
    public class Sample {
    
        /**
         * 使用AK和SK初始化账号Client
         *
         * @param accessKeyId
         * @param accessKeySecret
         * @return Client
         * @throws Exception
         */
        private static String ACCESSKEYID = "<yourAccessKeyId>";
        private static String SECRET = "<yourAccessKeySecrect>";
        private static String REGIONID = "<yourRegionId>";
    
        public static com.aliyun.ros20190910.Client createClient() throws Exception {
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(ACCESSKEYID)
                    // 您的AccessKey Secret
                    .setAccessKeySecret(SECRET);
            // 访问的域名
            config.endpoint = "ros.aliyuncs.com";
            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) 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)));
        }
    
        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 void main(String[] args_) throws Exception {
            java.util.List<String> args = java.util.Arrays.asList(args_);
            com.aliyun.ros20190910.Client client = Sample.createClient();
            CreateStackRequest.CreateStackRequestParameters parameters0 = new CreateStackRequest.CreateStackRequestParameters()
                    .setParameterKey("VpcName")
                    .setParameterValue("TestVpc");
            CreateStackRequest.CreateStackRequestParameters parameters1 = new CreateStackRequest.CreateStackRequestParameters()
                    .setParameterKey("CidrBlock")
                    .setParameterValue("192.168.0.0/16");
            CreateStackRequest createStackRequest = new CreateStackRequest()
                    .setTemplateBody("{\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" +
                            "}")
                    .setStackName("stackName")
                    .setRegionId("cn-hangzhou")
                    .setTimeoutInMinutes(40L)
                    .setParameters(java.util.Arrays.asList(
                            parameters0,
                            parameters1
                    ));
            // 复制代码运行后,请自行打印API的返回值。
            client.createStack(createStackRequest);
            Sample.describeRegions(client);
            Sample.getStack(client, "yourStackId", REGIONID);
            Sample.deleteStack(client, "yourStackId", REGIONID);
        }
    }