阿里云首页 弹性加速计算实例EAIS

Java示例

阿里云EAIS Java SDK让您不用复杂编程即可访问阿里云弹性加速计算实例EAIS服务。本文介绍如何使用EAIS Java SDK以及阿里云核心库。

安装Java SDK

您可以使用以下两种方式:

  • (推荐)使用Maven坐标来使用Java SDK,您只需在Maven pom.xml中添加以下依赖项即可。

    <dependencies>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <optional>true</optional>
                <version>4.5.16</version>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.5</version>
            </dependency>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-eais</artifactId>
                <version>2.0.1</version>
            </dependency>
    </dependencies>
  • 访问Java SDK的Maven地址:Maven Central Repository Search,自行下载aliyun-java-sdk-eais。

Demo示例

  • 创建一个EAIS实例。

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.eais.model.v20190624.*;
    
    public class CreateEai {
    
        public static void main(String[] args) {
    
            //创建DefaultAcsClient实例并初始化。        
            DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            //创建API请求并设置参数。
            CreateEaiRequest request = new CreateEaiRequest();
            request.setRegionId("cn-beijing");
            request.setInstanceType("eais.ei-a6.2xlarge");
    
            try {
                //发起请求并处理应答或异常。
                CreateEaiResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }

    返回如下数据。

    {
     "requestId": "FDFECE42-CE82-4D3D-87CC-DD2C4DB6DBE7",
     "elasticAcceleratedInstanceId": "eais-bjdzlzro*********"
    }
  • 绑定指定EAIS实例。

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.eais.model.v20190624.*;
    
    public class AttachEai {
    
        public static void main(String[] args) {
    
            //创建DefaultAcsClient实例并初始化。
            DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            //创建API请求并设置参数。
            AttachEaiRequest request = new AttachEaiRequest();
            request.setRegionId("cn-beijing");
            request.setElasticAcceleratedInstanceId("eais-bjdzlzro*********");
            request.setClientInstanceId("i-2zeaiw86f*********");
    
            try {
                //发起请求并处理应答或异常。
                AttachEaiResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
    
        }
    }

    返回如下数据。

    {
     "requestId": "D175D01C-64C4-4125-9B05-D6212159C5AF",
     "elasticAcceleratedInstanceId": "eais-bjdzlzro*********",
     "clientInstanceId": "i-2zeaiw86f*********"
    }
  • 解绑指定EAIS实例。

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.eais.model.v20190624.*;
    
    public class DetachEai {
    
        public static void main(String[] args) {
            //创建DefaultAcsClient实例并初始化。
            DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            //创建API请求并设置参数。
            DetachEaiRequest request = new DetachEaiRequest();
            request.setRegionId("cn-beijing");
            request.setElasticAcceleratedInstanceId("eais-bjdzlzro*********");
    
            try {
    
                //发起请求并处理应答或异常。
                DetachEaiResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
    
        }
    }

    返回如下数据。

    {
     "requestId": "024CC373-0F87-4E68-8C8B-E154F693DCFD"
    }
  • 删除指定EAIS实例。

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.eais.model.v20190624.*;
    
    public class DeleteEai {
    
        public static void main(String[] args) {
    
            //创建DefaultAcsClient实例并初始化。
            DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            //创建API请求并设置参数。
            DeleteEaiRequest request = new DeleteEaiRequest();
            request.setRegionId("cn-beijing");
            request.setElasticAcceleratedInstanceId("eais-bjdzlzro*********");
    
            try {
                //发起请求并处理应答或异常。
                DeleteEaiResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
    
        }
    }

    返回如下数据。

    {
     "requestId": "1A95DC1A-62E7-46E0-B240-5F54498ED1ED"
    }