选项
首页首页 Skill API开发 azure-servicebus-rust

azure-servicebus-rust

microsoft/skills microsoft/skills

从 Rust 应用程序中使用 Azure Service Bus 队列、主题和订阅发送和接收消息。

...展开全部
8
更新时间 2026-09-13

Azure Service Bus 的 Rust 库

Azure Service Bus 客户端库——企业级消息代理,支持队列和发布-订阅主题。

⚠️ 警告: 此 crate 处于早期开发阶段,严禁用于生产环境。API 可能会在未经通知的情况下发生变化。

在以下场景中使用此技能:

  • 应用需要通过 Rust 向 Azure Service Bus 发送或接收消息
  • 你需要基于队列的消息传递,且存在竞争消费者
  • 你需要基于主题和订阅的发布-订阅消息传递
  • 你需要具有完成语义的可靠消息传递

重要提示: 仅使用由 azure-sdk crates.io 用户发布的官方 azure_messaging_servicebus crate。请勿使用非官方或社区提供的 crate。官方 crate 的名称中使用下划线,且均没有 0.21.0 版本。

安装

cargo add azure_messaging_servicebus azure_identity tokio

如果你的代码直接使用了 azure_core 类型,请将 azure_core 添加到 Cargo.toml 中。如果你仅使用 azure_messaging_servicebus 的重新导出,则直接依赖 azure_core 是可选的。

环境变量

SERVICEBUS_NAMESPACE=<namespace>.servicebus.windows.net # 必需——完全限定的命名空间
</namespace>

核心概念

概念描述
**命名空间**所有消息传递组件的容器
**队列**点对点消息传递,具有竞争消费者
**主题**发布-订阅消息传递——一个发送者,多个订阅者
**订阅**从主题接收消息
**消息**数据和元数据的包,具有完成/放弃语义

身份验证

use azure_identity::DeveloperToolsCredential;
use azure_messaging_servicebus::ServiceBusClient;

#[tokio::main]
async fn main() -> Result> {
    // 本地开发:DeveloperToolsCredential。生产环境:使用 ManagedIdentityCredential。
    let credential = DeveloperToolsCredential::new(None)?;
    let client = ServiceBusClient::builder()
        .open("your_namespace.servicebus.windows.net", credential.clone())
        .await?;
    Ok(())
}

核心工作流

向队列发送消息

use azure_identity::DeveloperToolsCredential;
use azure_messaging_servicebus::{ServiceBusClient, Message};

#[tokio::main]
async fn main() -> Result> {
    let credential = DeveloperToolsCredential::new(None)?;
    let client = ServiceBusClient::builder()
        .open("your_namespace.servicebus.windows.net", credential.clone())
        .await?;
    let sender = client.create_sender("my_queue", None).await?;

    let message = Message::from("Hello, Service Bus!");
    sender.send_message(message, None).await?;
    Ok(())
}

从队列接收消息

use azure_identity::DeveloperToolsCredential;
use azure_messaging_servicebus::ServiceBusClient;

#[tokio::main]
async fn main() -> Result> {
    let credential = DeveloperToolsCredential::new(None)?;
    let client = ServiceBusClient::builder()
        .open("your_namespace.servicebus.windows.net", credential.clone())
        .await?;
    let receiver = client.create_receiver("my_queue", None).await?;

    let messages = receiver.receive_messages(5, None).await?;
    for message in messages {
        println!("Received: {}", message.body_as_string()?);
        receiver.complete_message(&message, None).await?;
    }
    Ok(())
}

向主题发送消息

let sender = client.create_sender("my_topic", None).await?;
let message = Message::from("Hello, Topic subscribers!");
sender.send_message(message, None).await?;

从订阅接收消息

let receiver = client
    .create_receiver_for_subscription("my_topic", "my_subscription", None)
    .await?;

let messages = receiver.receive_messages(5, None).await?;
for message in messages {
    println!("Received: {}", message.body_as_string()?);
    receiver.complete_message(&message, None).await?;
}

消息处理

操作目的
`complete`从队列中移除消息——处理成功
`abandon`释放锁——消息变为可重试状态

在处理成功后始终完成消息,以防止重新投递。

