Azure Function推送数据至Azure Event Hub失败问题排查
问题描述
我用Azure Function从Azure Service Bus消费数据,再推送到Azure Event Hub,但调用时出现404错误,提示找不到实体sb://****.servicebus.windows.net/Transactions-EVH-INS-DEV-EUW。我怀疑是配置项%Transactions:EVH:Transactions:Hub%的值不对,而且疑惑为什么错误里会提到Service Bus,已经查过官方文档。
代码实现
[FunctionName("IntegrationFunction")] [return: EventHub("%Transactions:EVH:Transactions:Hub%", Connection = "Transactions:SB:Transactions")] public Transaction Run( [ServiceBusTrigger( "%Transactions:SB:Transactions:ReceiveTopic%", "%Transactions:SB:Transactions:AnalyticsTopicSubscription%", Connection = "Transactions:SB:Transactions")] string mySbMsg, ILogger log) { if (string.IsNullOrWhiteSpace(mySbMsg)) { throw new ArgumentNullException(nameof(mySbMsg)); } log.LogInformation($"Service bus topic trigger function processing message: {mySbMsg}"); var retailTransaction = JsonConvert.DeserializeObject<RetailTransaction>( mySbMsg, JsonSerialisationUtils.SerialiserSettings); if (retailTransaction == null) { throw new JsonException("Deserialized transaction was null"); } try { var transaction = retailTransaction.ToDto(); log.LogInformation($"Transaction {transaction.TransactionNumber} processed."); return transaction; } catch (Exception e) { log.LogError(e, "Error mapping transaction."); } return null; }
错误信息
原始英文
Put token failed. status-code: 404, status-description: The messaging entity 'sb://.servicebus.windows.net/Transactions-EVH-INS-DEV-EUW' could not be found. To know more visit https://aka.ms/sbResourceMgrExceptions. TrackingId:**************, SystemTracker::Transactions-EVH-INS-DEV-EUW, Timestamp:2024-03-12T07:49:07.
中文翻译
令牌放置失败。状态码:404,状态描述:找不到消息实体'sb://.servicebus.windows.net/Transactions-EVH-INS-DEV-EUW'。跟踪ID:**************,系统跟踪器::Transactions-EVH-INS-DEV-EUW,时间戳:2024-03-12T07:49:07.
问题原因及解决方案
核心错误:Event Hub绑定用了Service Bus的连接字符串
代码里Event Hub输出绑定的Connection参数指定的是Transactions:SB:Transactions——这是Azure Service Bus的连接字符串。Event Hub和Service Bus是两个独立服务,必须使用Event Hub专属的连接字符串。这就是错误里提到Service Bus命名空间的原因:代码正拿着Service Bus的凭据去访问Event Hub实体,自然找不到目标资源。修复步骤
- 确认Event Hub名称配置:检查
%Transactions:EVH:Transactions:Hub%的值是纯Event Hub名称,不要带命名空间前缀(比如只填Transactions-EVH-INS-DEV-EUW,而不是完整的sb://xxx.servicebus.windows.net/xxx)。 - 添加Event Hub连接字符串配置:在配置文件里新增一个项(比如
Transactions:EVH:Connection),值从Azure Portal的Event Hub命名空间获取,要选择具备发送权限的共享访问政策。 - 修正绑定的Connection参数:修改Event Hub输出绑定的
Connection为新配置项:[return: EventHub("%Transactions:EVH:Transactions:Hub%", Connection = "Transactions:EVH:Connection")]
- 确认Event Hub名称配置:检查
额外验证点
- 确认目标Event Hub实体确实存在于对应的命名空间中,名称大小写完全匹配(Azure资源名称大小写敏感)。
- 保留Service Bus的连接字符串配置
Transactions:SB:Transactions不变,确保它能正常用于Service Bus触发器的消息消费。
内容的提问来源于stack exchange,提问作者Bhav

