下面显示的是英文原文。
with_broker_labeled
with_broker_labeled registers a broker under a label that is its server name in the document, and the broker describes the server.
with_broker_labeled takes a broker that implements DescribeServer; every shipped broker does.
The broker fills the server entry in its protocol's vocabulary: host, protocol, protocol version and bindings.
The in-memory broker describes an in-process server over the memory protocol, with no host.
The label is also the broker's identity in the service; a test addresses the broker by it with broker_named.
The registration's channels list the server, and its operations carry the server's protocol as a tag, so a document with several brokers reads by transport.
A document with one server puts every channel on it.
With several, the channels of an unlabeled registration list none, which AsyncAPI reads as "on every server".
An explicit server under the same label takes precedence over what the broker says.
with_broker_labeled_codec also names the scope's codec.
The example runs with cargo test --example asyncapi_servers --features macros,memory,asyncapi.
Example
- Macros
- Manual
fn app() -> Result<impl App, BindingError> {
let production = with_registry(secured(production()))?;
Ok(RustStream::new(AppInfo::new("orders", "0.1.0"))
// Declared: no broker of this process describes the cluster.
.server("production", production)
// Described by the broker itself, under its label.
.with_broker_labeled("in-process", MemoryBroker::new(), |b| {
b.include(accept);
}))
}
fn app() -> Result<impl App, BindingError> {
let production = with_registry(secured(production()))?;
Ok(RustStream::new(AppInfo::new("orders", "0.1.0"))
// Declared: no broker of this process describes the cluster.
.server("production", production)
// Described by the broker itself, under its label.
.with_broker_labeled("in-process", MemoryBroker::new(), |b| {
b.include(subscriber("orders", Accept).build());
}))
}