0

我一直在尝试将 Azure VM 部署到可用区,如以下链接https://azure.microsoft.com/en-us/blog/java-manage-availability-zones-and-more/,但我继续收到以下错误。

cannot find symbol 
symbol:   method withAvailabilityZone(AvailabilityZoneId)
  location: interface WithCreate

似乎 Java 找不到 withAvailabilityZone 方法,但在上面的链接中它似乎工作正常。当我查看 Azure 文档时,唯一的 withAvailabilityZone 方法在 withManagedCreate 类中,所以我不确定如何更改以下代码以匹配它:

VM_1 = azure.virtualMachines().define(name)
                    .withRegion(reg)
                    .withExistingResourceGroup(rg)
                    .withExistingPrimaryNetworkInterface(nI)
                    .withPopularLinuxImage(pli) 
                    .withExistingDataDisk(dd)
                    .withSize(type_1)
                    .withPriority(priority_var)
                    .withAvailabilityZone(availabilityZone) //error occurs here
                    .create();
4

1 回答 1

0

如果你想用java在Avilability Zone创建Azure VM,请参考以下步骤

  1. SDK
<dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-identity</artifactId>
      <version>1.2.4</version>
    </dependency>
    
    <dependency>
      <groupId>com.azure.resourcemanager</groupId>
      <artifactId>azure-resourcemanager</artifactId>
      <version>2.2.0</version>
    </dependency>

2.代码

 String clientId="";
        String clientSecret="";
        String tenant="";
        String subId="";
        AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
        TokenCredential credential = new ClientSecretCredentialBuilder()
                .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                .tenantId(tenant)
                .clientId(clientId)
                .clientSecret(clientSecret)
                .build();
        AzureResourceManager azureResourceManager = AzureResourceManager
                .configure()
                .withLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)
                .authenticate(credential,profile)
                .withSubscription(subId);


        VirtualMachine virtualMachine1 = azureResourceManager.virtualMachines()
                .define(vmName1)
                .withRegion(region)
                .withNewResourceGroup(rgName)
                .withNewPrimaryNetwork("10.0.0.0/28")
                .withPrimaryPrivateIPAddressDynamic()
                .withNewPrimaryPublicIPAddress(pipName1)
                .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
                .withRootUsername(userName)
                .withRootPassword(password)
                // Optional
                .withAvailabilityZone(AvailabilityZoneId.ZONE_1)
                .withSize(VirtualMachineSizeTypes.fromString("Standard_D2a_v4"))
                // Create VM
                .create();
        System.out.println("Created a zoned virtual machine: " + virtualMachine1.id());

在此处输入图像描述

于 2021-04-26T05:18:39.523 回答