Applicaotn  Insigths 使用 Application Maps 构建请求链路视图

构建系统时,请求的逻辑操作大多数情况下都需要在不同的服务,或接口中完成整个请求链路。一个请求可以经历多个组件,极有可能出现客户端请求站点1,站点1请求站点2, … 站点N才是最终处理数据然后依次返回。

在这样的情况,如果有一个直观的视图来展示请求在每一个站点上的状态(成功,失败),当问题发生时,非常有帮助定位问题发生的地点。借助Azure 应用程序见解(Application Insights)中的遥测关联建立的Application Maps就能实现

效果展示

实现原理

Application Insights定义了用于分配遥测关联的数据模型,每个传出操作(例如,对另一个组件的 HTTP 调用)是由依赖项遥测表示的。 依赖项遥测也定义了自身的全局独一无二的 id,此依赖项调用发起的请求遥测将此 id 用作其 operation_parentId。通过operation_Id、operation_parentId 和 request.id,即可以生成分布式逻辑操作的视图 (这些字段也定义了遥测调用的因果关系顺序)。

实例构建Application Maps

在实例中,这一个请求进行了4段转发。

第一段:本地代码访问APIM(test01.azure-api.cn),

第二段:APIM访问站点1(lbphptest.chinacloudsites.cn)

第三段:站点1访问站点2(lbjavatest.chinacloudsites.cn)

第四段:站点2访问最终处理请求的Azure Function(Http Trigger)( functionapp120201013155425.chinacloudsites.cn).

准备条件

  • 创建Application Insights (lbphptest202011050549)
  • 创建APIM(test01)
  • 创建两个App Service (lbphptest 和lbjavatest)
  • 创建一个Azure Function(functionapp120201013155425)

:如不熟悉如何创建以上资源,可以导航到文末的参考资料部分

步骤一:在Azure Funciton中启用并关联Application Insights服务

进入Azure Funciton门户,选择Application Insights功能,根据页面提示选择已创建好的Application Insights (lbphptest202011050549).

创建的Function为默认的HttpTrigger模式,测试目的,代码可以不需任何修改。参考下图获取到Function的URL,用于下一步在站点2中调用。

步骤二:在站点2(lbjavatest)中启用并关联Application Insights服务,并部署代码请求Azure Function

进入站点2的门户页面,在Application Insights目录中根据提示Enable Application Insights并选择相同的Application Insights。

部署代码调用步骤一中的Azure Function

        //Level 3
[HttpGet]
[Route("[Action]")]
public string Fun([FromQuery] string name)
{
using (HttpClient httpClient = new HttpClient())
{
var url = $"https://functionapp120201013155425.chinacloudsites.cn/api/HttpTrigger1?name={name}";
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
httpRequest.Headers.Add("Accept", "application/json, text/plain, */*"); var response = httpClient.SendAsync(httpRequest).Result;
string responseContent = response.Content.ReadAsStringAsync().Result; return responseContent;
}
}

步骤三:在站点1(lbphptest)中启用并关联Application Insights服务,并部署代码请求站点2

进入站点1的门户页面,在Application Insights目录中根据提示Enable Application Insights并选择相同的Application Insights。

部署代码调用步骤二中的站点2的URL,代码与访问Azure Function相似。

        //Level 2
[HttpGet]
[Route("[Action]")]
public string FunSub([FromQuery] string name)
{
using (HttpClient httpClient = new HttpClient())
{
var url = $"https://lbjavatest.chinacloudsites.cn/WeatherForecast/fun?name={name}";
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
httpRequest.Headers.Add("Accept", "application/json, text/plain, */*"); var response = httpClient.SendAsync(httpRequest).Result;
string responseContent = response.Content.ReadAsStringAsync().Result; return responseContent;
}
}

步骤四:在APIM中启用并关联Application Insights服务, 并设置API访问站点1

进入APIM的门户页面,在Application Insights目录中添加相同的Application Insights。

在APIM配置API访问站点1(lbphptest)

  • 点击“Add API” 按钮
  • 选择从App Service中创建
  • 选择站点1(lbphptest)

在接口中添加操作一个新操作,访问站点1中的接口/weatherforecast/funsub?name={name}

步骤五:在ASP.NET Core代码中添加Application Insights SDK并配置连接字符串,在接口中访问APIM.

在本地代码中添加Application Insights SDK

配置连接字符串(字符串中Application Insights的Overview页面复制)

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=xxx-xxx-xxx-xxx-xxxxx;EndpointSuffix=applicationinsights.azure.cn;IngestionEndpoint=https://chinaeast2-0.in.applicationinsights.azure.cn/"
},
"AllowedHosts": "*"
}

启动本地程序,并通过在浏览器中访问 https://localhost:44323/weatherforecast/Funsubfornt?name=test from local -- apim -- app 1 – app 2 -- function

查看最终效果图:

【END】

参考文档:

在 Azure 门户中创建第一个函数https://docs.azure.cn/zh-cn/azure-functions/functions-create-first-azure-function

适用于 ASP.NET Core 应用程序的 Application Insightshttps://docs.azure.cn/zh-cn/azure-monitor/app/asp-net-core

关于 API 管理https://docs.azure.cn/zh-cn/api-management/api-management-key-concepts

在 Azure 中创建 ASP.NET Core Web 应用https://docs.azure.cn/zh-cn/app-service/quickstart-dotnetcore?pivots=platform-linux

最新文章

  1. PHP验证用户登录例子-学习笔记
  2. 基于Libevent的HTTP Server
  3. python的编码判断_unicode_gbk/gb2312_utf8(附函数)
  4. slim框架中防止crsf攻击时,用到的函数hash_equals
  5. Android 采用HttpClient提交数据到服务器
  6. vector的应用
  7. jQuery Mobile_页面事件
  8. [HDOJ2602]Bone Collector(01背包)
  9. 基于W5500的嵌入式SNMP代理端实现
  10. H.264码流结构解析
  11. DJANGO技巧两则:模拟MKDIR -P及配合NGINX上传大文件不使超时
  12. 算法导论学习-RED-BLACK TREE
  13. 刚開始学习的人制作VMOS场效应管小功放
  14. c语言可变参函数探究
  15. CSS单行、多行文本溢出显示省略号(……)
  16. mongoDB之集合操作
  17. ASP.NET Identity V2简单介绍
  18. (转)JavaWeb学习之Servlet(二)----Servlet的生命周期、继承结构、修改Servlet模板
  19. 牛客寒假算法基础集训营6 J-迷宫
  20. RISC与CISCCPU构架

热门文章

  1. 64位Win7下H3C的iMC无法查看“网络拓扑”的解决方法、心路历程
  2. spring-boot-route(九)整合JPA操作数据库
  3. Zyan Drench,支持Wifi的Android游戏
  4. 探索与英特尔XDK
  5. centos7 yum 安装nodejs、npm、cnpm、pm2、yarn
  6. 网络IO模型-异步选择模型(Delphi版)
  7. MeteoInfoLab脚本示例:TRMM 3B43 HDF数据
  8. JS获取指定月份的天数几种方法
  9. go 爬取页面保存
  10. centos8平台redis cluster集群添加/删除node节点(redis5.0.7)