You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Azure Java SDK调用Resource Health REST API获取Azure资源可用性状态

Got it, let's walk through how to use the Azure Java SDK to check a resource's availability status via Resource Health. Here's a step-by-step guide that should get you sorted:

Prerequisites

Before you start, make sure you have:

  • An active Azure subscription
  • The full ID of the resource you want to check (you can grab this from the Azure Portal under the resource's Properties tab)
  • Java 8 or later installed
  • A build tool like Maven or Gradle
Step 1: Add Azure SDK Dependencies

First, include the relevant Azure SDK packages in your project. If you're using Maven, add these dependencies to your pom.xml:

<dependencies>
    <!-- Azure Resource Health Manager SDK -->
    <dependency>
        <groupId>com.azure.resourcemanager</groupId>
        <artifactId>azure-resourcemanager-resourcehealth</artifactId>
        <version>1.0.0</version> <!-- Use the latest stable version available -->
    </dependency>
    <!-- Azure Identity for authentication -->
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-identity</artifactId>
        <version>1.12.0</version> <!-- Use the latest stable version available -->
    </dependency>
</dependencies>
Step 2: Set Up Authentication

The Azure SDK uses DefaultAzureCredential to handle authentication seamlessly across environments (local dev, Azure CLI, Managed Identity, etc.). It automatically picks the best auth method based on where you're running the code.

Step 3: Write the Query Code

Here's a complete working example to fetch your resource's health status:

import com.azure.core.credential.TokenCredential;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.profile.AzureProfile;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.resourcehealth.ResourceHealthManager;
import com.azure.resourcemanager.resourcehealth.models.ResourceHealth;

public class ResourceHealthChecker {
    public static void main(String[] args) {
        // Replace with your actual resource ID
        String resourceId = "/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/{resource-provider}/{resource-type}/{resource-name}";
        
        // Configure Azure environment profile
        AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
        TokenCredential credential = new DefaultAzureCredentialBuilder()
                .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                .build();
        
        // Initialize the Resource Health manager
        ResourceHealthManager resourceHealthManager = ResourceHealthManager.authenticate(credential, profile);
        
        // Fetch the resource health status
        ResourceHealth resourceHealth = resourceHealthManager.resourceHealths().getByResource(resourceId);
        
        // Extract and print key health details
        System.out.println("Resource Availability State: " + resourceHealth.properties().availabilityState());
        System.out.println("Last Status Change Time: " + resourceHealth.properties().statusChangeTime());
        System.out.println("Status Reason: " + resourceHealth.properties().reason());
        System.out.println("Detailed Health Status: " + resourceHealth.properties().detailedStatus());
    }
}
Step 4: Interpret the Response

The ResourceHealth object gives you all critical details about your resource's availability:

  • availabilityState: The core state of the resource (e.g., Available, Unavailable, Degraded)
  • statusChangeTime: Timestamp when the status was last updated
  • reason: A concise explanation for the current status
  • detailedStatus: More granular context about the resource's health
Quick Notes
  • Ensure the identity you're using has the Resource Health Reader role (or equivalent permissions) on the target resource or subscription.
  • If running locally, log into Azure CLI with az login first, or set up environment variables for service principal authentication.

内容的提问来源于stack exchange,提问作者Bernhard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 10:42:50