NATS Streaming服务器已被弃用。关键错误修复和安全修复将应用到 2023 年 6 月。需要持久性的支持 NATS 的应用程序应使用 JetStream。

Nats JetStream

安装

https://docs.nats.io/running-a-nats-service/introduction/installation

运行nats

1
2
nats-server #不支持持久化
nats-server -js #支持持久化

集群

非JetStream

nats_1.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# https://docs.nats.io/running-a-nats-service/configuration/clustering/cluster_config

listen=4222

cluster {
name: example

# host/port for inbound route connections from other server
listen: localhost:4244

# Authorization for route connections
# Other server can connect if they supply the credentials listed here
# This server will connect to discovered routes using this user
authorization {
user: route_user
password: pwd
timeout: 0.5
}

# This server establishes routes with these server.
# This server solicits new routes and Routes are actively solicited and connected to from this server.
# Other servers can connect to us if they supply the correct credentials
# in their routes definitions from above.
routes = [
nats-route://route_user:pwd@127.0.0.1:4245
nats-route://route_user:pwd@127.0.0.1:4246
]
}

nats_2.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# https://docs.nats.io/running-a-nats-service/configuration/clustering/cluster_config

listen=4223

cluster {
name: example

# host/port for inbound route connections from other server
listen: localhost:4245

# Authorization for route connections
# Other server can connect if they supply the credentials listed here
# This server will connect to discovered routes using this user
authorization {
user: route_user
password: pwd
timeout: 0.5
}

# This server establishes routes with these server.
# This server solicits new routes and Routes are actively solicited and connected to from this server.
# Other servers can connect to us if they supply the correct credentials
# in their routes definitions from above.
routes = [
nats-route://route_user:pwd@127.0.0.1:4244
nats-route://route_user:pwd@127.0.0.1:4246
]
}

nats_3.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# https://docs.nats.io/running-a-nats-service/configuration/clustering/cluster_config

listen=4224

cluster {
name: example

# host/port for inbound route connections from other server
listen: localhost:4246

# Authorization for route connections
# Other server can connect if they supply the credentials listed here
# This server will connect to discovered routes using this user
authorization {
user: route_user
password: pwd
timeout: 0.5
}

# This server establishes routes with these server.
# This server solicits new routes and Routes are actively solicited and connected to from this server.
# Other servers can connect to us if they supply the correct credentials
# in their routes definitions from above.
routes = [
nats-route://route_user:pwd@127.0.0.1:4244
nats-route://route_user:pwd@127.0.0.1:4245
]
}

启动nats

1
2
3
./nats-server -c ./nats_1.conf
./nats-server -c ./nats_2.conf
./nats-server -c ./nats_3.conf

JetStream

n1_c1.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# https://docs.nats.io/running-a-nats-service/configuration/clustering/jetstream_clustering
server_name=n1-c1
listen=4222

jetstream {
store_dir=/nats/n1-c1/storage
max_mem: 1G
max_file: 1G
}

cluster {
name: C1
listen: localhost:6222
routes: [
nats-route://localhost:6223
nats-route://localhost:6224
]
}

n2_c1.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# https://docs.nats.io/running-a-nats-service/configuration/clustering/jetstream_clustering
server_name=n2-c1
listen=4223

jetstream {
store_dir=/nats/n2-c1/storage
max_mem: 1G
max_file: 1G
}

cluster {
name: C1
listen: localhost:6223
routes: [
nats-route://localhost:6222
nats-route://localhost:6224
]
}

n3_c1.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# https://docs.nats.io/running-a-nats-service/configuration/clustering/jetstream_clustering
server_name=n3-c1
listen=4224

jetstream {
store_dir=/nats/n3-c1/storage
max_mem: 1G
max_file: 1G
}

cluster {
name: C1
listen: localhost:6224
routes: [
nats-route://localhost:6222
nats-route://localhost:6223
]
}

启动nats jetstream

1
2
3
./nats-server -c ./n1_c1.conf
./nats-server -c ./n2_c1.conf
./nats-server -c ./n3_c1.conf