阿里云资源编排服务 Java SDK使用入门

简介: 资源编排 Java SDK 入门

阿里云资源编排服务 Java SDK使用入门

安装依赖

添加Maven库

<repositories>
    <repository>
        <id>sonatype-nexus-staging</id>
        <name>Sonatype Nexus Staging</name>
        <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

在项目中包含依赖

创建一个新的maven项目,或者在您已有的项目中通过maven引入依赖:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>3.2.4</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-ros</artifactId>
    <version>2.2.6</version>
</dependency>

ROS JAVA SDK 与服务器通过 HTTP 的方式交互,HTTP Request 和 Response 的内容为 json 格式的字符串,请在代码中引入合适的 json 包,如:

import org.json.JSONObject;

示例中使用的版本为:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20170516</version>
</dependency>

初始化客户端

在您准备调用SDKjava类中引入相关的包:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;

配置您的客户端对象:

private static String REGION_ID = "YOUR REGION";
private static String ACCESS_ID = "YOUR ID";
private static String ACCESS_KEY = "YOUR KEY";

IClientProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_ID, ACCESS_KEY);
IAcsClient client = new DefaultAcsClient(profile);
  • 其中AccessKeyIdAccessKeySecure是用户访问阿里云Open API时的认证信息,可以登陆阿里云官方站后获得。
  • 第三个参数是用户访问的资源所在的默认region-id

使用SDK

基本流程

  • 根据应用场景选择要调用的方法,申明其请求对象

    CreateStacksRequest describe = new CreateStacksRequest();
    
  • 按照参数设置要求设置请求的参数,具体的参数设置要求参见 API文档

    // header
    describe.putHeaderParameter("x-acs-region-id", "cn-beijing"); 
    
    // url, sdk request会有对应的方法
    describe.setName("Name") 
    
    // Content
    describe.setContent(content.getBytes("utf-8"), "utf-8", FormatType.JSON);
    
  • 获取结果,为json字符串,之后您可以根据您的需要进行处理

    HttpResponse response = client.doAction(describe);
    String stringContent = ParseContent(response);
    System.out.println(stringContent);
    

这里我们列举四个示例,示例中用到的其余函数参见附件中的代码:

List Region

/*
 * List regions
 */
public static void ListRegions(IAcsClient client) {
   
    DescribeRegionsRequest describe = new DescribeRegionsRequest();

    try {
   
        HttpResponse response = client.doAction(describe);
        String stringContent = ParseContent(response);
        System.out.println(stringContent);

    }catch (ServerException e) {
   
        e.printStackTrace();
    } 
    catch (ClientException e) {
   
        e.printStackTrace();
    } 
}

示例输出:

{
   "Regions": [{
   "LocalName": "\u534e\u5317 1", "RegionId": "cn-qingdao"}, {
   "LocalName": "\u534e\u5317 2", "RegionId": "cn-beijing"}, {
   "LocalName": "\u534e\u5317 3", "RegionId": "cn-zhangjiakou"}, {
   "LocalName": "\u534e\u4e1c 1", "RegionId": "cn-hangzhou"}, {
   "LocalName": "\u534e\u4e1c 2", "RegionId": "cn-shanghai"}, {
   "LocalName": "\u534e\u5357 1", "RegionId": "cn-shenzhen"}, {
   "LocalName": "\u9999\u6e2f", "RegionId": "cn-hongkong"}, {
   "LocalName": "\u4e9a\u592a\u4e1c\u5317 1 (\u4e1c\u4eac)", "RegionId": "ap-northeast-1"}, {
   "LocalName": "\u4e9a\u592a\u4e1c\u5357 1 (\u65b0\u52a0\u5761)", "RegionId": "ap-southeast-1"}, {
   "LocalName": "\u4e9a\u592a\u4e1c\u5357 2 (\u6089\u5c3c)", "RegionId": "ap-southeast-2"}, {
   "LocalName": "\u7f8e\u56fd\u4e1c\u90e8 1 (\u5f17\u5409\u5c3c\u4e9a)", "RegionId": "us-east-1"}, {
   "LocalName": "\u7f8e\u56fd\u897f\u90e8 1 (\u7845\u8c37)", "RegionId": "us-west-1"}, {
   "LocalName": "\u4e2d\u4e1c\u4e1c\u90e8 1 (\u8fea\u62dc)", "RegionId": "me-east-1"}, {
   "LocalName": "\u6b27\u6d32\u4e2d\u90e8 1 (\u6cd5\u5170\u514b\u798f)", "RegionId": "eu-central-1"}]}

List Stacks

/*
 * List stacks
 */
public static void ListStacks(IAcsClient client) {
         
    DescribeStacksRequest describe = new DescribeStacksRequest();

    // example for set parameters in header
    describe.putHeaderParameter("x-acs-region-id", "cn-beijing"); 

    // example for set parameters in url
    describe.setName("liyi_test_170615"); // if not set, list all stacks

    try {
   
        HttpResponse response = client.doAction(describe);
        String stringContent = ParseContent(response);
        System.out.println(stringContent);

    }catch (ServerException e) {
   
        e.printStackTrace();
    } 
    catch (ClientException e) {
   
        e.printStackTrace();
    } 
}

Validate Template

/*
 * Validate a template
 * Succeed: return template
 * Fail: return error infomation 
 */
