Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async nats trying cluster connect #801

Closed
andreyddk opened this issue Jan 13, 2023 · 4 comments
Closed

Async nats trying cluster connect #801

andreyddk opened this issue Jan 13, 2023 · 4 comments

Comments

@andreyddk
Copy link

Hi! I trying connect to cluster and got the message
NATS server URL is invalid: invalid port number

env:
NATS_CLUSTER=nats://localhost:4222,nats://localhost:14222,nats://localhost:24222

let config = config_rs::Config::new();
let nats = match async_nats::connect(config.nats.get_cluster() // String).await {
    Ok(r) => r,
    Err(e) => panic!("{}", e),
  };

Why its happening?

@Jarema
Copy link
Member

Jarema commented Jan 13, 2023

Hey!

ToServerAddrs trait is pretty type-safe and does not try to split &str if that's whats passed.

You can either iterate over some collection and parse addresses, or pass slice directly.

    async fn addrs() -> Result<(), async_nats::Error> {
        let servers = vec!["demo.nats.io", "other.nats.io"];
        let client = async_nats::connect(
            servers
                .iter()
                .map(|url| url.parse())
                .collect::<Result<Vec<ServerAddr>, _>>()?
                .as_slice(),
        )
        .await?;
        

or pass list directly:

        let client = async_nats::connect(
            [
                "demo.nats.io".parse::<ServerAddr>()?,
                "other.nats.io".parse::<ServerAddr>()?,
            ]
            .as_slice(),
        )
        .await
        .unwrap();

        Ok(())
    }

@Jarema
Copy link
Member

Jarema commented Jan 13, 2023

Added utility to pass Vec directly and some examples to the docs:
#802

@andreyddk
Copy link
Author

Hey!

ToServerAddrs trait is pretty type-safe and does not try to split &str if that's whats passed.

You can either iterate over some collection and parse addresses, or pass slice directly.

    async fn addrs() -> Result<(), async_nats::Error> {
        let servers = vec!["demo.nats.io", "other.nats.io"];
        let client = async_nats::connect(
            servers
                .iter()
                .map(|url| url.parse())
                .collect::<Result<Vec<ServerAddr>, _>>()?
                .as_slice(),
        )
        .await?;
        

or pass list directly:

        let client = async_nats::connect(
            [
                "demo.nats.io".parse::<ServerAddr>()?,
                "other.nats.io".parse::<ServerAddr>()?,
            ]
            .as_slice(),
        )
        .await
        .unwrap();

        Ok(())
    }

Oh its work, thank you 🙂

@Jarema
Copy link
Member

Jarema commented Jan 18, 2023

Great!
Closing.

@Jarema Jarema closed this as completed Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants