问题描述

在使用Java的启动Azure VM的过程中,遇见了com.azure.core.management.exception.ManagementException: Status code 404错误,纠起原因就是订阅无法发现。详细的错误为:

Selected subscription: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

com.azure.core.management.exception.ManagementException: Status code 404, "{"error":{"code":"SubscriptionNotFound","message":"The subscription 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' could not be found."}}": The subscription 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' could not be found.

sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

以上问题主要时在使用Java代码访问Azure资源的时候出现的权限问题有关。所以解决的办法是需要先让应用程序有权限访问到Azure VM资源,这里使用的是AAD认证。在代码中使用ApplicationTokenCredentials方式认证。

AzureTokenCredentials credentials = new ApplicationTokenCredentials("clientid", "tenantid", "clientsecret", AzureEnvironment.AZURE_CHINA);
Azure azure = Azure.authenticate(credentials).withSubscription("subid");
VirtualMachine testvm = azure.virtualMachines().getById("/subscriptions/<your subscription id>/resourceGroups/<group name>/providers/Microsoft.Compute/virtualMachines/<vm name>");
  • 通过AAD中注册应用的client id, tenantid,secret获取token认证
  • 输入订阅号登录到Azure
  • 在Azure VM门户中获取到该VM的属性ID

(获取以上值的方式见附录一)

完整的代码内容

package org.example;

import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.credentials.AzureTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.*; /**s
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) {
AzureTokenCredentials credentials = new ApplicationTokenCredentials("clientid", "tenantid", "clientsecret", AzureEnvironment.AZURE_CHINA);
Azure azure=null; azure=Azure.authenticate(credentials).withSubscription("subid"); VirtualMachine testvm = azure.virtualMachines().getById("vmresourceid");
testvm.start(); System.out.println( "Hello World!" );
}
}

POM.XML文件内容(使用的依赖包:com.microsoft.azure):

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>getvm</artifactId>
<version>1.0-SNAPSHOT</version> <name>getvm</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.37.1</version>
</dependency>
</dependencies> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

附录一:如何获取代码中需要的配置值:

一:获取 AAD中应用的Client ID, Tenant ID

  • 登录Azure 门户,进入AAD页面(https://portal.azure.cn/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/Overview)
  • 选择“App registrations”,如已经有App,则直接选中即可进入下一步。否则点击“Add Application”按钮,注册一个新应用,名称唯一即可。如mykeyvaultapp01,,
  • 创建完成后,进入应用Overview页面,复制保存(Client ID, Tenant ID, Object ID)值,以备后续使用。

二:获取AAD中应用的Secret

  • 在AAD应用页面,进入“证书和密码”页面,点击“新客户端密码”按钮,添加新的Secret(因密码值只能在最开始创建时可见,所以必须在离开页面前复制它

三:获取Azure VM的Resource ID

参考文档

使用 Java 创建和管理 Azure 中的 Windows VM: https://docs.azure.cn/zh-cn/virtual-machines/windows/java

【Azure Developer】使用Java代码启动Azure VM(虚拟机)的更多相关文章

  1. 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)

    关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...

  2. 【Azure 存储服务】代码版 Azure Storage Blob 生成 SAS (Shared Access Signature: 共享访问签名)

    问题描述 在使用Azure存储服务,为了有效的保护Storage的Access Keys.可以使用另一种授权方式访问资源(Shared Access Signature: 共享访问签名), 它的好处可 ...

  3. DataX通过纯Java代码启动

    DataX是阿里巴巴团队开发的一个很好开源项目,但是他们对如何使用只提供了python命令启动方式,这种方式对于只是想简单的用下DataX的人来说很是友好,仅仅需要几行代码就可以运行,但是如果你需要在 ...

  4. 如何使用java代码启动tomcat和打开浏览器

    1.用于代码启动tomcat,也可以用代码运行电脑应用程序 public static void main(String[] args) { /* new MyThread().start(); ne ...

  5. 【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization

    Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, ...

  6. Java 18 新特性:使用Java代码启动jwebserver

    前几天分享了Java 18 新特性:简单Web服务器的jwebserver命令行功能. 今天换一种方式,使用Java代码来实现一个静态资源服务器. 详细步骤我录了个视频放到B站了,感兴趣的小伙伴可以点 ...

  7. Java代码启动/关闭进程

    ProcessBuilder builder = new ProcessBuilder(命令,参数,参数...); Process process = builder.start(); br = ne ...

  8. 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例

    当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...

  9. 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code

    问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...

随机推荐

  1. 【Azure 云服务】如何从Azure Cloud Service中获取项目的部署文件

    问题描述 在历史已经部署的云服务(Azure Cloud Service)中,如何获取到项目在很久以前的部署包文件呢? 解决办法 1)如果部署云服务是通过门户上传部署包到存储账号中,则可以直接从存储账 ...

  2. DOS打开方式

    一.CMD窗口 Ⅰ.普通身份 按下键盘组合键:Windows + R --> 出现'运行'窗口,在'打开'的输入框中输入'cmd' --> 点击确定 打开'开始'菜单 --> 找到' ...

  3. uniapp封装uni.request请求

    封装一个uniapp请求 新建一个http.js文件封装uni.request const BASE_URL = process.env.NODE_ENV === 'development' ? '' ...

  4. 单细胞分析实录(9): 展示marker基因的4种图形(二)

    在上一篇中,我已经讲解了展示marker基因的前两种图形,分别是tsne/umap图.热图,感兴趣的读者可以回顾一下.这一节我们继续学习堆叠小提琴图和气泡图. 3. 堆叠小提琴图展示marker基因 ...

  5. 剑指 Offer 33. 二叉搜索树的后序遍历序列 + 根据二叉树的后序遍历序列判断对应的二叉树是否存在

    剑指 Offer 33. 二叉搜索树的后序遍历序列 Offer_33 题目详情 题解分析 本题需要注意的是,这是基于一颗二叉排序树的题目,根据排序二叉树的定义,中序遍历序列就是数据从小到大的排序序列. ...

  6. Lua生成Guid(uuid)

    全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) .GUID是一种由算法生成的二进制长度为 ...

  7. MongoDB4.2 副本集扫盲说明

    说明: 在扫盲MongoDB相关的一些知识的时候,顺手做下笔记.本文将说明副本集相关的内容.在比较早之前已经对这些有过说明,可以看MongoDB 副本集的原理.搭建.应用.MongoDB中的副本集是一 ...

  8. git clone 提速

    将类似于 git clone https://github.com/graykode/nlp-tutorial 的命令改成 https://github.com.cnpmjs.org/graykode ...

  9. Spring笔记(10) - 日志体系

    一.概况 在项目开发当中,日志对于我们开发或运维人员来说,是一个必不可少的工具.在线下我们可以通过 debug 来查找排除问题,但对于线上系统来说,我们只能通过日志分析来查找问题,我们可以通过日志打印 ...

  10. PHP配置 4. 虚拟主机配置open_basedir

    将/usr/local/php/etc/php.ini中open_basedir注释掉,编辑虚拟主机配置open_basedir #vim /usr/local/apache2 .4/conf/ext ...