Azure Functions 的 RabbitMQ 触发器概述RabbitMQ trigger for Azure Functions overview

备注

仅高级和专用计划完全支持 RabbitMQ 绑定。The RabbitMQ bindings are only fully supported on Premium and Dedicated plans. 不支持消耗。Consumption is not supported.

使用 RabbitMQ 触发器响应来自 RabbitMQ 队列的消息。Use the RabbitMQ trigger to respond to messages from a RabbitMQ queue.

有关设置和配置详细信息,请参阅概述For information on setup and configuration details, see the overview.

示例Example

以下示例显示了一个 C# 函数,该函数将 RabbitMQ 消息作为 RabbitMQ 事件进行读取和记录:The following example shows a C# function that reads and logs the RabbitMQ message as a RabbitMQ Event:

[FunctionName("RabbitMQTriggerCSharp")]
public static void RabbitMQTrigger_BasicDeliverEventArgs(
    [RabbitMQTrigger("queue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")] BasicDeliverEventArgs args,
    ILogger logger
    )
{
    logger.LogInformation($"C# RabbitMQ queue trigger function processed message: {Encoding.UTF8.GetString(args.Body)}");
}

以下示例演示如何将消息作为 POCO 读取。The following example shows how to read the message as a POCO.

namespace Company.Function
{
    public class TestClass
    {
        public string x { get; set; }
    }

    public class RabbitMQTriggerCSharp{
        [FunctionName("RabbitMQTriggerCSharp")]
        public static void RabbitMQTrigger_BasicDeliverEventArgs(
            [RabbitMQTrigger("queue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")] TestClass pocObj,
            ILogger logger
            )
        {
            logger.LogInformation($"C# RabbitMQ queue trigger function processed message: {pocObj}");
        }
    }
}

与 Json 对象一样,如果未将消息格式正确设置为 C# 对象,则会发生错误。Like with Json objects, an error will occur if the message isn't properly formatted as a C# object. 如果格式设置正确,则将其绑定到变量 pocObj,该变量可用于所需的任何用途。If it is, it is then bound to the variable pocObj, which can be used for what whatever it is needed for.

特性和注释Attributes and annotations

C# 类库中,使用 RabbitMQTrigger 特性。In C# class libraries, use the RabbitMQTrigger attribute.

下面是某个方法签名中的 RabbitMQTrigger 特性:Here's a RabbitMQTrigger attribute in a method signature:

[FunctionName("RabbitMQTest")]
public static void RabbitMQTest([RabbitMQTrigger("queue")] string message, ILogger log)
{
    ...
}

有关完整示例,请参阅 C# 示例For a complete example, see C# example.

配置Configuration

下表解释了在 function.json 文件和 RabbitMQTrigger 特性中设置的绑定配置属性。The following table explains the binding configuration properties that you set in the function.json file and the RabbitMQTrigger attribute.

function.json 属性function.json property Attribute 属性Attribute property 说明Description
typetype 不适用n/a 必须设置为“RabbitMQTrigger”。Must be set to "RabbitMQTrigger".
directiondirection 不适用n/a 必须设置为“in”。Must be set to "in".
namename 不适用n/a 表示函数代码中的队列的变量的名称。The name of the variable that represents the queue in function code.
queueNamequeueName QueueNameQueueName 从中接收消息的队列的名称。Name of the queue to receive messages from.
hostNamehostName HostNameHostName (如果使用 ConnectStringSetting,则忽略)(ignored if using ConnectStringSetting)
队列的主机名(例如:10.26.45.210)Hostname of the queue (Ex: 10.26.45.210)
userNameSettinguserNameSetting UserNameSettingUserNameSetting (如果使用 ConnectionStringSetting,则忽略)(ignored if using ConnectionStringSetting)
应用设置的名称,该设置包含用于访问队列的用户名。Name of the app setting that contains the username to access the queue. 例如:Ex. UserNameSetting: "%< UserNameFromSettings >%"UserNameSetting: "%< UserNameFromSettings >%"
passwordSettingpasswordSetting PasswordSettingPasswordSetting (如果使用 ConnectionStringSetting,则忽略)(ignored if using ConnectionStringSetting)
应用设置的名称,该设置包含用于访问队列的密码。Name of the app setting that contains the password to access the queue. 例如:Ex. PasswordSetting: "%< PasswordFromSettings >%"PasswordSetting: "%< PasswordFromSettings >%"
connectionStringSettingconnectionStringSetting ConnectionStringSettingConnectionStringSetting 包含 RabbitMQ 消息队列连接字符串的应用设置的名称。The name of the app setting that contains the RabbitMQ message queue connection string. 请注意,如果直接指定连接字符串,而不是通过 local.settings.json 中的应用设置进行指定,则触发器将不起作用。Please note that if you specify the connection string directly and not through an app setting in local.settings.json, the trigger will not work. (例如:在 function.json 中:connectionStringSetting: "rabbitMQConnection"(Ex: In function.json: connectionStringSetting: "rabbitMQConnection"
在 local.settings.json 中:"rabbitMQConnection" : "< ActualConnectionstring >")In local.settings.json: "rabbitMQConnection" : "< ActualConnectionstring >")
portport 端口Port (如果使用 ConnectionStringSetting,则忽略)获取或设置所使用的端口。(ignored if using ConnectionStringSetting) Gets or sets the Port used. 默认值为 0,该值指向 rabbitmq 客户端的默认端口设置:5672。Defaults to 0 which points to rabbitmq client's default port setting: 5672.

