選項
首頁首頁 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 將自動檢測並使用該技能。
儲存庫 microsoft/skills

相關技能

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