Powered By GitBook
Connecting to a Cluster
When connecting to a cluster, there are a few things to think about.
    Passing a URL for each cluster member (semi-optional)
    The connection algorithm
    The reconnect algorithm (discussed later)
    Server provided URLs
When a client library first tries to connect it will use the list of URLs provided to the connection options or function. These URLs are usually checked in random order as to not have every client connect to the same server. The first successful connection is used. Randomization can be explicitly disabled.
After a client connects to the server, the server may provide a list of URLs for additional known servers. This allows a client to connect to one server and still have other servers available during reconnect.
To ensure the initial connection, your code should include a list of reasonable front line or seed servers. Those servers may know about other members of the cluster, and may tell the client about those members. But you don't have to configure the client to pass every valid member of the cluster in the connect method.
By providing the ability to pass multiple connect options, NATS can handle the possibility of a machine going down or being unavailable to a client. By adding the ability of the server to feed clients a list of known servers as part of the client-server protocol the mesh created by a cluster can grow and change organically while the clients are running.
Note, failure behavior is library dependent, please check the documentation for your client library on information about what happens if the connect fails.
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
servers := []string{"nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://127.0.0.1:1224"}
2
3
nc, err := nats.Connect(strings.Join(servers, ","))
4
if err != nil {
5
log.Fatal(err)
6
}
7
defer nc.Close()
8
9
// Do something with the connection
Copied!
1
Options options = new Options.Builder().
2
server("nats://localhost:1222").
3
server("nats://localhost:1223").
4
server("nats://localhost:1224").
5
build();
6
Connection nc = Nats.connect(options);
7
8
// Do something with the connection
9
10
nc.close();
Copied!
1
let nc = NATS.connect({
2
servers: [
3
"nats://demo.nats.io:4222",
4
"nats://localhost:4222"
5
]}
6
);
7
nc.on('connect', (c) => {
8
// Do something with the connection
9
doSomething();
10
// When done close it
11
nc.close();
12
});
13
nc.on('error', (err) => {
14
failed(err);
15
});
Copied!
1
nc = NATS()
2
await nc.connect(servers=[
3
"nats://127.0.0.1:1222",
4
"nats://127.0.0.1:1223",
5
"nats://127.0.0.1:1224"
6
])
7
8
# Do something with the connection
9
10
await nc.close()
Copied!
1
require 'nats/client'
2
3
NATS.start(servers: ["nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://127.0.0.1:1224"]) do |nc|
4
# Do something with the connection
5
6
# Close the connection
7
nc.close
8
end
Copied!
1
// will throw an exception if connection fails
2
let nc = await connect({
3
servers: [
4
"nats://demo.nats.io:4222",
5
"nats://localhost:4222"
6
]
7
});
8
// Do something with the connection
9
10
// When done close it
11
nc.close();
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
const char *servers[] = {"nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://127.0.0.1:1224"};
5
6
s = natsOptions_Create(&opts);
7
if (s == NATS_OK)
8
s = natsOptions_SetServers(opts, servers, 3);
9
if (s == NATS_OK)
10
s = natsConnection_Connect(&conn, opts);
11
12
(...)
13
14
// Destroy objects that were created
15
natsConnection_Destroy(conn);
16
natsOptions_Destroy(opts);
Copied!
Last modified 1yr ago
Export as PDF
Copy link