在本地进行开发时,应用设置将取 local.settings.json 文件的值。When you're developing locally, app settings go into the local.settings.json file.

使用情况Usage

默认消息类型为 RabbitMQ 事件,RabbitMQ 事件的 Body 属性可以读取为下面列出的类型:The default message type is RabbitMQ Event, and the Body property of the RabbitMQ Event can be read as the types listed below:

  • An object serializable as JSON - 消息以有效的 JSON 字符串形式传递。An object serializable as JSON - The message is delivered as a valid JSON string.
  • string
  • byte[]
  • POCO - 消息的格式设置为 C# 对象。POCO - The message is formatted as a C# object. 有关完整示例,请参阅 C# 示例For a complete example, see C# example.

死信队列Dead letter queues

死信队列和交换不能通过 RabbitMQ 触发器进行控制或配置。Dead letter queues and exchanges can't be controlled or configured from the RabbitMQ trigger. 为了使用死信队列,请在 RabbitMQ 中预先配置触发器使用的队列。In order to use dead letter queues, pre-configure the queue used by the trigger in RabbitMQ. 请参阅 RabbitMQ 文档Please refer to the RabbitMQ documentation.

host.json 设置host.json settings

本部分介绍版本 2.x 及更高版本中可用于此绑定的全局配置设置。This section describes the global configuration settings available for this binding in versions 2.x and higher. 下面的示例 host.json 文件仅包含此绑定的设置。The example host.json file below contains only the settings for this binding. 有关全局配置设置的详细信息,请参阅 Azure Functions 版本的 host.json 参考For more information about global configuration settings, see host.json reference for Azure Functions version.

{
    "version": "2.0",
    "extensions": {
        "rabbitMQ": {
            "prefetchCount": 100,
            "queueName": "queue",
            "connectionString": "amqp://user:password@url:port",
            "port": 10
        }
    }
}
属性Property 默认Default 说明Description
prefetchCountprefetchCount 3030 获取或设置消息接收方可以同时请求并缓存的消息数。Gets or sets the number of messages that the message receiver can simultaneously request and is cached.
queueNamequeueName 不适用n/a 从中接收消息的队列的名称。Name of the queue to receive messages from.
connectionStringconnectionString 不适用n/a RabbitMQ 消息队列连接字符串。The RabbitMQ message queue connection string. 请注意,连接字符串在此处直接指定,而不是通过应用设置指定。Please note that the connection string is directly specified here and not through an app setting.
portport 00 (如果使用 connectionString,则忽略)获取或设置所使用的端口。(ignored if using connectionString) Gets or sets the Port used. 默认值为 0,该值指向 rabbitmq 客户端的默认端口设置:5672。Defaults to 0 which points to rabbitmq client's default port setting: 5672.

本地测试Local testing

备注

connectionString 优先于“hostName”、“userName”和“password”。The connectionString takes precedence over "hostName", "userName", and "password". 如果这几项全部设置,则 connectionString 将替代另两项。If these are all set, the connectionString will override the other two.

如果在没有连接字符串的情况下进行本地测试,则应在 host.json 的“rabbitMQ”部分中设置“hostName”设置以及“userName”和“password”(如果适用):If you are testing locally without a connection string, you should set the "hostName" setting and "userName" and "password" if applicable in the "rabbitMQ" section of host.json:

{
    "version": "2.0",
    "extensions": {
        "rabbitMQ": {
            ...
            "hostName": "localhost",
            "username": "userNameSetting",
            "password": "passwordSetting"
        }
    }
}
属性Property 默认Default 说明Description
hostNamehostName 不适用n/a (如果使用 connectionString,则忽略)(ignored if using connectionString)
队列的主机名(例如:10.26.45.210)Hostname of the queue (Ex: 10.26.45.210)
userNameuserName 不适用n/a (如果使用 connectionString,则忽略)(ignored if using connectionString)
用于访问队列的名称Name to access the queue
passwordpassword 不适用n/a (如果使用 connectionString,则忽略)(ignored if using connectionString)
用于访问队列的密码Password to access the queue

启用运行时缩放Enable Runtime Scaling

为了使 RabbitMQ 触发器横向扩展到多个实例,必须启用“运行时缩放监视”设置。In order for the RabbitMQ trigger to scale out to multiple instances, the Runtime Scale Monitoring setting must be enabled.

在门户中,可以在函数应用的“配置” > “函数运行时设置”下找到此设置 。In the portal, this setting can be found under Configuration > Function runtime settings for your function app.

VNETToggle

在 CLI 中,可以使用以下命令启用“运行时缩放监视”:In the CLI, you can enable Runtime Scale Monitoring by using the following command:

az resource update -g <resource_group> -n <function_app_name>/config/web --set properties.functionsRuntimeScaleMonitoringEnabled=1 --resource-type Microsoft.Web/sites

监视 RabbitMQ 终结点Monitoring RabbitMQ endpoint

若要监视特定 RabbitMQ 终结点的队列和交换,请执行以下操作:To monitor your queues and exchanges for a certain RabbitMQ endpoint:

后续步骤Next steps