Azure APIM策略,用于在超时情况下将主后端URL故障切换到辅助后端URL

Azure APIM策略,用于在超时情况下将主后端URL故障切换到辅助后端URL,azure,azure-api-management,policies,Azure,Azure Api Management,Policies,我的要求相当简单。我有一个APIM端点,它调用具有预定义超时的后端HTTP服务。如果请求超时或返回的错误不是“错误请求”(状态代码>400),则需要使用故障转移HTTP服务重试调用,直到成功或最多5次。我正试图用APIM政策来实现这一点。当返回的状态代码高于400时,重试和故障转移可以工作,但如果请求只是超时,则不会发生这种情况。如果请求只是超时,控件甚至不会进入块。我相当确定我设置的重试条件不能捕获超时条件。有人能帮我弄清楚用于记录超时的APIM策略吗 以下是APIM政策 <polici

我的要求相当简单。我有一个APIM端点,它调用具有预定义超时的后端HTTP服务。如果请求超时或返回的错误不是“错误请求”(
状态代码>400)
,则需要使用故障转移HTTP服务重试调用,直到成功或最多5次。我正试图用APIM政策来实现这一点。当返回的状态代码高于400时,重试和故障转移可以工作,但如果请求只是超时,则不会发生这种情况。如果请求只是超时,控件甚至不会进入
块。我相当确定我设置的重试条件不能捕获超时条件。有人能帮我弄清楚用于记录超时的APIM策略吗

以下是APIM政策

<policies>
    <inbound>
        <base />
        <set-variable name="isFlag" value="true" />
    </inbound>
    <backend>
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault<string>("isFlag") == "true")">
                <set-backend-service base-url="<primary url>" />
                <forward-request timeout="5" />
                <retry condition="@(context.Response == null || context.Response.StatusCode > 400)" count="5" interval="1" first-fast-retry="true">
                    <set-backend-service base-url="<secondary url>" />                        
                </retry>
            </when>
        </choose>
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />        
    </on-error>
</policies>

以下是APIM跟踪的摘录

