最近准备基于Microsoft Azure Management Libraries 实现虚拟机的监控。主要的需求就是获取虚拟机内置的性能计数器数据,基于性能计数器实现后续的监控和自动伸缩。

作为这一票研究的第一篇,我们以连接中国区的Azure作为起步吧。

通过Azure的订阅(Subscription)建立Azure的连接

首先要有连接的凭据,通过代码验证,这里主要有两种连接凭据:令牌凭据(TokenCloudCredentials)和证书凭据(CertificateCloudCredentials)。

我们主要介绍令牌凭据(TokenCloudCredentials):这里用到了Window Azure的OAuth认证,需要在Azure Manage Portal中允许我们的App访问Azure。

微软提供了一个入门的链接,不过貌似不是针对咱们天朝的,同时有代码编译不通过的问题,可以参考一下:

https://msdn.microsoft.com/en-us/zh-us/library/azure/dn722415.aspx

整体上分为三步:

  1. 在Azure AD(活动目录)中添加一个应用程序
  2. 创建Project添加Nuget应用
  3. 创建令牌连接Azure

我们一步一步来:

1. 在Azure AD中添加一个应用程序

访问https://manage.windowsazure.cn,输入用户名和密码登录,用户必须有Active Dictionary权限。

左边菜单倒数第二个Active Directory,选择对应的目录,点击应用程序(Applications)Tab选型卡,添加一个应用程序:AzureTestApp,类型是Native Client Application,Redirect URL设置为:http://localhost

记得要保存:

 2. 创建Project添加Nuget引用

这里使用Console工程好了,主要添加Microsoft Azure Management LibrariesActive Directory Authentication Library

Package文件是这样的:

3. 创建令牌连接Azure

在创建令牌之前,我们需要先配置一下App.Config,将我们的订阅、应用程序信息、Login服务器、ApiEndPoint信息等,这些信息又用到了我们刚才创建的应用程序。

微软给的msdn指导说明中是这样的:我们主要用前5个:

有个疑问,这几个key主要用在哪,后续代码中大家一看就明白了。微软给的示例说明中的URL,很明显是Azure Global的,我们需要搞成中国的URL,其中

login:https://login.chinacloudapi.cn/{0}

apiEndpoint:https://management.core.chinacloudapi.cn/

不要问我为什么,哥也是在鞠强老大的指导下,配置成这样的。

然后,ClientID、tenantID从哪找呢?subscriptionId肯定是你的订阅的ID,比如:37a8***-5107-4f9b-***-a11***0226

这样我们的App.Config就OK了,对了,还有一个redirectUri : http://localhost/

撸代码吧:

访问App.Config肯定要添加System.configuration引用。

为了方便凭据管理,我们设计一个Azure认证器类:Authorizator

namespace AzureTest
{
using System.Configuration;
using Microsoft.WindowsAzure;
using Microsoft.IdentityModel.Clients.ActiveDirectory; /// <summary>
/// Window Azure登录验证器
/// </summary>
class Authorizator
{
/// <summary>
/// 获取令牌凭据
/// </summary>
/// <returns>令牌凭据</returns>
public static TokenCloudCredentials GetCredentials(string subscriptionId = "")
{
var token = GetAccessToken();
if(string.IsNullOrEmpty(subscriptionId))
subscriptionId = ConfigurationManager.AppSettings["subscriptionId"];
var credential = new TokenCloudCredentials(subscriptionId, token); return credential;
} /// <summary>
/// 获取访问令牌
/// </summary>
/// <returns>访问令牌</returns>
private static string GetAccessToken()
{
AuthenticationResult result = null; var context = new AuthenticationContext(string.Format(
ConfigurationManager.AppSettings["login"],
ConfigurationManager.AppSettings["tenantId"])); result = context.AcquireToken(
ConfigurationManager.AppSettings["apiEndpoint"],
ConfigurationManager.AppSettings["clientId"],
new Uri(ConfigurationManager.AppSettings["redirectUri"])); if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
} return result.AccessToken;
}
}
}

上面代码中,Azure连接认证就ok了,我们测试一下,应该弹出Azure登录验证界面:

static void Main(string[] args)
{
var credential = Authorizator.GetCredentials();
client = new MonitorClient(credential);
client.GetMetricDefinitions();
Console.ReadLine();
}

