开发者社区> 问答> 正文

资源编排Java SDK如何使用?

资源编排Java SDK如何使用?

展开
收起
小天使爱美 2020-03-24 18:48:04 736 0
2 条回答
写回答
取消 提交回答
  • 有点尴尬唉 你要寻找的东西已经被吃掉啦!

    准备工作

    下载及安装Java SDK。
    说明 建议您使用JRE 1.8及以上版本。
    在pom.xml中,添加aliyun-java-sdk-core及其它依赖包。
    
    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ros</artifactId>
            <version>3.2.0</version>
        </dependency>
    </dependencies>
    
    初始化SDK。
        导入相关的包。
    
        import java.util.ArrayList;
        import java.util.List;
        import com.aliyuncs.DefaultAcsClient;
        import com.aliyuncs.exceptions.ClientException;
        import com.aliyuncs.http.FormatType;
        import com.aliyuncs.http.HttpResponse;
        import com.aliyuncs.profile.DefaultProfile;
        import com.aliyuncs.ros.model.v20190910.CreateStackRequest;
        import com.aliyuncs.ros.model.v20190910.DeleteStackRequest;
        import com.aliyuncs.ros.model.v20190910.DescribeRegionsRequest;
        import com.aliyuncs.ros.model.v20190910.GetStackRequest;
    
        初始化SDK客户端对象。
    
        private static String ACCESSKEYID = "<yourAccessKeyId>";
        private static String SECRET = "<yourAccessKeySecrect>";
        private static String REGIONID = "<yourRegionId>";
        private static DefaultAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET));
        private static int connectTimeout = 10000;
        private static int readTimeout = 10000;
        private static Long timeoutInMinutes = 30L;
    

    查询可用地域列表

    您可以使用Java SDK查询可用地域列表。

    public String describeRegion() { DescribeRegionsRequest request = new DescribeRegionsRequest(); request.setConnectTimeout(connectTimeout); request.setReadTimeout(readTimeout); request.setAcceptFormat(FormatType.JSON); HttpResponse response = null; String responseContent = null; try { response = client.doAction(request); responseContent = response.getHttpContentString(); } catch (ClientException e) { e.printStackTrace(); } return responseContent; }

    创建资源栈 创建资源栈时,您必须指定以下参数:

    StackName:将要创建的资源栈的名称。每个用户空间下的资源栈名称不能重复。
    TimeoutInMinutes:资源栈创建时长,单位为分钟。如果在指定时间内不能完成创建,则超时失败。
    TemplateBody:创建的资源栈使用的模板内容。
    TemplateURL:模板主体的文件的位置。必须指定TemplateBody或TemplateURL,但不能同时指定两者。
    Parameters:创建的资源栈所需的参数。需要在模板中定义Key。
    

    String stackName = "MyStack"; 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" + "}"; List<CreateStackRequest.Parameters> parameterss = new ArrayList<CreateStackRequest.Parameters>(); CreateStackRequest.Parameters cidrBlock = new CreateStackRequest.Parameters(); cidrBlock.setParameterKey("CidrBlock"); cidrBlock.setParameterValue("192.168.0.0/16"); CreateStackRequest.Parameters vpcName = new CreateStackRequest.Parameters(); vpcName.setParameterKey("VpcName"); vpcName.setParameterValue("TestVpc"); parameterss.add(cidrBlock); parameterss.add(vpcName);

    public String createStack(String stackName, String templateBody, List<CreateStackRequest.Parameters> parameterss) { CreateStackRequest request = new CreateStackRequest(); request.setConnectTimeout(connectTimeout); request.setReadTimeout(readTimeout); request.setTimeoutInMinutes(timeoutInMinutes); request.setStackName(stackName); // 若模板较大,建议使用TemplateURL参数,避免URL过长,调用失败。 // 您也可以继承CreateStackRequest类,通过调用putBodyParameter(protected方法)自行实现setTemplateBody方法。 // request.putBodyParameter("TemplateBody", templateBody) request.setTemplateBody(templateBody); request.setParameterss(parameterss); HttpResponse response = null; String responseContent = null; try { response = client.doAction(request); responseContent = response.getHttpContentString(); } catch (ClientException e) { e.printStackTrace(); } return responseContent; }

    查询资源栈

    输入目标资源栈的ID。

    public String getStack(String stackId) { GetStackRequest request = new GetStackRequest(); request.setConnectTimeout(connectTimeout); request.setReadTimeout(readTimeout); request.setStackId(stackId); request.setAcceptFormat(FormatType.JSON); HttpResponse response = null; String responseContent = null; try { response = client.doAction(request); responseContent = response.getHttpContentString(); } catch (ClientException e) { e.printStackTrace(); } return responseContent; }

    删除资源栈

    输入目标资源栈的ID。

    public String deleteStack(String stackId) { DeleteStackRequest request = new DeleteStackRequest(); request.setConnectTimeout(connectTimeout); request.setReadTimeout(readTimeout); request.setStackId(stackId); request.setAcceptFormat(FormatType.JSON); HttpResponse response = null; String responseContent = null; try { response = client.doAction(request); responseContent = response.getHttpContentString(); } catch (ClientException e) { e.printStackTrace(); } return responseContent; }

    操作样例

    package com;

    import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.ros.model.v20190910.CreateStackRequest; import com.aliyuncs.ros.model.v20190910.DeleteStackRequest; import com.aliyuncs.ros.model.v20190910.DescribeRegionsRequest; import com.aliyuncs.ros.model.v20190910.GetStackRequest;

    import java.util.ArrayList; import java.util.List;

    public class Stack { private static String ACCESSKEYID = " "; private static String SECRET = " "; private static String REGIONID = " "; private static DefaultAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET)); private static int connectTimeout = 10000; private static int readTimeout = 10000; private static Long timeoutInMinutes = 30L;

    public String describeRegion() {
        DescribeRegionsRequest request = new DescribeRegionsRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setAcceptFormat(FormatType.JSON);
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }
    
    public String createStack(String stackName, String templateBody, List<CreateStackRequest.Parameters> parameterss) {
        CreateStackRequest request = new CreateStackRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setTimeoutInMinutes(timeoutInMinutes);
        request.setStackName(stackName);
        // 若模板较大,建议使用TemplateURL参数,避免URL过长,调用失败。
        // 您也可以继承CreateStackRequest类,通过调用putBodyParameter(protected方法)自行实现setTemplateBody方法。
        // request.putBodyParameter("TemplateBody", templateBody)
        request.setTemplateBody(templateBody);
        request.setParameterss(parameterss);
    
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }
    
    public String getStack(String stackId) {
        GetStackRequest request = new GetStackRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setStackId(stackId);
        request.setAcceptFormat(FormatType.JSON);
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }
    
    public String deleteStack(String stackId) {
        DeleteStackRequest request = new DeleteStackRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setStackId(stackId);
        request.setAcceptFormat(FormatType.JSON);
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }
    
    public static void main(String[] args) throws ClassNotFoundException {
        StackDemo stack = new StackDemo();
        String stackName = "MyStack";
        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" +
                "}";
        List<CreateStackRequest.Parameters> parameterss = new ArrayList<CreateStackRequest.Parameters>();
        CreateStackRequest.Parameters cidrBlock = new CreateStackRequest.Parameters();
        cidrBlock.setParameterKey("CidrBlock");
        cidrBlock.setParameterValue("192.168.0.0/16");
        CreateStackRequest.Parameters vpcName = new CreateStackRequest.Parameters();
        vpcName.setParameterKey("VpcName");
        vpcName.setParameterValue("TestVpc");
        parameterss.add(cidrBlock);
        parameterss.add(vpcName);
        stack.createStack(stackName, templateBody, parameterss);
        stack.describeRegion();
        stack.getStack("<yourStackId>");
        stack.deleteStack("<yourStackId>");
    }
    

    }

    2020-04-04 22:01:43
    赞同 展开评论 打赏
  • 本文为您介绍如何使用资源编排服务(ROS)的Java SDK来创建和管理资源栈。

    背景信息 您除了可以在ROS控制台创建资源栈,还可以使用API代码来创建和管理资源栈。

    准备工作 下载及安装Java SDK。 注意 建议您使用JRE 1.8及以上版本。 在pom.xml中,添加aliyun-java-sdk-core及其它依赖包。 com.aliyun aliyun-java-sdk-core 4.1.0 com.aliyun aliyun-java-sdk-ros 3.0.1 初始化SDK。 导入以下库。 import java.util.ArrayList; import java.util.List; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.ros.model.v20190910.CreateStackRequest; import com.aliyuncs.ros.model.v20190910.DeleteStackRequest; import com.aliyuncs.ros.model.v20190910.DescribeRegionsRequest; import com.aliyuncs.ros.model.v20190910.GetStackRequest; 初始化SDK客户端对象。 private static String ACCESSKEYID = " "; private static String SECRET = " "; private static String REGIONID = " "; private static DefaultAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET)); private static int connectTimeout = 10000; private static int readTimeout = 10000; private static Long timeoutInMinutes = 30L; 查询可用地域列表 您可以使用Java SDK查询可用地域列表。

    public String describeRegion() { DescribeRegionsRequest request = new DescribeRegionsRequest(); request.setConnectTimeout(connectTimeout); request.setReadTimeout(readTimeout); request.setAcceptFormat(FormatType.JSON); HttpResponse response = null; String responseContent = null; try { response = client.doAction(request); responseContent = response.getHttpContentString(); } catch (ClientException e) { e.printStackTrace(); } return responseContent; } 创建资源栈 创建资源栈时,您必须指定以下参数: StackName:将要创建的资源栈的名称。每个用户空间下的资源栈名称不能重复。 TimeoutInMinutes:资源栈创建的时长。如果在指定的时长内,资源栈未被创建,则创建超时(失败)。单位:分钟。 TemplateBody:创建的资源栈使用的模板内容。 TemplateURL:模板主体的文件的位置。必须指定TemplateBody或TemplateURL,但不能同时指定两者。 Parameters:创建的资源栈所需的参数。需要在模板中定义Key。 String stackName = "MyStack"; 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" + "}"; List<CreateStackRequest.Parameters> parameterss = new ArrayList<CreateStackRequest.Parameters>(); CreateStackRequest.Parameters cidrBlock = new CreateStackRequest.Parameters(); cidrBlock.setParameterKey("CidrBlock"); cidrBlock.setParameterValue("192.168.0.0/16"); CreateStackRequest.Parameters vpcName = new CreateStackRequest.Parameters(); vpcName.setParameterKey("VpcName"); vpcName.setParameterValue("TestVpc"); parameterss.add(cidrBlock); parameterss.add(vpcName); public String createStack(String stackName, String templateBody, List<CreateStackRequest.Parameters> parameterss) { CreateStackRequest request = new CreateStackRequest(); request.setConnectTimeout(connectTimeout); request.setReadTimeout(readTimeout); request.setTimeoutInMinutes(timeoutInMinutes); request.setStackName(stackName); request.setTemplateBody(templateBody); request.setParameterss(parameterss); HttpResponse response = null; String responseContent = null; try { response = client.doAction(request); responseContent = response.getHttpContentString(); } catch (ClientException e) { e.printStackTrace(); } return responseContent; } 查询资源栈 输入目标资源栈的ID。

    public String getStack(String stackId) { GetStackRequest request = new GetStackRequest(); request.setConnectTimeout(connectTimeout); request.setReadTimeout(readTimeout); request.setStackId(stackId); request.setAcceptFormat(FormatType.JSON); HttpResponse response = null; String responseContent = null; try { response = client.doAction(request); responseContent = response.getHttpContentString(); } catch (ClientException e) { e.printStackTrace(); } return responseContent; } 删除资源栈 输入目标资源栈的ID。

    public String deleteStack(String stackId) { DeleteStackRequest request = new DeleteStackRequest(); request.setConnectTimeout(connectTimeout); request.setReadTimeout(readTimeout); request.setStackId(stackId); request.setAcceptFormat(FormatType.JSON); HttpResponse response = null; String responseContent = null; try { response = client.doAction(request); responseContent = response.getHttpContentString(); } catch (ClientException e) { e.printStackTrace(); } return responseContent; } 操作样例 package com;

    import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.ros.model.v20190910.CreateStackRequest; import com.aliyuncs.ros.model.v20190910.DeleteStackRequest; import com.aliyuncs.ros.model.v20190910.DescribeRegionsRequest; import com.aliyuncs.ros.model.v20190910.GetStackRequest;

    import java.util.ArrayList; import java.util.List;

    public class Stack { private static String ACCESSKEYID = " "; private static String SECRET = " "; private static String REGIONID = " "; private static DefaultAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET)); private static int connectTimeout = 10000; private static int readTimeout = 10000; private static Long timeoutInMinutes = 30L;

    public String describeRegion() {
        DescribeRegionsRequest request = new DescribeRegionsRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setAcceptFormat(FormatType.JSON);
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }
    
    public String createStack(String stackName, String templateBody, List<CreateStackRequest.Parameters> parameterss) {
        CreateStackRequest request = new CreateStackRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setTimeoutInMinutes(timeoutInMinutes);
        request.setStackName(stackName);
        request.setTemplateBody(templateBody);
        request.setParameterss(parameterss);
    
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }
    
    public String getStack(String stackId) {
        GetStackRequest request = new GetStackRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setStackId(stackId);
        request.setAcceptFormat(FormatType.JSON);
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }
    
    public String deleteStack(String stackId) {
        DeleteStackRequest request = new DeleteStackRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setStackId(stackId);
        request.setAcceptFormat(FormatType.JSON);
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }
    
    public static void main(String[] args) throws ClassNotFoundException {
        StackDemo stack = new StackDemo();
        String stackName = "MyStack";
        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" +
                "}";
        List<CreateStackRequest.Parameters> parameterss = new ArrayList<CreateStackRequest.Parameters>();
        CreateStackRequest.Parameters cidrBlock = new CreateStackRequest.Parameters();
        cidrBlock.setParameterKey("CidrBlock");
        cidrBlock.setParameterValue("192.168.0.0/16");
        CreateStackRequest.Parameters vpcName = new CreateStackRequest.Parameters();
        vpcName.setParameterKey("VpcName");
        vpcName.setParameterValue("TestVpc");
        parameterss.add(cidrBlock);
        parameterss.add(vpcName);
        stack.createStack(stackName, templateBody, parameterss);
        stack.describeRegion();
        stack.getStack("<yourStackId>");
        stack.deleteStack("<yourStackId>");
    }
    

    }

    2020-03-24 19:32:51
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
从 SDK 到编解码:视频直播架构解析 立即下载
跨平台的云服务SDK需要什么 立即下载
一个跨平台的云服务SDK需要什么 立即下载

相关镜像