你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Java 为虚拟机启用 Azure Automanage

Azure Automanage 允许用户将 Azure 最佳做法无缝应用到其虚拟机。 本快速入门指南将帮助你使用 azure-sdk-for-java 存储库将最佳做法配置文件应用到现有虚拟机。

先决条件

注意

免费试用帐户无法访问本教程中使用的虚拟机。 请升级为即用即付订阅。

重要

必须在包含 VM 的资源组上具有“参与者”角色,才能启用 Automanage。 如果是首次对订阅启用 Automanage,则需要以下权限:订阅的“所有者”角色或“参与者”以及“用户访问管理员”角色 。

添加必需的依赖项

将 Azure Identity 和 Azure Automanage 依赖项添加到 pom.xml

<!-- https://mvnrepository.com/artifact/com.azure/azure-identity -->
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.6.0-beta.1</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.azure.resourcemanager/azure-resourcemanager-automanage -->
<dependency>
    <groupId>com.azure.resourcemanager</groupId>
    <artifactId>azure-resourcemanager-automanage</artifactId>
    <version>1.0.0-beta.1</version>
</dependency>

向 Azure 进行身份验证并创建 Automanage 客户端

使用 Azure Identity 包向 Azure 进行身份验证,然后创建 Automanage 客户端:

AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
TokenCredential credential = new DefaultAzureCredentialBuilder()
    .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
    .build();

AutomanageManager client = AutomanageManager
    .authenticate(credential, profile);

为现有虚拟机启用最佳做法配置文件

String configProfile = "/providers/Microsoft.Automanage/bestPractices/AzureBestPracticesProduction";

client
    .configurationProfileAssignments()
    .define("default") // name must be default
    .withExistingVirtualMachine("resourceGroupName", "vmName")
    .withProperties(
        new ConfigurationProfileAssignmentProperties()
            .withConfigurationProfile(configProfile))
    .create();

后续步骤

通过访问 azure-sdk-for-java 存储库,了解如何使用 Java Automanage 客户端执行更多操作。