Git Product home page Git Product logo

aliyun-openapi-java-sdk's Introduction

Alibaba Cloud Java Software Development Kit

Maven Central Build Status codecov Codacy Badge Appveyor Badge

中文文档

The Alibaba Cloud Software Development Kit Java (SDK) allows you to access Alibaba Cloud services such as Elastic Compute Service (ECS), Server Load Balancer (SLB), and CloudMonitor. You can access Alibaba Cloud services without the need to handle API related tasks, such as signing and constructing your requests.

This document introduces how to obtain and call Alibaba Cloud Java SDK.

If you have any problem while using Java SDK, please submit an issue.

Prerequisites

  • To use Alibaba Cloud Java SDK, you must have an Alibaba Cloud account as well as an AccessKey ID and an AccessKey Secret. Create and view your AccessKey on the RAM console or contact your system administrator.

  • To use an Alibaba Cloud Java SDK to access the APIs of a product, you must first activate the product on the Alibaba Cloud console if required.

  • Alibaba Cloud Java SDK requires JDK 1.6 or later.

Install Java SDK

If you use Apache Maven to manage Java projects, you only need to add corresponding dependencies to the pom.xml files of the projects. You can download the Maven dependencies of different cloud products in Alibaba Cloud SDK.

You must install the SDK core library for any SDK you use. For example, to call the ECS SDK, you must install both the ECS SDK and the SDK core library.

For example, with SDK core library latest version and ECS SDK latest version, you need to declare the two SDKs in the pom.xml file.

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>[4.3.2,10.0.0)</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-ecs</artifactId>
    <version>[4.16.0,10.0.0)</version>
</dependency>

If maven is not downloading jar packages from a central repository, you need to add this dependency in the pom.xml file, or a NoClassDefFoundError exception will be reported

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

Initiate a call

The following code example shows the three main steps to use Alibaba Cloud Java SDK:

  1. Create and initialize a DefaultAcsClient instance.

  2. Create an API request and set parameters.

  3. Initiate the request and handle the response or exceptions.

Using AccessKey call

 package com.testprogram;
 import com.aliyuncs.profile.DefaultProfile;
 import com.aliyuncs.DefaultAcsClient;
 import com.aliyuncs.IAcsClient;
 import com.aliyuncs.exceptions.ClientException;
 import com.aliyuncs.exceptions.ServerException;
 import com.aliyuncs.ecs.model.v20140526.*;
 public class Main {
     public static void main(String[] args) {
         // Create and initialize a DefaultAcsClient instance
         DefaultProfile profile = DefaultProfile.getProfile(
             "<your-region-id>",          // The region ID
             "<your-access-key-id>",      // The AccessKey ID of the RAM account
             "<your-access-key-secret>"); // The AccessKey Secret of the RAM account
         IAcsClient client = new DefaultAcsClient(profile);
         // Create an API request and set parameters
         DescribeInstancesRequest request = new DescribeInstancesRequest();
         request.setPageSize(10);
         // Initiate the request and handle the response or exceptions
         DescribeInstancesResponse response;
         try {
             response = client.getAcsResponse(request);
             for (DescribeInstancesResponse.Instance instance:response.getInstances()) {
                 System.out.println(instance.getPublicIpAddress());
             }
         } catch (ServerException e) {
             e.printStackTrace();
         } catch (ClientException e) {
             e.printStackTrace();
         }
     }
 }

Using BearerToken call

Only CCC supports this method

 package com.testprogram;
 import com.aliyuncs.profile.DefaultProfile;
 import com.aliyuncs.DefaultAcsClient;
 import com.aliyuncs.IAcsClient;
 import com.aliyuncs.exceptions.ClientException;
 import com.aliyuncs.exceptions.ServerException;
 import com.aliyuncs.ccc.model.v20170705.ListPhoneNumbersRequest;
 import com.aliyuncs.ccc.model.v20170705.ListPhoneNumbersResponse;
 public class Main {
     public static void main(String[] args) {
         // Create and initialize a DefaultAcsClient instance
         DefaultProfile profile = DefaultProfile.getProfile(
             "<your-region-id>"         // The region ID
         ); 
         BearerTokenCredentials bearerTokenCredential = new BearerTokenCredentials("<your-bearer-token>");
         DefaultAcsClient client = new DefaultAcsClient(profile, bearerTokenCredential);
         // Create an API request and set parameters
         ListPhoneNumbersRequest request = new ListPhoneNumbersRequest();
         request.getInstanceId("yourId");
         request.setOutboundOnly(true);
         // Initiate the request and handle the response or exceptions
         ListPhoneNumbersResponse response; 
         try {
             response = client.getAcsResponse(request);
         } catch (ServerException e) {
             e.printStackTrace();
         } catch (ClientException e) {
             e.printStackTrace();
         }
     }
 }

Debugging

If there is an environment variable DEBUG=sdk , all http request/response will work in debug mode.

Configure the connection pool

Multiple SDK clients share the same connection pool, configure the pool in the initialization phase of the client

// Create and initialize a DefaultAcsClient instance
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>",          // The region ID
"<your-access-key-id>",      // The AccessKey ID of the RAM account
"<your-access-key-secret>"); // The AccessKey Secret of the RAM account

// Multiple SDK clients share the same connection pool, set the
// parameters for this pool here such as maxRequestsPerHost, timeout, etc.
HttpClientConfig clientConfig = HttpClientConfig.getDefault();
clientConfig.setMaxRequestsPerHost(6);
clientConfig.setMaxRequests(60);

profile.setHttpClientConfig(clientConfig);
IAcsClient client = new DefaultAcsClient(profile);

Timeout

Request Settings -> Client Settings -> Default Settings, priority from high to low; The default ConnectTimeout is 5 seconds and the ReadTimeout is 10 seconds.

//The client setting is valid for all requests
HttpClientConfig clientConfig = HttpClientConfig.getDefault();
clientConfig.setReadTimeoutMillis(readTimeoutMillis);
clientConfig.setConnectionTimeoutMillis(connectionTimeoutMillis);
IClientProfile profile = DefaultProfile.getProfile(regionId, accesskeyId, accesskeySecret);
profile.setHttpClientConfig(clientConfig);
DefaultAcsClient client = new DefaultAcsClient(profile);

//The request setting, valid only for the current request
request.setSysReadTimeout(readTimeoutMillis);
request.setConnectionTimeoutMillis(connectionTimeoutMillis);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.