RBAC 角色

对于 Entra ID 身份验证,请分配以下角色之一:

角色访问权限
`Azure Service Bus Data Sender`发送消息
`Azure Service Bus Data Receiver`接收消息
`Azure Service Bus Data Owner`完全访问权限

最佳实践

  1. 使用 cargo add 管理依赖项,切勿直接编辑 Cargo.toml 使用 cargo 命令添加和移除 Rust SDK 依赖项,而不是手动编辑清单文件。
  2. 仅在直接导入 azure_core 类型时添加 azure_core 如果你的代码导入了 azure_core::http::Urlazure_core::http::RequestContentazure_core::error::ErrorKind,请包含 azure_core;否则,直接依赖是可选的。
  3. 本地开发使用 DeveloperToolsCredential生产环境使用 ManagedIdentityCredential——Rust 不提供单一的 DefaultAzureCredential 类型
  4. 切勿硬编码凭据——请使用环境变量或托管身份
  5. 分配 RBAC 角色——确保身份具有适当的 Service Bus 数据角色
  6. 始终完成消息——处理完成后调用 complete_message 以从队列中移除
  7. 使用主题进行扇出——当多个消费者需要相同的消息时,请使用带有订阅的主题
  8. 此 crate 处于预生产阶段——API 可能会发生变化;请在你的依赖工作流中使用 cargo 命令固定依赖版本

参考链接

资源链接
API 参考https://docs.rs/azure\_messaging\_servicebus/latest/azure\_messaging\_servicebus
crates.iohttps://crates.io/crates/azure\_messaging\_servicebus
源代码https://github.com/Azure/azure-sdk-for-rust/tree/main/sdk/servicebus/azure\_messaging\_servicebus
在 GitHub 上查看
---
name: azure-servicebus-rust
description: Send and receive messages using Azure Service Bus queues, topics, and subscriptions from Rust applications.
license: MIT
---

# Azure Service Bus library for Rust

Client library for Azure Service Bus — enterprise message broker with queues and publish-subscribe topics.

> **⚠️ WARNING:** This crate is in early development and **SHOULD NOT** be used in production. APIs may change without notice.

Use this skill when:

- An app needs to send or receive messages via Azure Service Bus from Rust
- You need queue-based messaging with competing consumers
- You need publish-subscribe messaging with topics and subscriptions
- You need reliable message delivery with completion semantics

> **IMPORTANT:** Only use the official `azure_messaging_servicebus` crate published by the [azure-sdk](https://crates.io/users/azure-sdk) crates.io user. Do NOT use unofficial or community crates. Official crates use underscores in names and none have version 0.21.0.

## Installation

```sh
cargo add azure_messaging_servicebus azure_identity tokio
```

> If your code uses `azure_core` types directly, add `azure_core` to `Cargo.toml`. If you only use `azure_messaging_servicebus` re-exports, direct `azure_core` dependency is optional.

## Environment Variables

```bash
SERVICEBUS_NAMESPACE=<namespace>.servicebus.windows.net # Required — fully qualified namespace
```

## Key Concepts

| Concept          | Description                                                     |
| ---------------- | --------------------------------------------------------------- |
| **Namespace**    | Container for all messaging components                          |
| **Queue**        | Point-to-point messaging with competing consumers               |
| **Topic**        | Publish-subscribe messaging — one sender, many subscribers      |
| **Subscription** | Receives messages from a topic                                  |
| **Message**      | Package of data and metadata, with completion/abandon semantics |

## Authentication

```rust
use azure_identity::DeveloperToolsCredential;
use azure_messaging_servicebus::ServiceBusClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Local dev: DeveloperToolsCredential. Production: use ManagedIdentityCredential.
    let credential = DeveloperToolsCredential::new(None)?;
    let client = ServiceBusClient::builder()
        .open("your_namespace.servicebus.windows.net", credential.clone())
        .await?;
    Ok(())
}
```

## Core Workflow

### Send a Message to a Queue

```rust
use azure_identity::DeveloperToolsCredential;
use azure_messaging_servicebus::{ServiceBusClient, Message};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let credential = DeveloperToolsCredential::new(None)?;
    let client = ServiceBusClient::builder()
        .open("your_namespace.servicebus.windows.net", credential.clone())
        .await?;
    let sender = client.create_sender("my_queue", None).await?;

    let message = Message::from("Hello, Service Bus!");
    sender.send_message(message, None).await?;
    Ok(())
}
```

### Receive Messages from a Queue

```rust
use azure_identity::DeveloperToolsCredential;
use azure_messaging_servicebus::ServiceBusClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let credential = DeveloperToolsCredential::new(None)?;
    let client = ServiceBusClient::builder()
        .open("your_namespace.servicebus.windows.net", credential.clone())
        .await?;
    let receiver = client.create_receiver("my_queue", None).await?;

    let messages = receiver.receive_messages(5, None).await?;
    for message in messages {
        println!("Received: {}", message.body_as_string()?);
        receiver.complete_message(&message, None).await?;
    }
    Ok(())
}
```

### Send a Message to a Topic

```rust
let sender = client.create_sender("my_topic", None).await?;
let message = Message::from("Hello, Topic subscribers!");
sender.send_message(message, None).await?;
```

### Receive Messages from a Subscription

```rust
let receiver = client
    .create_receiver_for_subscription("my_topic", "my_subscription", None)
    .await?;

let messages = receiver.receive_messages(5, None).await?;
for message in messages {
    println!("Received: {}", message.body_as_string()?);
    receiver.complete_message(&message, None).await?;
}
```

## Message Settlement

| Action     | Purpose                                            |
| ---------- | -------------------------------------------------- |
| `complete` | Remove message from queue — processing succeeded   |
| `abandon`  | Release lock — message becomes available for retry |

Always complete messages after successful processing to prevent redelivery.

## RBAC Roles

For Entra ID auth, assign one of these roles:

| Role                              | Access           |
| --------------------------------- | ---------------- |
| `Azure Service Bus Data Sender`   | Send messages    |
| `Azure Service Bus Data Receiver` | Receive messages |
| `Azure Service Bus Data Owner`    | Full access      |

## Best Practices

1. **Use `cargo add` to manage dependencies, never edit `Cargo.toml` directly.** Add and remove Rust SDK dependencies with cargo commands instead of manual manifest edits.
2. **Add `azure_core` only when importing `azure_core` types directly.** If your code imports `azure_core::http::Url`, `azure_core::http::RequestContent`, or `azure_core::error::ErrorKind`, include `azure_core`; otherwise a direct dependency is optional.
3. **Use `DeveloperToolsCredential`** for local dev, **`ManagedIdentityCredential`** for production — Rust does not provide a single `DefaultAzureCredential` type
4. **Never hardcode credentials** — use environment variables or managed identity
5. **Assign RBAC roles** — ensure the identity has appropriate Service Bus data roles
6. **Always complete messages** — call `complete_message` after processing to remove from queue
7. **Use topics for fan-out** — when multiple consumers need the same messages, use topics with subscriptions
8. **This crate is pre-production** — APIs may change; pin your dependency version with cargo commands in your dependency workflow

## Reference Links

| Resource      | Link                                                                                            |
| ------------- | ----------------------------------------------------------------------------------------------- |
| API Reference | https://docs.rs/azure_messaging_servicebus/latest/azure_messaging_servicebus                    |
| crates.io     | https://crates.io/crates/azure_messaging_servicebus                                             |
| Source Code   | https://github.com/Azure/azure-sdk-for-rust/tree/main/sdk/servicebus/azure_messaging_servicebus |

所有文件

0 个文件

安装 azure-servicebus-rust

下载并将技能文件解压至你的 .claude/skills/ 目录。

下载ZIP

克隆仓库并复制技能文件到您的项目中。

git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-rust/skills/azure-servicebus-rust # Copy SKILL.md to your .claude/skills/ directory

复制 复制
快速设置: 将技能文件夹复制到 .claude/skills/ 目录。Claude 将自动检测并使用该技能。

相关技能

brightdata-cli
更新时间 2026-06-29
humanize
更新时间 2026-07-07
agentwallet
更新时间 2026-07-07
korean-stock-search
更新时间 2026-07-08
OR