至此,Azure连接就可以了,上面代码中有些监控的代码,MonitorClient,我们将在下一篇中介绍如何获取VM的监控指标和监控数据。

周国庆

@济南 2016/3

[博客迁移]探索Windows Azure 监控和自动伸缩系列1 - 连接中国区Azure的更多相关文章

  1. 探索Windows Azure 监控和自动伸缩系列1 - 连接中国区Azure

    最近准备基于Microsoft Azure Management Libraries 实现虚拟机的监控.主要的需求就是获取虚拟机内置的性能计数器数据,基于性能计数器实现后续的监控和自动伸缩. 作为这一 ...

  2. [博客迁移]探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据

    上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...

  3. 探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据

    上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...

  4. [博客迁移]探索Windows Azure 监控和自动伸缩系列3 - 启用Azure监控扩展收集自定义监控数据

    上一篇我们介绍了获取Azure的监控指标和监控数据: http://www.cnblogs.com/teld/p/5113376.html 本篇我们继续:监控虚拟机的自定义性能计数器. 随着我们应用规 ...

  5. 探索Windows Azure 监控和自动伸缩系列3 - 启用Azure监控扩展收集自定义监控数据

    上一篇我们介绍了获取Azure的监控指标和监控数据: http://www.cnblogs.com/teld/p/5113376.html 本篇我们继续:监控虚拟机的自定义性能计数器. 随着我们应用规 ...

  6. 博客迁移至http://www.maxzhang.com,欢迎访问!

    博客迁移至http://www.maxzhang.com,欢迎访问!

  7. 告示:CSDN博客通道支持Windows Live Writer写blog离线好友

    尊敬的各位CSDN用户: 您好! 为了更好的服务客户.CSDN已经支持Windows Live Writer离线写博客啦.Windows Live Writer于2014年5月29日正式上线啦!欢迎大 ...

  8. 博客迁移至 http://www.loveli.site

    对于博客园的Markdow 支持太过...,你懂的,  以后博客迁移至:http://www.loveli.site

  9. 【博客迁移】hyrepo.com

    博客迁移至 www.hyrepo.com

随机推荐

  1. LeetCode 557 Reverse Words in a String III 解题报告

    题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...

  2. 2018/05/02 PHP 之错误与异常处理

    在学习中,越学习越觉得自己基础薄弱. 在平常工作中,对于某些错误处理感觉不知道怎么下手,于是决定重新再整理一下. 强烈推荐这篇文章,真的感觉学习到了很多. 部分引用::再谈PHP错误与异常处理 -- ...

  3. NMAP执行脚本smb-check-vulns.nse出错

    错误信息:NSE: failed to initialize the script engine: /usr/bin/../share/nmap/nse_main.lua:801: ‘smb-chec ...

  4. js:return [ expression ],return false,return true,return的区别

    1.return [ expression ] return返回值实际上是对window.event.returnvalue进行设置. 2.return false,阻止默认的行为, ① 当给a标签绑 ...

  5. 【leetcode】部分思路整理

    题目: 求一个树的最小深度. 思路: 思路一:递归     若为空树返回0:     若左子树为空,则返回右子树的最小深度+1:(加1是因为要加上根这一层,下同)     若右子树为空,则返回左子树的 ...

  6. echarts给数据视图添加表格样式

    1,准备好样式 <style>.myTable {margin: 0 auto;/* height: 300px; */width: 700px;} .myTitle {backgroun ...

  7. vue中使用lodash

    1.安装:npm i --save lodash 2.引入:import _from 'lodash' 3.使用: <template> <div class="templ ...

  8. emq知识点

    1  配置用户名 默认是可以匿名登录(与mosquitto相同) ## Allow Anonymous authentication mqtt.allow_anonymous = true etc/p ...

  9. Vbox下的linux和windows共享文件设置

    参考链接: https://jingyan.baidu.com/article/2fb0ba40541a5900f2ec5f07.html

  10. js抽红包分配

    将 50000元随机分给10个人,其中3个人必须分到百位数,4个人分到千位数,3个人分到万位数,每个人所得金额 <!DOCTYPE html> <html lang="zh ...