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

如何通过Java Azure SDK为虚拟机规模集指定镜像版本

解决Azure Java SDK更新虚拟机规模集(VMSS)镜像版本的问题

你提到的AZ CLI命令是正确的,它通过--set直接修改镜像版本属性。对于Azure Resource Manager Java SDK,由于SDK的更新构建器采用分层设计,无法直接调用withVirtualMachineProfileStorageProfile方法,正确的实现需要逐层更新配置:

正确代码示例

// 资源组和规模集名称
final String resourceGroupName = "myResourceGroup";
final String scaleSetName = "myScaleSet";

// 获取目标VMSS实例
VirtualMachineScaleSet scaleSet = azureResourceManager.virtualMachineScaleSets()
        .getByResourceGroup(resourceGroupName, scaleSetName);

// 构建更新参数并执行更新
VirtualMachineScaleSet updatedScaleSet = scaleSet.update()
        // 进入虚拟机配置更新层级
        .withVirtualMachineProfile(
            scaleSet.update().virtualMachineProfile()
                // 进入存储配置更新层级
                .withStorageProfile(
                    scaleSet.update().virtualMachineProfile().storageProfile()
                        // 设置新的镜像版本
                        .withImageReference(new ImageReference()
                            .withVersion("0.235.102011129"))
                        .parent()
                )
                .parent()
        )
        .apply();

关键说明

  • Azure Java SDK的更新构建器采用分层链式设计,需要从VMSS顶层逐步进入virtualMachineProfile,再到storageProfile,最后修改imageReference的版本属性,避免直接替换整个配置对象导致其他属性被覆盖。
  • 若仅需修改镜像版本,无需替换完整的storageProfile或virtualMachineProfile,上述方式可精准更新目标属性。
  • 确保使用的Azure Java SDK为最新稳定版,不同版本的API可能存在细微差异,需对应版本的官方API文档确认方法细节。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 01:07:39