If you are a developer, you can refer to the sample Java code provided in this topic to query the latest price of a preemptible instance.

Prerequisites

  • An Alibaba Cloud account is created and its AccessKey pair is obtained.

    You must configure the AccessKey pair of your Alibaba Cloud account to use Alibaba Cloud Elastic Compute Service (ECS) SDK for Java. For more information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.

  • ECS SDK for Java is installed in the development environment.
    You must add the following dependencies to the Maven project. For more information, see Install ECS SDK for Java.
    <dependencies>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-ecs</artifactId>
                <version>4.23.10</version>
            </dependency>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>4.0.8</version>
            </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.68</version>
            </dependency>
    </dependencies>

Sample code

In this topic, the QuerySpotLatestPrice Java class is used. The following code provides an example on how to query the latest price of a preemptible instance by calling the DescribePrice operation.
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribePriceRequest;
import com.aliyuncs.ecs.model.v20140526.DescribePriceResponse;
import com.aliyuncs.profile.DefaultProfile;

/**
 * Call the DescribePrice operation to query the latest price of a preemptible instance. 
 */
public class QuerySpotLatestPrice {
    private static IAcsClient client;
    // Specify the region ID of the preemptible instance. 
    static String regionId = "cn-hangzhou";
    // Set the network type of the preemptible instance to Virtual Private Cloud (VPC). 
    static String resourceType = "instance";
    static String instanceNetworkType = "vpc";
    // Specify the instance type of the preemptible instance. 
    static String instanceType = "ecs.g6.8xlarge";
    // Set the preemption policy to SpotAsPriceGo. 
    static String spotStrategy = "SpotAsPriceGo";
    // Set spotDuration to the protection period within which to retain the preemptible instance. If you cannot determine the protection period, set the value to 0. 
    static Integer spotDuration = 1;
    // Specify the zone ID of the preemptible instance. 
    static String zoneId = "cn-hangzhou-i";

    public static void main(String[] args) throws Exception {
        client = Initialization();
        describePrice(client);
    }

    public static void describePrice(IAcsClient client) throws Exception {
        // Set the parameters of the DescribePrice operation and send the request. 
        DescribePriceRequest request = new DescribePriceRequest();
        request.setRegionId(regionId);
        request.setResourceType(resourceType);
        request.setInstanceType(instanceType);
        request.setInstanceNetworkType(instanceNetworkType);
        request.putQueryParameter("spotStrategy",spotStrategy);
        request.putQueryParameter("spotDuration",spotDuration);
        request.putQueryParameter("zoneId",zoneId);
        // Obtain the response and the latest price of the preemptible instance. 
        DescribePriceResponse describePriceResponse = client.getAcsResponse(request);
        System.out.println("Preemptible instance price:"+describePriceResponse.getPriceInfo().getPrice().getTradePrice()+ "Yuan");
    }

    private static IAcsClient Initialization() {
        /**
         * Initialize request parameters. 
         * Set the <your-access-key-id> variable to the AccessKey ID of your Alibaba Cloud account. 
         * Set the <your-access-key-secret> variable to the AccessKey secret of your Alibaba Cloud account. 
         */
        DefaultProfile profile = DefaultProfile.getProfile(regionId, "<your-access-key-id>", "<your-access-key-secret>");
        return new DefaultAcsClient(profile);
    }
}
The following figure shows a sample response.Preemptible instance price