public static void ValidateTemplate(IAcsClient client) {
   
    ValidateTemplateRequest describe = new ValidateTemplateRequest();

    // example for set parameters in content
    String content = "{\"Template\":" + readToString("C:\\Users\\quming.ly\\Desktop\\nodejs.json") + "}";
    try {
   
        describe.setContent(content.getBytes("utf-8"), "utf-8", FormatType.JSON);
    } catch (UnsupportedEncodingException e1) {
   
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
   
        HttpResponse response = client.doAction(describe);
        String stringContent = ParseContent(response);
        System.out.println(stringContent);

    }catch (ServerException e) {
   
        e.printStackTrace();
    } 
    catch (ClientException e) {
   
        e.printStackTrace();
    } 
}

Create Stack

/*
 * Create a template
 * Succeed: return stack name and id
 * Fail: return error infomation 
 */
public static void CreateStack(IAcsClient client) {
   
    CreateStacksRequest describe = new CreateStacksRequest();

    // example for set parameters in header
    describe.putHeaderParameter("x-acs-region-id", "cn-beijing"); 

    // example for set parameters in content
    JSONObject object = new JSONObject(); 
    object.put("TimeoutMins", 60);
    object.put("Name", "JAVA_SDK_DEMO");
    object.put("Template",readToString("C:\\Users\\quming.ly\\Desktop\\template.json"));

    // The follow parameters are depend on your template
    JSONObject parameters = new JSONObject(); 
    parameters.put("DBUser", "Demo");
    parameters.put("DBPassword", "Demo123456");
    parameters.put("DBRootPassword", "Demo123456");
    parameters.put("InstancePassword", "Demo123456");

    object.put("Parameters", parameters);

    try {
   
        describe.setContent(object.toString().getBytes("utf-8"), "utf-8", FormatType.JSON);
    } catch (UnsupportedEncodingException e1) {
   
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
   
        HttpResponse response = client.doAction(describe);
        String stringContent = ParseContent(response);
        System.out.println(stringContent);

    }catch (ServerException e) {
   
        e.printStackTrace();
    } 
    catch (ClientException e) {
   
        e.printStackTrace();
    } 
}

完整的工程代码见附件。

相关实践学习
使用ROS创建VPC和VSwitch
本场景主要介绍如何利用阿里云资源编排服务,定义资源编排模板,实现自动化创建阿里云专有网络和交换机。
ROS入门实践
本课程将基于基础设施即代码 IaC 的理念,介绍阿里云自动化编排服务ROS的概念、功能和使用方式,并通过实际应用场景介绍如何借助ROS实现云资源的自动化部署,使得云上资源部署和运维工作更为高效。
相关文章
|
弹性计算 安全 开发工具
灵码评测-阿里云提供的ECS python3 sdk做安全组管理
批量变更阿里云ECS安全组策略(批量变更)
|
程序员 开发工具 Android开发
Android|使用阿里云推流 SDK 实现双路推流不同画面
本文记录了一种使用没有原生支持多路推流的阿里云推流 Android SDK,实现同时推送两路不同画面的流的方法。
500 7
|
Kubernetes API 开发工具
【Azure Developer】通过SDK(for python)获取Azure服务生命周期信息
需要通过Python SDK获取Azure服务的一些通知信息,如:K8S版本需要更新到指定的版本,Azure服务的维护通知,服务处于不健康状态时的通知,及相关的操作建议等内容。
368 18
|
Java 开发工具
通过Java SDK调用阿里云模型服务
在阿里云平台上,可以通过创建应用并使用模型服务完成特定任务,如生成文章内容。本示例展示了一段简化的Java代码,演示了如何调用阿里云模型服务生成关于“春秋战国经济与文化”的简短文章。示例代码通过设置系统角色为历史学家,并提出文章生成需求,最终处理并输出生成的文章内容。在实际部署前,请确保正确配置环境变量中的密钥和ID,并根据需要调整SDK导入语句及类名。更多详情和示例,请参考相关链接。
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
218 0
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
Azure 容器实例(Azure Container Instances,简称 ACI)是一个无服务器容器解决方案,允许用户在 Azure 云环境中运行 Docker 容器,而无需设置虚拟机、集群或编排器。 ACI 适用于任何可以在隔离容器中操作的场景,包括事件驱动的应用程序、从容器开发管道快速部署、数据处理和生成作业。
258 1
|
消息中间件 分布式计算 DataWorks
DataWorks产品使用合集之如何使用Python和阿里云SDK读取OSS中的文件
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
Ubuntu 机器人 Linux
|
传感器 人工智能 算法
ROS机器人操作系统
ROS机器人操作系统
895 1
|
自动驾驶 安全 机器人
ROS2:从初识到深入,探索机器人操作系统的进化之路
前言 最近开始接触到基于DDS的这个系统,是在稚晖君的机器人项目中了解和认识到。于是便开始自己买书学习起来,感觉挺有意思的,但是只是单纯的看书籍,总会显得枯燥无味,于是自己又开始在网上找了一些视频教程结合书籍一起来看,便让我对ROS系统有了更深的认识和理解。 ROS的发展历程 ROS诞生于2007年的斯坦福大学,这是早期PR2机器人的原型,这个项目很快被一家商业公司Willow Garage看中,类似现在的风险投资一样,他们投了一大笔钱给这群年轻人,PR2机器人在资本的助推下成功诞生。 2010年,随着PR2机器人的发布,其中的软件正式确定了名称,就叫做机器人操作系统,Robot Op
1117 14

推荐镜像

更多
  • ros