如何使用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:
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
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>
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.
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()); } }
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 updatedreason: A concise explanation for the current statusdetailedStatus: More granular context about the resource's health
- 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 loginfirst, or set up environment variables for service principal authentication.
内容的提问来源于stack exchange,提问作者Bernhard

