Azure Functions KafkaTrigger本地正常,部署后极少触发问题排查
Azure Functions Kafka Trigger连接Event Hub部署后无法正常触发问题
我用Java开发了一个Azure Function,正从Event Hub Trigger迁移到Kafka Trigger。测试阶段使用启用Kafka协议的Event Hub,后续计划部署到Confluent Kafka实例。
本地用个人账号通过mvn azure-functions:run运行函数时,能正常消费本地生产者发送的消息;但将同一函数部署到Azure并配置托管身份后,几乎无法触发消息消费。函数采用消费计划,host.json中设置了"functionTimeout": "00:04:30"。
代码
函数代码
@FunctionName("KafkaLicensesConsumer") public void consumeKafka( @KafkaTrigger( name = "KafkaLicensesConsumer", brokerList = "BOOTSTRAPSERVERS", topic = "<TOPICS>_NAME", consumerGroup = "<TOPIC>_CONSUMER_GROUP", dataType = "binary", protocol = BrokerProtocol.SASLSSL, authenticationMode = BrokerAuthenticationMode.PLAIN, username = "SASL_USERNAME", password = "SASL_PASSWORD" ) byte[] eventData, final ExecutionContext context ) { //doStuff }
Event Hub命名空间、事件中心及授权规则(Bicep)
resource eventHub_namespace 'Microsoft.EventHub/namespaces@2021-11-01' = { name: eventHub.namespace location: location sku: { name: 'Standard' tier: 'Standard' capacity: 1 } properties: { zoneRedundant: false isAutoInflateEnabled: true maximumThroughputUnits: 1 kafkaEnabled: true } } resource eventHub_namespace_eventHub_licensesTopic 'Microsoft.EventHub/namespaces/eventhubs@2021-11-01' = { name: '${eventHub.namespace}/${eventHub.licensesTopic}' location: location properties: { messageRetentionInDays: 7 partitionCount: 1 status: 'Active' } dependsOn: [ eventHub_namespace ] } resource eventHub_namespace_eventHub_licensesTopic_eventHub_authorizationRule 'Microsoft.EventHub/namespaces/eventhubs/authorizationRules@2021-11-01' = { name: '${eventHub.namespace}/${eventHub.licensesTopic}/${eventHub.authorizationRule}' location: location properties: { rights: [ 'Listen' 'Send' 'Manage' ] } dependsOn: [ eventHub_namespace_eventHub_licensesTopic ] }
Function App定义(Bicep)
resource functionApp 'Microsoft.Web/sites@2021-03-01' = { name: functionAppName location: location kind: 'functionapp,linux' tags: resourceTags identity: { type: 'UserAssigned' userAssignedIdentities: { '${myFunctionAppMsiResourceId}': { } } } properties: { httpsOnly: 'true' serverFarmId: serverfarm.id siteConfig: { linuxFxVersion: 'Java|8' functionAppScaleLimit: 100 appSettings: [ //settings here ] } } dependsOn: [ storageAccount ] }
已确认事项
- 本地和Azure环境的BrokerList、topic及认证配置完全一致。
- 本地和部署后的函数使用不同的消费者组,避免互相干扰。
- 将Event Hub的分区数设为1后,部署的函数收到了4条消息(第3条延迟10分钟),之后又恢复为无触发状态。
- 函数使用的身份对Event Hub拥有Send、Receive和Owner权限。
- 改用Azure容器实例中部署的常规Kafka代理时,部署后的函数能正常接收消息,但因现有测试环境限制,此方案不可行。
- 部署后,旧的Event Hub Trigger能被同一Hub中的每条消息触发,而Kafka Trigger几乎无响应。
- 项目函数部署完成后,已通过程序重启并同步。
补充说明
Event Hub有时显示的消息数多于入站消息数,推测有多个消费者在监听,但函数并未触发。
内容的提问来源于stack exchange,提问作者Nikolaj Nielsen
相关产品推荐
相关产品推荐

