{"id":202363,"date":"2025-05-10T16:26:44","date_gmt":"2025-05-10T08:26:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202363\/"},"modified":"2025-05-10T16:26:44","modified_gmt":"2025-05-10T08:26:44","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e4%bb%a3%e7%a0%81%e8%8e%b7%e5%8f%96azure-redis%e7%9a%84%e7%9b%91%e6%8e%a7%e6%8c%87%e6%a0%87%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202363\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u4ee3\u7801\u83b7\u53d6Azure Redis\u7684\u76d1\u63a7\u6307\u6807\u503c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528Python\u4ee3\u7801\u83b7\u53d6Azure Redis\u7684\u76d1\u63a7\u6307\u6807\u503c<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528Python\u4ee3\u7801\u83b7\u53d6Azure Redis\u7684\u76d1\u63a7\u6307\u6807\u503c\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<h2>\u95ee\u9898\u63cf\u8ff0<\/h2>\n<p>\u901a\u8fc7Metrics\u76d1\u63a7\u9875\u9762\uff0c\u6211\u4eec\u80fd\u5f97\u77e5\u5f53\u524d\u8d44\u6e90(\u5982Redis)\u7684\u8fd0\u884c\u60c5\u51b5\u4e0e\u5404\u79cd\u6307\u6807\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u628a\u6307\u6807\u4e0b\u8f7d\u5230\u672c\u5730\u6216\u8005\u751f\u6210JSON\u6570\u636e\u5bfc\u5165\u5230\u7b2c\u4e09\u65b9\u7684\u76d1\u63a7\u5e73\u53f0\u5462\uff1fAzure\u662f\u5426\u53ef\u4ee5\u901a\u8fc7Python\u4ee3\u7801\u6216\u8005\u65f6Powershell\u811a\u672c\u5bfc\u51fa\u5404\u79cd\u6307\u6807\u6570\u636e\u5462\uff1f<\/p>\n<h2><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138853406627d09c21769.png\" class=\"aligncenter\"><\/h2>\n<h2>\u89e3\u51b3\u529e\u6cd5<\/h2>\n<p><strong>\u53ef\u4ee5\uff01&nbsp; &nbsp; &nbsp; &nbsp;<\/strong><strong>PowerShell\u547d\u4ee4\u53ef\u4ee5\u4f7f\u7528Get-AzMetric \u6216\u8005\u662f&nbsp;az monitor metrics list\u547d\u4ee4\u6765\u83b7\u53d6\u8d44\u6e90\u7684Metrics\u503c\u3002<\/strong><\/p>\n<ul>\n<li>\n<p><strong>Get-AzMetric\uff1a<\/strong>Gets the metric values of a resource.&nbsp;https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/az.monitor\/get-azmetric?view=azps-5.4.0&amp;viewFallbackFrom=azps-5.2.0<\/p>\n<\/li>\n<li>\n<p><strong>&nbsp;az monitor metrics list:&nbsp;<\/strong>List the metric values for a resource.&nbsp;https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/monitor\/metrics?view=azure-cli-latest#az_monitor_metrics_list<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u800c\u4f7f\u7528Python\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528Metrics\u7684REST API\u6765\u5b9e\u73b0<\/strong><\/p>\n<ul>\n<li>\n<p><strong>Metrics \u2013 List<\/strong>\uff1aLists the metric values for a resource. https:\/\/docs.microsoft.com\/en-us\/rest\/api\/monitor\/metrics\/list<\/p>\n<\/li>\n<li>\n<p>\u5728AAD\u4e2d\u6ce8\u518c\u5e94\u7528\u83b7\u53d6\u5728Python\u4ee3\u7801\u4e2d\u8bbf\u95eeRedis Metrics\u7684Access Token: (\u5c06\u5e94\u7528\u7a0b\u5e8f\u6ce8\u518c\u5230 Microsoft \u6807\u8bc6\u5e73\u53f0: https:\/\/docs.azure.cn\/zh-cn\/active-directory\/develop\/quickstart-register-app)<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u6ce8\uff1a\u4f7f\u7528Powershell\u5fc5\u987b\u5148\u767b\u5f55\u5230Azure\u3002\u4f7f\u7528\u547d\u4ee4&nbsp;<\/strong>Connect-AzAccount -Environment AzureChinaCloud<strong>&nbsp;\u6216&nbsp;<\/strong>az cloud set \u2013name AzureChinaCloud&nbsp; \u548c az login\u3002<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp;<strong>\u4f7f\u7528Python\u4ee3\u7801\u5219\u9700\u8981\u5148\u83b7\u53d6\u5230\u8bbf\u95eeRedis Metrics\u7684Token\u3002\u83b7\u53d6Token\u53ef\u4ee5\u5728Azure AD\u4e2d\u6ce8\u518c\u4e00\u4e2a\u5e94\u7528\uff0c\u7136\u540e\u7ed9\u8be5\u5e94\u7528\u5728Redis\u7684\u8bbf\u95ee\u63a7\u5236\u4e2d\u8d4b\u4e88reader\u7684\u6743\u9650\u5373\u53ef\u8bfb\u53d6Metris\u6570\u636e\u3002<\/strong><\/p>\n<h2>\u6267\u884c\u6b65\u9aa4<\/h2>\n<h3>Python<\/h3>\n<h3>\u6b65\u9aa4\u4e00\uff1a\u6ce8\u518cAAD\u5e94\u7528\uff0c\u590d\u5236\u5e94\u7528ID\uff0c\u5ba2\u6237\u7aef\u8bbf\u95ee\u5bc6\u7801<\/h3>\n<ul>\n<li>\n<p>&nbsp;\u767b\u5f55Azure\u5e73\u53f0\uff0c\u8fdb\u5165AAD\u9875\u9762\uff0c\u70b9\u51fb<strong>App registrations<\/strong>\uff1a&nbsp;https:\/\/portal.azure.cn\/?l=en.en-us#blade\/Microsoft_AAD_IAM\/ActiveDirectoryMenuBlade\/RegisteredApps<\/p>\n<\/li>\n<li>\n<p>\u70b9\u51fb\u201c<strong>New Registration<\/strong>\u201d \u6309\u94ae\uff0c\u8f93\u5165\u5e94\u7528\u540d\u79f0\uff0c\u5176\u4ed6\u503c\u4fdd\u7559\u9ed8\u8ba4\uff0c\u70b9\u51fb\u4fdd\u5b58<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u6210\u529f\u540e\uff0c\u8fdb\u5165\u5e94\u7528\u9875\u9762\uff0c\u5bfc\u5165\u5230\u201c<strong>Certificates &amp; secrets<\/strong>\u201d\u9875\u9762\uff0c\u521b\u5efa\u9700\u8981\u4f7f\u7528\u7684Client Secret\u5e76\u590d\u5236\u51fa\u6765\uff0c\u7b2c\u4e09\u6b65\u9700\u8981\u4f7f\u7528<\/p>\n<\/li>\n<li>\n<p>\u5728\u5e94\u7528\u9875\u9762\u590d\u5236\u51fa<strong>Tenant ID, Applicaiton ID<\/strong>\u9700\u8981\u5728\u7b2c\u4e09\u6b65\u4ee3\u7801\u4e2d\u4f7f\u7528<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u5177\u4f53\u64cd\u4f5c\u8fc7\u7a0b\u89c1\u5982\u4e0b\u52a8\u56fe\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138853406627d09c251f0.png\" class=\"aligncenter\"><\/p>\n<h3>\u6b65\u9aa4\u4e8c\uff1a\u8d4b\u4e88\u83b7\u53d6Metrics\u7684\u6743\u9650<\/h3>\n<p>\u5728Redis\u7684Access control (IAM)\u9875\u9762\u4e2d\uff0c\u901a\u8fc7\u6b65\u9aa4\u4e00\u7684\u5e94\u7528\u540d\u641c\u7d22\u5e76\u8d4b\u4e88Monitoring Reader\u6743\u9650<\/p>\n<p>&nbsp;<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138853406627d09c32bc3.png\" class=\"aligncenter\"><\/p>\n<p>\u6ce8\uff1a\u5982\u6ca1\u6709\u8d4b\u4e88\u6743\u9650\uff0c\u5219\u4ee3\u7801\u4e2d\u4f1a\u62a5\u51fa\u7c7b\u4f3c\u9519\u8bef\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>Status Code: &lt;Response [403]&gt;<br \/>Response Content: b'{\u201cerror\u201d:{\u201ccode\u201d:\u201dAuthorizationFailed\u201d,\u201dmessage\u201d:\u201dThe client \u2018xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\u2019 with object id \u2018xxxxxxxx-xxxx-xxxx-xxxx-36166b5f7276\u2019 does not have authorization to perform action \u2018microsoft.insights\/metrics\/read\u2019 over scope \u2018\/subscriptions\/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\/resourceGroups\/xxxx-rg\/providers\/Microsoft.Cache\/Redis\/xxxx\/providers\/microsoft.insights\u2019 or the scope is invalid. If access was recently granted, please refresh your credentials.\u201d}}\u2019<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u6b65\u9aa4\u4e09\uff1a\u7f16\u5199Python\u4ee3\u7801\uff0c\u4f7f\u7528requests\u6765\u53d1\u9001psot\uff0cget\u8bf7\u6c42<\/h3>\n<ul>\n<li>\n<p>\u4ee3\u7801\u4e2d\u4e3b\u8981\u6709\u4e24\u90e8\u5206\u5185\u5bb9\uff1a\u4e00\u662f\u83b7\u53d6Access Token\uff0c\u4e8c\u662f\u83b7\u53d6Metrics Data<\/p>\n<\/li>\n<li>\n<p>\u9ad8\u4eae\u4e2d\u7684\u5185\u5bb9\u90fd\u662f\u9700\u8981\u66ff\u6362\u6210\u76f8\u5e94\u7684\u8d44\u6e90\u4fe1\u606f\u548c\u7b2c\u4e00\u6b65\u4e2d\u51c6\u5907\u7684\u4fe1\u606f<\/p>\n<\/li>\n<li>\n<p>\u5728\u83b7\u53d6Access Token\u7684Body\u5185\u5bb9\u4e2d\uff0cgrant_type\u662f\u56fa\u5b9a\u503c\uff0c\u4e3aclient_credentials\u3002resource\u7684\u503c\u4e3a\u4e2d\u56fd\u533aazure\u7684\u7ba1\u7406\u7ec8\u7ed3\u70b9\uff1a<strong>https:\/\/management.chinacloudapi.cn<\/strong><\/p>\n<\/li>\n<\/ul>\n<pre>import&nbsp;requestsimport&nbsp;json##Part&nbsp;1:&nbsp;Get&nbsp;Access&nbsp;Tokenaadurl=\"https:\/\/login.chinacloudapi.cn\/&lt;your&nbsp;aad&nbsp;tenant&nbsp;id&gt;\/oauth2\/token\"aadbody={'grant_type':'client_credentials','client_id':'your&nbsp;aad&nbsp;client&nbsp;id','client_secret':'your&nbsp;aad&nbsp;client&nbsp;secret','resource':'https:\/\/management.chinacloudapi.cn'}\nrtoken=&nbsp;requests.post(aadurl,&nbsp;data=aadbody)##print(rtoken)objtoken&nbsp;=&nbsp;json.loads(rtoken.text)##print(obj['access_token'])##Part&nbsp;2:&nbsp;Get&nbsp;the&nbsp;Metrics&nbsp;Value&nbsp;by&nbsp;Tokenheaders&nbsp;=&nbsp;{'content-type':&nbsp;\"application\/json\",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Authorization':&nbsp;'Bearer&nbsp;'+objtoken['access_token']\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\nurl=&nbsp;\"https:\/\/management.chinacloudapi.cn\/subscriptions\/&lt;subscriptions&gt;\/resourceGroups\/&lt;resourceGroups&gt;\/providers\/Microsoft.Cache\/Redis\/&lt;your&nbsp;redis&nbsp;name&gt;\/providers\/microsoft.insights\/metrics?api-version=2018-01-01&amp;metricnames=expiredkeys,usedmemory\"r&nbsp;=&nbsp;requests.get(url,&nbsp;headers=headers)print('Status&nbsp;Code:&nbsp;'&nbsp;+&nbsp;str(r))print('Response&nbsp;Content:&nbsp;'&nbsp;+&nbsp;str(r.content))<\/pre>\n<p><strong>\u8fd0\u884c\u6548\u679c\u5982\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138853406627d09c373a7.png\" class=\"aligncenter\"><\/p>\n<h3>Powershell<\/h3>\n<ul>\n<li>\n<p>\u767b\u5f55azure<\/p>\n<\/li>\n<li>\n<p>\u51c6\u5907az monitor metrics list\u547d\u4ee4<\/p>\n<\/li>\n<\/ul>\n<pre>az&nbsp;cloud&nbsp;set&nbsp;--name&nbsp;AzureChinaCloud\n\naz&nbsp;login\n\naz&nbsp;monitor&nbsp;metrics&nbsp;list&nbsp;--resource&nbsp;\/subscriptions\/&lt;your&nbsp;subscriptions&gt;\/resourceGroups\/&lt;resourceGroups&gt;\/providers\/Microsoft.Cache\/Redis\/&lt;your&nbsp;redis&nbsp;name&gt;&nbsp;--metric&nbsp;usedmemory&nbsp;--aggregation&nbsp;Maximum&nbsp;--interval&nbsp;PT1M<\/pre>\n<p><strong>\u6267\u884c\u6548\u679c\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138853406627d09c3f088.png\" class=\"aligncenter\">&nbsp;<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138853406627d09c44b1b.png\" class=\"aligncenter\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528Python\u4ee3\u7801\u83b7\u53d6Az&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-202363","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202363"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202363\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}