【问题标题】:Azure Service Bus behind APIMAPIM 背后的 Azure 服务总线
【发布时间】:2021-08-11 20:24:21
【问题描述】:

我正在尝试通过 APIM 将消息发送到服务总线队列我已经阅读了几篇类似下面的文章,但没有运气 https://connectedcircuits.blog/2018/09/25/exposing-azure-service-bus-through-apim-generating-a-sas-token-and-setting-the-session-id/ https://www.serverlessnotes.com/docs/load-article/docs/en/expose-service-bus-queue-through-api-management

现在的问题是,当我尝试发送消息时,而不是创建的典型 201,我只收到 200 OK 并且没有消息到达队列。请注意,它直接针对有问题的服务总线工作得很好。

以下是我目前使用的政策

<policies>
<inbound>
<set-header name="Authorization" exists-action="override">
<value>@{
// Load variables
string resourceUri = "xxxxx.servicebus.windows.net/apimqueue/messages";
string sasKeyName = "";
string sasKey = "";
// Set the token lifespan
System.TimeSpan sinceEpoch = System.DateTime.UtcNow.Subtract(new System.DateTime(1970, 1, 1));
var expiry = System.Convert.ToString((int)sinceEpoch.TotalSeconds + 60); //1 minute
string stringToSign = System.Uri.EscapeDataString(resourceUri) + "\n" + expiry;
System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(sasKey));
var signature = System.Convert.ToBase64String(hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(stringToSign)));
// Format the sas token
var sasToken = String.Format("SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
System.Uri.EscapeDataString(resourceUri), System.Uri.EscapeDataString(signature), expiry, sasKeyName);
return sasToken;
}</value>
</set-header>
</inbound>
<backend>
<!-- base: Begin Product scope -->
<!-- base: Begin Global scope -->
<forward-request />
<!-- base: End Global scope -->
<!-- base: End Product scope -->
</backend>
<outbound />
<on-error />
</policies>

我在对https://apimtestbus.azure-api.net/azurebustest21243.servicebus.windows.net/apimqueue/messages987654323@进行 POST 调用时得到响应

HTTP/1.1 200 正常

content-length: 0

日期:2021 年 5 月 23 日星期日 06:02:25 GMT ocp-apim-apiid:服务总线 ocp-apim-operationid:发送消息 ocp-apim-subscriptionid: 主 ocp-apim-trace-位置:https://apimstdebros05i5mtudswoa.blob.core.windows.net/apiinspectorcontainer/cHcJa9krxa3PhOTGYwgFgw2-37?sv=2019-07-07&sr=b&sig=uh6Y%2BVekeFU%2BZ5G1t8dXt6gVp6sQku5Mp25OTagJUts%3D&se=2021-05-24T06%3A02%3A26Z&sp=r&traceId=8097b06294bd49a0bc95cf1979ac7448 变化:原产地

如有任何帮助,我们将不胜感激

【问题讨论】:

  • 您是否在 servicebus 仪表板上看到任何错误?
  • 没有错误,实际上请求甚至没有到达服务总线,因为我在测试时没有请求/消息仪表板,所以不确定为什么 APIM 会回复 200 OK如果事情不顺利
  • 我缺少 rewrite-uri 模板set-backend-service 的条目。如果这不是完整的政策,我猜该请求已被阅读。因此 context.Request.Body.As(preserveContent: true); 会有所帮助
  • 谢谢@MarkusMeyer 你有我可以尝试的示例政策吗?

标签: azure azureservicebus azure-api-management azure-servicebus-queues


【解决方案1】:

问题是我在 API 级别确认了一项政策,但操作级别的另一项政策正在覆盖它。 删除操作级别策略后它起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多