Azure Java SDK - 在何处获取新门户上的 serviceName 和 deploymentName 的值

Azure Java SDK - Where to get values for serviceName and deploymentName on new Portal

我正在尝试使用 Azure Java SDK 为我的 azure 虚拟机自动执行任务,例如在一天中的不同阶段启动和停止它们

我正在查看有关启动虚拟机的 azure 文档 here

问题方法签名如下

OperationResponse beginStarting(java.lang.String serviceName,
                            java.lang.String deploymentName,
                            java.lang.String virtualMachineName)
                     throws java.io.IOException,
                            ServiceException

我想知道 - 在我的虚拟机的 Azure 控制台上,我在哪里可以获得 serviceName 和 deploymentName 的值?

我尝试查看旧门户和新门户,但迄今为止我一直无法找到这些值

这个问题看起来与下面 url 中的问题重复,但它不是

确实,当您查看旧门户网站时 (https://manage.windowsazure.com) - 以上 link 给出了获取部署名称的正确答案

但是,如果您在旧门户中创建 VirtualMachine 并在新门户 (https://portal.azure.com) 中查看它 - 它会显示在 Virtual Machine Classic 选项下。借助以上 link 我能够使用 Java SDK

在经典虚拟机上执行操作

如果我在新门户中的虚拟机选项(不是虚拟机(经典))下创建虚拟机,我找不到 deploymentName 或 serviceName

因此更新问题 - 如何在 new 门户

中找到虚拟机的 deploymentName 和 serviceName

另外 - 我注意到 SDK 的另一件事 - 如果你输入了错误的服务名称 - 它会记录部署名称错误 - 这让我在经典虚拟机上花了一段时间

只是为了结束这个问题 为了能够在非经典虚拟机上执行操作——使用这个 maven 依赖项

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-mgmt-compute</artifactId>
    <version>0.9.0</version>
</dependency>

对于非经典虚拟机上的操作 - 您需要使用 Active Directory Security - 请参阅此 link - https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/ 要在经典虚拟机上执行操作 - 使用此 maven 依赖项

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-svc-mgmt-compute</artifactId>
    <version>0.9.0</version>
</dependency>

对于经典虚拟机上的操作 - 您需要使用证书

我没有使用过 Java SDK(所以我在这里可能完全没有根据)但我不认为你可以使用这个库在 non-classic 虚拟机上执行操作.这些 VM 是通过 Azure 资源管理器部署的,它们具有完全不同的资源管理机制。

在此处查看源代码:https://github.com/azure/azure-sdk-for-java, I believe this is where you will find the methods to manage Virtual Machines: https://github.com/Azure/azure-sdk-for-java/tree/master/resource-management/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute

对于您的具体查询,请参阅此处的文档:http://azure.github.io/azure-sdk-for-java/com/microsoft/azure/management/compute/VirtualMachineOperations.html#beginStarting-java.lang.String-java.lang.String- (This is the place where you will find entire documentation - http://azure.github.io/azure-sdk-for-java/)。

最近 azure 发布了 Java SDK 1.0.0 作为 LTS 版本。使用那个 SDK。

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    <version>1.0.0</version>
</dependency>

启动和停止虚拟机

    ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, tenantId, clientKey, AzureEnvironment.AZURE);
    Azure azure =  Azure.authenticate(credentials).withSubscription(subscriptionId);

azure.virtualMachines().start("resourceGroupName", "vmName");
azure.virtualMachines().powerOff("resourceGroupName", "vmName");

    azure.virtualMachines().getByResourceGroup("resourceGroupName", "vmName").start();
    azure.virtualMachines().getByResourceGroup("resourceGroupName", "vmName").powerOff();
        
        

但这些都是阻塞调用。您可以使用 startAsync() 方法以异步方式启动它。