Available on crate feature discover only.
Expand description

Service discovery

This module provides the Change enum, which indicates the arrival or departure of a service from a collection of similar services. Most implementations should use the Discover trait in their bounds to indicate that they can handle services coming and going. Discover itself is primarily a convenience wrapper around TryStream<Ok = Change>.

Every discovered service is assigned an identifier that is distinct among the currently active services. If that service later goes away, a Change::Remove is yielded with that service’s identifier. From that point forward, the identifier may be re-used.


use futures_util::{future::poll_fn, pin_mut};
use tower::discover::{Change, Discover};
async fn services_monitor<D: Discover>(services: D) {
    while let Some(Ok(change)) = poll_fn(|cx| services.as_mut().poll_discover(cx)).await {
        match change {
            Change::Insert(key, svc) => {
                // a new service with identifier `key` was discovered
            Change::Remove(key) => {
                // the service with identifier `key` has gone away


Static service discovery based on a predetermined list of services.


A change in the service set.


A dynamically changing set of related services.