适用于 Azure Functions 的 RabbitMQ 输出绑定概述RabbitMQ output binding 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 output binding to send messages to a RabbitMQ queue.

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

示例Example

以下示例演示一个 C# 函数,该函数在由 TimerTrigger 每 5 分钟触发一次时,使用方法返回值作为输出,发送 RabbitMQ 消息:The following example shows a C# function that sends a RabbitMQ message when triggered by a TimerTrigger every 5 minutes using the method return value as the output:

[FunctionName("RabbitMQOutput")]
[return: RabbitMQ(QueueName = "outputQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")]
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    return $"{DateTime.Now}";
}

以下示例演示如何使用 IAsyncCollector 接口发送消息。The following example shows how to use the IAsyncCollector interface to send messages.

[FunctionName("RabbitMQOutput")]
public static async Task Run(
[RabbitMQTrigger("sourceQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")] string rabbitMQEvent,
[RabbitMQ(QueueName = "destinationQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")]IAsyncCollector<string> outputEvents,
ILogger log)
{
     // send the message
    await outputEvents.AddAsync(JsonConvert.SerializeObject(rabbitMQEvent));
}

以下示例演示如何将消息作为 POCO 发送。The following example shows how to send the messages as POCOs.

namespace Company.Function
{
    public class TestClass
    {
        public string x { get; set; }
    }
    public static class RabbitMQOutput{
        [FunctionName("RabbitMQOutput")]
        public static async Task Run(
        [RabbitMQTrigger("sourceQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")] TestClass rabbitMQEvent,
        [RabbitMQ(QueueName = "destinationQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")]IAsyncCollector<TestClass> outputPocObj,
        ILogger log)
        {
            // send the message
            await outputPocObj.AddAsync(rabbitMQEvent);
        }
    }
}

特性和注释Attributes and annotations

C# 类库中,使用 RabbitMQAttributeIn C# class libraries, use the RabbitMQAttribute.

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

[FunctionName("RabbitMQOutput")]
public static async Task Run(
[RabbitMQTrigger("SourceQueue", ConnectionStringSetting = "TriggerConnectionString")] string rabbitMQEvent,
[RabbitMQ("DestinationQueue", ConnectionStringSetting = "OutputConnectionString")]IAsyncCollector<string> outputEvents,
ILogger log)
{
    ...
}

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

配置Configuration

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

function.json 属性function.json property Attribute 属性Attribute property 说明Description
typetype 不适用n/a 必须设置为“RabbitMQ”。Must be set to "RabbitMQ".
directiondirection 不适用n/a 必须设置为“out”。Must be set to "out".
namename 不适用n/a 表示函数代码中的队列的变量的名称。The name of the variable that represents the queue in function code.
queueNamequeueName QueueNameQueueName 要向其发送消息的队列的名称。Name of the queue to send messages to.
hostNamehostName HostNameHostName (如果使用 ConnectStringSetting,则忽略)(ignored if using ConnectStringSetting)
队列的主机名(例如:10.26.45.210)Hostname of the queue (Ex: 10.26.45.210)
userNameuserName UserNameUserName (如果使用 ConnectionStringSetting,则忽略)(ignored if using ConnectionStringSetting)
应用设置的名称,该设置包含用于访问队列的用户名。Name of the app setting that contains the username to access the queue. 例如:Ex. UserNameSetting: "< UserNameFromSettings >"UserNameSetting: "< UserNameFromSettings >"
passwordpassword 密码Password (如果使用 ConnectionStringSetting,则忽略)(ignored if using ConnectionStringSetting)
应用设置的名称,该设置包含用于访问队列的密码。Name of the app setting that contains the password to access the queue. 例如:Ex. UserNameSetting: "< UserNameFromSettings >"UserNameSetting: "< UserNameFromSettings >"
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

对输出绑定使用以下参数类型:Use the following parameter types for the output binding:

  • byte[] - 如果函数退出时参数值为 null,Functions 不创建消息。byte[] - If the parameter value is null when the function exits, Functions does not create a message.
  • string - 如果函数退出时参数值为 null,Functions 不创建消息。string - If the parameter value is null when the function exits, Functions does not create a message.
  • POCO - 如果参数值未格式化为 C# 对象,则会收到错误。POCO - If the parameter value isn't formatted as a C# object, an error will be received. 有关完整示例,请参阅 C# 示例For a complete example, see C# example.

使用 C# 函数时:When working with C# functions:

  • 异步函数需要返回值或 IAsyncCollector 而不是 out 参数。Async functions need a return value or IAsyncCollector instead of an out parameter.

后续步骤Next steps