Inbound
(3.014 ms)
api-inspector (0.403 ms)
{
    "request": {
        "method": "GET",
        "url": "https://gpcshare-apim-gpc-dev-aes.azure-api.net/statcode/405?sleep=7000",
        "headers": [
            {
                "name": "Ocp-Apim-Subscription-Key",
                "value": "a02f7f4e18d54d84ba55d4f122548072"
            },
            {
                "name": "Sec-Fetch-Site",
                "value": "cross-site"
            },
            {
                "name": "Sec-Fetch-Mode",
                "value": "cors"
            },
            {
                "name": "X-Forwarded-For",
                "value": "103.111.183.78"
            },
            {
                "name": "Cache-Control",
                "value": "no-cache, no-store"
            },
            {
                "name": "Via",
                "value": "ICAP/1.0 bmg70302.ibosscloud.com (IBOSS/0.4.4 iboss ICAP service )"
            },
            {
                "name": "Content-Type",
                "value": "text/plain;charset=UTF-8"
            },
            {
                "name": "Accept",
                "value": "*/*"
            },
            {
                "name": "Accept-Encoding",
                "value": "gzip,deflate,br"
            },
            {
                "name": "Accept-Language",
                "value": "en-US,en;q=0.9"
            },
            {
                "name": "Host",
                "value": "gpcshare-apim-gpc-dev-aes.azure-api.net"
            },
            {
                "name": "Referer",
                "value": "https://apimanagement.hosting.portal.azure.net/apimanagement/Content/1.42.0.1/apimap//apimap-apis/index.html?clientOptimizations=undefined&l=en.en-us&trustedAuthority=https%3A%2F%2Fportal.azure.com&shellVersion=undefined"
            }
        ]
    }
}
api-inspector (0.004 ms)
{
    "configuration": {
        "api": {
            "from": "/statcode",
            "to": null,
            "version": null,
            "revision": "1"
        },
        "operation": {
            "method": "GET",
            "uriTemplate": "/{code}"
        },
        "user": "-",
        "product": "-"
    }
}
cors (2.601 ms)
"Origin header was missing or empty and the request was classified as not cross-domain. CORS policy was not applied."
cors (0.002 ms)
"Origin header was missing or empty and the request was classified as not cross-domain. CORS policy was not applied."
set-variable (0.003 ms)
{
    "message": "Context variable was successfully set.",
    "name": "isFlag",
    "value": "true"
}
Backend
(4,991.587 ms)↑ Back to top
choose (0.017 ms)
{
    "message": "Expression was successfully evaluated.",
    "expression": "context.Variables.GetValueOrDefault<string>(\"isFlag\") == \"true\"",
    "value": true
}
set-backend-service (0.007 ms)
{
    "message": "Backend service URL was changed.",
    "oldBackendServiceUrl": "",
    "newBackendServiceUrl": "https://httpstat.us/",
    "request": {
        "url": "https://httpstat.us/405?sleep=7000"
    }
}
forward-request (0.151 ms)
{
    "message": "Request is being forwarded to the backend service. Timeout set to 5 seconds",
    "request": {
        "method": "GET",
        "url": "https://httpstat.us/405?sleep=7000",
        "headers": [
            {
                "name": "Host",
                "value": "httpstat.us"
            },
            {
                "name": "Request-Id",
                "value": "|f595c50c66d7453c97a7dc14c9c1af9e.2b06f656cb6c4704_b907aebb."
            },
            {
                "name": "Ocp-Apim-Subscription-Key",
                "value": "a02f7f4e18d54d84ba55d4f122548072"
            },
            {
                "name": "Sec-Fetch-Site",
                "value": "cross-site"
            },
            {
                "name": "Sec-Fetch-Mode",
                "value": "cors"
            },
            {
                "name": "X-Forwarded-For",
                "value": "103.111.183.78,13.91.254.72"
            },
            {
                "name": "Cache-Control",
                "value": "no-cache, no-store"
            },
            {
                "name": "Via",
                "value": "ICAP/1.0 bmg70302.ibosscloud.com (IBOSS/0.4.4 iboss ICAP service )"
            },
            {
                "name": "Content-Type",
                "value": "text/plain;charset=UTF-8"
            },
            {
                "name": "Accept",
                "value": "*/*"
            },
            {
                "name": "Accept-Encoding",
                "value": "gzip,deflate,br"
            },
            {
                "name": "Accept-Language",
                "value": "en-US,en;q=0.9"
            },
            {
                "name": "Referer",
                "value": "https://apimanagement.hosting.portal.azure.net/apimanagement/Content/1.42.0.1/apimap//apimap-apis/index.html?clientOptimizations=undefined&l=en.en-us&trustedAuthority=https%3A%2F%2Fportal.azure.com&shellVersion=undefined"
            }
        ]
    }
}
forward-request (4,991.413 ms)
{
    "messages": [
        "Error occured while calling backend service.",
        "Request to the backend service timed out"
    ]
}
Outbound
(0.183 ms)↑ Back to top
transfer-response (0.067 ms)
{
    "message": "Response headers have been sent to the caller."
}
transfer-response (0.116 ms)
{
    "message": "Response body streaming to the caller is complete."
}
入站
(3.014毫秒)
api检查员(0.403毫秒)
{
“请求”:{
“方法”:“获取”,
“url”:”https://gpcshare-apim-gpc-dev-aes.azure-api.net/statcode/405?sleep=7000",
“标题”:[
{
“名称”:“Ocp Apim订阅密钥”,
“值”:“a02f7f4e18d54d84ba55d4f122548072”
},
{
“名称”:“Sec获取站点”,
“值”:“跨站点”
},
{
“名称”:“秒提取模式”,
“值”:“cors”
},
{
“名称”:“X-Forwarded-For”,
“值”:“103.111.183.78”
},
{
“名称”:“缓存控制”,
“值”:“无缓存,无存储”
},
{
“名称”:“通过”,
“值”:“ICAP/1.0 bmg70302.ibosscloud.com(IBOSS/0.4.4 IBOSS ICAP服务)”
},
{
“名称”:“内容类型”,
“值”:“文本/纯文本;字符集=UTF-8”
},
{
“名称”:“接受”,
“值”:“*/*”
},
{
“名称”:“接受编码”,
“值”:“gzip,deflate,br”
},
{
“名称”:“接受语言”,
“值”:“en-US,en;q=0.9”
},
{
“名称”:“主机”,
“值”:“gpcshare-apim-gpc-dev-aes.azure-api.net”
},
{
“姓名”:“推荐人”,
“值”:https://apimanagement.hosting.portal.azure.net/apimanagement/Content/1.42.0.1/apimap//apimap-apis/index.html?clientOptimizations=undefined&l=en.en-us&trustedAuthority=https%3A%2F%2Fportal.azure.com&shellVersion=undefined“
}
]
}
}
api检查员(0.004毫秒)
{
“配置”:{
“api”:{
“发件人”:“/statcode”,
“to”:空,
“版本”:空,
“修订”:“1”
},
“操作”:{
“方法”:“获取”,
“uriTemplate”:“/{code}”
},
“用户”:“-”,
“产品”:“-”
}
}
cors(2.601毫秒)
“原始标头丢失或为空,请求被分类为非跨域。未应用CORS策略。”
cors(0.002毫秒)
“原始标头丢失或为空,请求被分类为非跨域。未应用CORS策略。”
设置变量(0.003毫秒)
{
“消息”:“已成功设置上下文变量。”,
“名称”:“isFlag”,
“值”:“真”
}
后端
(4991.587毫秒)↑ 回到顶端
选择(0.017毫秒)
{
“消息”:“表达式已成功计算。”,
“表达式”:“context.Variables.GetValueOrDefault(\“isFlag\”)==“true\”,
“价值”:真实
}
设置后端服务(0.007毫秒)
{
“消息”:“后端服务URL已更改。”,
“oldBackendServiceUrl”:“”,
“newBackendServiceUrl”:https://httpstat.us/",
“请求”:{
“url”:”https://httpstat.us/405?sleep=7000"
}
}
转发请求(0.151毫秒)
{
“消息”:“请求正在转发到后端服务。超时设置为5秒”,
“请求”:{
“方法”:“获取”,
“url”:”https://httpstat.us/405?sleep=7000",
“标题”:[
{
“名称”:“主机”,
“值”:“httpstat.us”
},
{
“名称”:“请求Id”,
“值”:“f595c50c66d7453c97a7dc14c9c1af9e.2b06f656cb6c4704_b907aebb。”
},
{
“名称”:“Ocp Apim订阅密钥”,
“值”:“a02f7f4e18d54d84ba55d4f122548072”
},
{
“名称”:“Sec获取站点”,
“值”:“跨站点”
},
{
“名称”:“秒提取模式”,
“值”:“cors”
},
{
“名称”:“X-Forwarded-For”,
“值”:“103.111.183.78,13.91.254.72”
},
{
“名称”:“缓存控制”,
“值”:“无缓存,无存储”
},
{
“名称”:“通过”,
“值”:“ICAP/1.0 bmg70302.ibosscloud.com(IBOSS/0.4.4 IBOSS ICAP服务)”
},
{
“名称”:“内容类型”,
“值”:“文本/纯文本;字符集=UTF-8”
},
{
“名称”:“接受”,
“值”:“*/*”
},
{
“名称”:“接受编码”,
“值”:“gzip,deflate,br”
},
{
“名称”:“接受语言”,
“值”:“en-US,en;q=0.9”
},
{
“姓名”:“推荐人”,