最近准备基于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

设置AzureTestApp的权限:Windows Azure Service Management API

记得要保存:

 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的监控指标和监控数据。

周国庆

@济南

探索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. 安装 Visual Studio,连接中国区 Azure

    中国数据中心 目前,中国区 Azure 有两个数据中心,在位置字段中显示为“中国北部”和“中国东部”. 在 Azure 上创建应用程序的区别 在中国区 Azure 上开发应用程序与在境外 Azure ...

  7. 探索 Windows Azure 网站中的自动伸缩功能

     去年10月,我们发布了若干针对 WindowsAzure平台的更新,其中一项更新是添加了基于日期的自动伸缩调度支持(在不同的日期设置不同的规则). 在这篇博客文章中,我们将了解自动伸缩的概念,并 ...

  8. Windows Azure 自动伸缩已内置

     WindowsAzure平台提供的主要优点之一是能够在有需要时快速缩放云中的应用程序以响应波动.去年7月以前,您必须编写自定义脚本或使用其他工具(如Wasabi或MetricsHub)来启用自动 ...

  9. 中国区 Azure 应用程序开发说明

    1.文档简介 微软公司为其在境外由微软运营的 Azure 服务(以下简称为 “境外 Azure”),创建和部署云应用程序,提供了相应工具. 在中国,由世纪互联运营的 Microsoft Azure ( ...

随机推荐

  1. hdu4004(二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 大致题意 二分最大跳跃能力,判断是否可以在m次内到达对岸! 分析:由于求青蛙最小弹跳能力,所以二 ...

  2. Linux下精确控制时间的函数

    Linux下精确控制时间的函数 在测试程序接口运行时间的时候,常用time,gettimeofday等函数,但是这些函数在程序执行的时候是耗费时间的,如果仅仅测试时间还行,但是如果程序中用到时间控制类 ...

  3. 微信简单Demo

    新建一个WxHandler.ashx public class WxHandler : IHttpHandler { public static string Msg; public void Pro ...

  4. A Game of Thrones(3) - Daenerys

    Her brother held the gown up for her inspection. “This is beauty. Touch it. Go on. Caress(爱抚,抚抱) the ...

  5. 深入了解HTTP协议、HTTP协议原则

    ttp协议学习系列 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写. 它的发展是万维网协会(World Wide Web C ...

  6. html 跳转页面,同时跳转到一个指定的位置

    比如我现在 a.html 的时候,我想跳转到 b.html ,并且是 b.html 的某一个位置,用 <a href=>, a.html里: <a href="b.html ...

  7. 设计模式初探3——装饰者模式(Decorator Pattern)

    装饰者模式:动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 适用范围: 1. 须要扩展一个类的功能.或给一个类加入附加职责. 2. 须要动态的给一个对象加入功能,这些功 ...

  8. QT Programming 1

    1.控制台输出 helloworld #include<QtCore/QCoreApplication> #include<QDebug> int main(int argc, ...

  9. iOS随机颜色

    #import <UIKit/UIKit.h> @interface UIColor (RandomColor) +(UIColor *) randomColor; @end #impor ...

  10. 第三章 AOP 基于Schema的AOP

    基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已. 3.7.1一般增强的使用 a.目标类 public class Target { public void ...