Powered By GitBook
Leaf Nodes
Using JetStream on Leaf Nodes
If you want to see a demonstration of the full range of this functionality, check out our video
One of the use cases for a NATS server configured as a leaf node is to provide a local NATS network even when the connection to a hub or the cloud is down. To support such a disconnected use case with JetStream, independent JetStream islands are also supported and available through the same NATS network.
The general issue with multiple, independent JetStreams, accessible from the same client is that you need to be able to tell them apart. As an example, consider a leaf node with a non-clustered JetStream in each server. You connect to one of them, but which JetStream responds when you use the JetStream API $JS.API.> ?
To disambiguate between servers, the option domain was added to the JetStream configuration block. When using it, follow these rules: Every server in a cluster and super cluster needs to have the same domain name. This means that domain names can only change between two servers if they are connected via a leaf node connection. As a result of this the JetStream API $JS.API.> will also be available under a disambiguated name $JS.<domain>.API.>. Needless to say, domain names need to be unique.
There are reasons to connect system accounts on either end of your leaf node connection. You probably don't want to connect your cloud and edge device system accounts, but you might connect them when the only reason keeping you from using a super cluster are firewall rules.
The benefits are:
    Monitoring of all connected nats-servers
    nats-account-resolver working on the entire network
    extended JetStream cluster
When domain is set, JetStream-related traffic on the system account is suppressed. This is what causes JetStream not to be extended.
In addition, traffic on $JS.API.> is also suppressed. This causes clients to use the local JetStream that is available in the nats-servers they are connected to. To communicate with another JetStream, that JetStream's unique domain specific prefix $JS.<domain>.API needs to be specified.
Please be aware that each domain is an independent name space. Meaning, inside the same account it is legal to use the same stream name in different domains.
Furthermore, regular message flow is not restricted. Thus, if the same subject is subscribed to by different streams in the same account in different domains, as long as the underlying leaf node was connected at the time, each stream will store the message. This can be resolved by using the same account but use different subjects in each domain or use different accounts in each domain or isolate accounts used in leaf nodes.
Known issue: if you have more than one JetStream enabled leaf node in a different cluster, the cluster you connect to also needs JetStream enabled and a domain set.
Known issue: when you intend to extend a central JetStream, by not supplying a domain name in leaf nodes, that central JetStream needs to be in clustered mode.

Configuration

Below is the config needed to connect two JetStream enabled servers via a leaf node connection. In the example, the system accounts are connected for demonstration purposes (you do not have to do that).

accounts.conf Imported by Both Servers

1
accounts {
2
SYS: {
3
users: [{user: admin, password: admin}]
4
},
5
ACC: {
6
users: [{user: acc, password: acc}],
7
jetstream: enabled
8
}
9
}
10
system_account: SYS
Copied!

hub.conf

To be started with nats-server -c hub.conf:
1
port: 4222
2
server_name: hub-server
3
jetstream {
4
store_dir="./store_server"
5
domain=hub
6
}
7
leafnodes {
8
port: 7422
9
}
10
include ./accounts.conf
Copied!

leaf.conf

To be started with nats-server -c leaf.conf:
1
port: 4111
2
server_name: leaf-server
3
jetstream {
4
store_dir="./store_leaf"
5
domain=leaf
6
}
7
leafnodes {
8
remotes = [
9
{
10
urls: ["nats-leaf://admin:[email protected]:7422"]
11
account: "SYS"
12
},
13
{
14
urls: ["nats-leaf://acc:[email protected]:7422"]
15
account: "ACC"
16
}
17
]
18
}
19
include ./accounts.conf
Copied!

Usage

Because the system account is connected, you can obtain the JetStream server report from both servers.
1
> nats --server nats://admin:[email protected]:4222 server report jetstream
2
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
3
│ JetStream Summary │
4
├─────────────┬─────────────┬────────┬─────────┬───────────┬──────────┬───────┬────────┬──────┬─────────┬─────────┤
5
│ Server │ Cluster │ Domain │ Streams │ Consumers │ Messages │ Bytes │ Memory │ File │ API Req │ API Err │
6
├─────────────┼─────────────┼────────┼─────────┼───────────┼──────────┼───────┼────────┼──────┼─────────┼─────────┤
7
│ leaf-server │ leaf-server │ leaf │ 0000 B │ 0 B │ 0 B │ 00
8
│ hub-server │ │ hub │ 0000 B │ 0 B │ 0 B │ 00
9
├─────────────┼─────────────┼────────┼─────────┼───────────┼──────────┼───────┼────────┼──────┼─────────┼─────────┤
10
│ │ │ │ 0000 B │ 0 B │ 0 B │ 00
11
╰─────────────┴─────────────┴────────┴─────────┴───────────┴──────────┴───────┴────────┴──────┴─────────┴─────────╯
Copied!
Create a stream named test subscribing to subject test in the JetStream domain, the program is connected to. As a result, this stream will be created in the domain hub which is the domain of the server listening on localhost:4222.
1
> nats --server nats://acc:[email protected]:4222 stream add
2
? Stream Name test
3
? Subjects to consume test
4
? Storage backend file
5
? Retention Policy Limits
6
? Discard Policy Old
7
? Stream Messages Limit -1
8
? Per Subject Messages Limit -1
9
? Message size limit -1
10
? Maximum message age limit -1
11
? Maximum individual message size -1
12
? Duplicate tracking time window 2m
13
? Replicas 1
14
Stream test was created
15
16
Information for Stream test created 2021-06-28T12:52:29-04:00
17
18
Configuration:
19
20
Subjects: test
21
Acknowledgements: true
22
Retention: File - Limits
23
Replicas: 1
24
Discard Policy: Old
25
Duplicate Window: 2m0s
26
Maximum Messages: unlimited
27
Maximum Bytes: unlimited
28
Maximum Age: 0.00s
29
Maximum Message Size: unlimited
30
Maximum Consumers: unlimited
31
32
33
State:
34
35
Messages: 0
36
Bytes: 0 B
37
FirstSeq: 0
38
LastSeq: 0
39
Active Consumers: 0
Copied!
To create a stream in a different domain while connected somewhere else, just provide the js-domain argument. While connected to the same server as before, now the stream is created in leaf.
1
> nats --server nats://acc:[email protected]:4222 stream add --js-domain leaf
2
? Stream Name test
3
? Subjects to consume test
4
? Storage backend file
5
? Retention Policy Limits
6
? Discard Policy Old
7
? Stream Messages Limit -1
8
? Per Subject Messages Limit -1
9
? Message size limit -1
10
? Maximum message age limit -1
11
? Maximum individual message size -1
12
? Duplicate tracking time window 2m
13
? Replicas 1
14
Stream test was created
15
16
Information for Stream test created 2021-06-28T12:59:18-04:00
17
18
Configuration:
19
20
Subjects: test
21
Acknowledgements: true
22
Retention: File - Limits
23
Replicas: 1
24
Discard Policy: Old
25
Duplicate Window: 2m0s
26
Maximum Messages: unlimited
27
Maximum Bytes: unlimited
28
Maximum Age: 0.00s
29
Maximum Message Size: unlimited
30
Maximum Consumers: unlimited
31
32
33
State:
34
35
Messages: 0
36
Bytes: 0 B
37
FirstSeq: 0
38
LastSeq: 0
39
Active Consumers: 0
Copied!
Publish a message so there is something to retrieve.
1
> nats --server nats://acc:[email protected]:4222 pub test "hello world"
2
13:33:17 Published 11 bytes to "test"
Copied!
Because both streams subscribe to the same subject, each one now reports one message. This is done to demonstrate the issue. If you want to avoid that, you need to either use different subjects, different accounts, or one isolated account.
1
> nats --server nats://acc:[email protected]:4222 stream report --js-domain leaf
2
Obtaining Stream stats
3
4
╭─────────────────────────────────────────────────────────────────────────────╮
5
│ Stream Report │
6
├────────┬─────────┬───────────┬──────────┬───────┬──────┬─────────┬──────────┤
7
│ Stream │ Storage │ Consumers │ Messages │ Bytes │ Lost │ Deleted │ Replicas │
8
├────────┼─────────┼───────────┼──────────┼───────┼──────┼─────────┼──────────┤
9
test │ File │ 0145 B │ 00 │ │
10
╰────────┴─────────┴───────────┴──────────┴───────┴──────┴─────────┴──────────╯
11
12
> nats --server nats://acc:[email protected]:4222 stream report --js-domain hub
13
Obtaining Stream stats
14
15
╭─────────────────────────────────────────────────────────────────────────────╮
16
│ Stream Report │
17
├────────┬─────────┬───────────┬──────────┬───────┬──────┬─────────┬──────────┤
18
│ Stream │ Storage │ Consumers │ Messages │ Bytes │ Lost │ Deleted │ Replicas │
19
├────────┼─────────┼───────────┼──────────┼───────┼──────┼─────────┼──────────┤
20
test │ File │ 0145 B │ 00 │ │
21
╰────────┴─────────┴───────────┴──────────┴───────┴──────┴─────────┴──────────╯
Copied!

Copying across domains via source or mirror

In order to copy a stream from one domain into another, specify the JetStream domain when creating a mirror. If you want to connect a leaf to the hub and get commands, even when the leaf node connection is offline, mirroring a stream located in the hub is the way to go.
1
> nats --server nats://acc:[email protected]:4222 stream add --js-domain hub --mirror test
2
? Stream Name backup-test-leaf
3
? Storage backend file
4
? Retention Policy Limits
5
? Discard Policy Old
6
? Stream Messages Limit -1
7
? Message size limit -1
8
? Maximum message age limit -1
9
? Maximum individual message size -1
10
? Replicas 1
11
? Adjust mirror start No
12
? Import mirror from a different JetStream domain Yes
13
? Foreign JetStream domain name leaf
14
? Delivery prefix
15
Stream backup-test-leaf was created
16
17
Information for Stream backup-test-leaf created 2021-06-28T14:00:43-04:00
18
19
Configuration:
20
21
Acknowledgements: true
22
Retention: File - Limits
23
Replicas: 1
24
Discard Policy: Old
25
Duplicate Window: 2m0s
26
Maximum Messages: unlimited
27
Maximum Bytes: unlimited
28
Maximum Age: 0.00s
29
Maximum Message Size: unlimited
30
Maximum Consumers: unlimited
31
Mirror: test, API Prefix: $JS.leaf.API, Delivery Prefix:
32
33
34
State:
35
36
Messages: 0
37
Bytes: 0 B
38
FirstSeq: 0
39
LastSeq: 0
40
Active Consumers: 0
Copied!
Similarly, if you want to aggregate streams located in any number of leaf nodes use source. If the streams located in each leaf are used for the same reasons, it is recommended to aggregate them in the hub for processing via source.
1
> nats --server nats://acc:[email protected]:4222 stream add --js-domain hub --source test
2
? Stream Name aggregate-test-leaf
3
? Storage backend file
4
? Retention Policy Limits
5
? Discard Policy Old
6
? Stream Messages Limit -1
7
? Message size limit -1
8
? Maximum message age limit -1
9
? Maximum individual message size -1
10
? Duplicate tracking time window 2m
11
? Replicas 1
12
? Adjust source "test" start No
13
? Import "test" from a different JetStream domain Yes
14
? test Source foreign JetStream domain name leaf
15
? test Source foreign JetStream domain delivery prefix
16
Stream aggregate-test-leaf was created
17
18
Information for Stream aggregate-test-leaf created 2021-06-28T14:02:36-04:00
19
20
Configuration:
21
22
Acknowledgements: true
23
Retention: File - Limits
24
Replicas: 1
25
Discard Policy: Old
26
Duplicate Window: 2m0s
27
Maximum Messages: unlimited
28
Maximum Bytes: unlimited
29
Maximum Age: 0.00s
30
Maximum Message Size: unlimited
31
Maximum Consumers: unlimited
32
Sources: test, API Prefix: $JS.leaf.API, Delivery Prefix:
33
34
35
State:
36
37
Messages: 0
38
Bytes: 0 B
39
FirstSeq: 0
40
LastSeq: 0
41
Active Consumers: 0
Copied!
source as well as mirror take a copy of the messages. Once copied, accessing the data is independent of the leaf node connection being online. Copying this way also avoids having to run a dedicated program of your own. This is the recommended way to exchange persistent data across domains.
1
> nats --server nats://acc:[email protected]:4222 stream report --js-domain hub
2
Obtaining Stream stats
3
4
╭──────────────────────────────────────────────────────────────────────────────────────────╮
5
│ Stream Report │
6
├─────────────────────┬─────────┬───────────┬──────────┬───────┬──────┬─────────┬──────────┤
7
│ Stream │ Storage │ Consumers │ Messages │ Bytes │ Lost │ Deleted │ Replicas │
8
├─────────────────────┼─────────┼───────────┼──────────┼───────┼──────┼─────────┼──────────┤
9
│ backup-test-leaf │ File │ 0145 B │ 00 │ │
10
test │ File │ 0145 B │ 00 │ │
11
│ aggregate-test-leaf │ File │ 0198 B │ 00 │ │
12
╰─────────────────────┴─────────┴───────────┴──────────┴───────┴──────┴─────────┴──────────╯
13
14
╭────────────────────────────────────────────────────────────────────────────────────╮
15
│ Replication Report │
16
├─────────────────────┬────────┬──────────────┬───────────────┬────────┬─────┬───────┤
17
│ Stream │ Kind │ API Prefix │ Source Stream │ Active │ Lag │ Error │
18
├─────────────────────┼────────┼──────────────┼───────────────┼────────┼─────┼───────┤
19
│ backup-test-leaf │ Mirror │ $JS.leaf.API │ test0.21s │ 0 │ │
20
│ aggregate-test-leaf │ Source │ $JS.leaf.API │ test1.23s │ 0 │ │
21
╰─────────────────────┴────────┴──────────────┴───────────────┴────────┴─────┴───────╯
Copied!

Cross account & domain import

All of the above happened in the same account. To share domain access across accounts the account.conf from above needs to be modified and the server restarted or reloaded. This example exports the consumer API as well as a delivery subject which is used by the internal push consumer created by source and mirror.
In support of another example on how to share a durable consumer for client access across domains and accounts, the NEXT and ACK API are exported as well.
On import, the JetStream API prefix $JS.hub.API is renamed to [email protected]. This is to, once more, disambiguate which JetStream a client in the importing account might want to interact with. When using domains, the general recommendation is to export the domain specific API $JS.<domain>.API as this allows you to pin the export to a particular domain.
Furthermore, the delivery subject is extended on import. This is to allow for easier export into multiple accounts.
This example also exports the absolute minimum necessary. It is possible to give access to the entire consumer API $JS.hub.API.CONSUMER.> or the entire API in a domain $JS.hub.API.> or the entire API $JS.API.> wherever the importing client connects.
1
accounts {
2
SYS: {
3
users: [{user: admin, password: admin}]
4
},
5
ACC: {
6
users: [{user: acc, password: acc}],
7
jetstream: enabled
8
exports: [
9
# minimum export needed to allow source/mirror to create a consumer on the fly
10
{service: "$JS.hub.API.CONSUMER.CREATE.*", response_type: "stream"}
11
# minimum export needed for push consumer. This includes source and mirror!
12
{stream: "deliver.acc.hub.>"}
13
# minimum export needed for durable pull consumer `dur` in stream `aggregate-test-leaf`. (clients only - source and mirror do not use this)
14
{service: "$JS.hub.API.CONSUMER.MSG.NEXT.aggregate-test-leaf.dur", response_type: "stream"}
15
# minimum export needed to ack messages for durable consumer `dur` in stream `aggregate-test-leaf`. (clients only - source and mirror do not use this)
16
{service: "$JS.ACK.aggregate-test-leaf.dur.>"}
17
]
18
}
19
IMPORT_MIRROR: {
20
users: [{user: import_mirror, password: import_mirror}],
21
jetstream: enabled
22
imports: [
23
{service: {account: ACC, subject: "$JS.hub.API.CONSUMER.CREATE.*"}, to: "[email protected]*" }
24
{stream: {account: ACC, subject: deliver.acc.hub.import_mirror.>}}
25
]
26
}
27
IMPORT_CLIENT: {
28
users: [{user: import_client, password: import_client}],
29
jetstream: enabled
30
imports: [
31
{service: {account: ACC, subject: "$JS.hub.API.CONSUMER.MSG.NEXT.aggregate-test-leaf.dur"}, to: "[email protected]" }
32
{service: {account: ACC, subject: "$JS.ACK.aggregate-test-leaf.dur.>"}}
33
]
34
}
35
}
36
system_account: SYS
Copied!

Copying via source and mirror

Once the servers have been restarted or reloaded, a mirror can be created as follows (same applies to source): On import from a different account the renamed prefix [email protected] is provided. In addition, the delivery subject name is extended to also include the importing domain and stream. This makes it unique to that particular import. If every delivery prefix follows the pattern <static type>.<exporting account>.<exporting domain>.<importing account>.<importing domain>.<importing domain>.<importing stream name> overlaps caused by multiple imports are avoided.
1
> nats --server nats://import_mirror:[email protected]:4222 stream add --js-domain hub --mirror aggregate-test-leaf
2
? Stream Name aggregate-test-leaf-from-acc
3
? Storage backend file
4
? Retention Policy Limits
5
? Discard Policy Old
6
? Stream Messages Limit -1
7
? Message size limit -1
8
? Maximum message age limit -1
9
? Maximum individual message size -1
10
? Replicas 1
11
? Adjust mirror start No
12
? Import mirror from a different JetStream domain No
13
? Import mirror from a different account Yes
14
? Foreign account API prefix [email protected]
15
? Foreign account delivery prefix deliver.acc.hub.import_mirror.hub.aggregate-test-leaf-from-acc
16
Stream aggregate-test-leaf-from-acc was created
17
18
Information for Stream aggregate-test-leaf-from-acc created 2021-06-28T16:59:15-04:00
19
20
Configuration:
21
22
Acknowledgements: true
23
Retention: File - Limits
24
Replicas: 1
25
Discard Policy: Old
26
Duplicate Window: 2m0s
27
Maximum Messages: unlimited
28
Maximum Bytes: unlimited
29
Maximum Age: 0.00s
30
Maximum Message Size: unlimited
31
Maximum Consumers: unlimited
32
Mirror: aggregate-test-leaf, API Prefix: [email protected], Delivery Prefix: deliver.acc.hub.import_mirror.hub.aggregate-test-leaf-from-acc
33
34
35
State:
36
37
Messages: 0
38
Bytes: 0 B
39
FirstSeq: 0
40
LastSeq: 0
41
Active Consumers: 0
Copied!
A subsequent check shows that the one message stored in the stream aggregate in account ACC got copied to the new stream in the account IMPORTER.
1
> nats --server nats://import_mirror:[email protected]:4222 stream report --js-domain hub
2
Obtaining Stream stats
3
4
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
5
│ Stream Report │
6
├──────────────────────────────┬─────────┬───────────┬──────────┬───────┬──────┬─────────┬──────────┤
7
│ Stream │ Storage │ Consumers │ Messages │ Bytes │ Lost │ Deleted │ Replicas │
8
├──────────────────────────────┼─────────┼───────────┼──────────┼───────┼──────┼─────────┼──────────┤
9
│ aggregate-test-leaf-from-acc │ File │ 0198 B │ 00 │ │
10
╰──────────────────────────────┴─────────┴───────────┴──────────┴───────┴──────┴─────────┴──────────╯
11
12
╭─────────────────────────────────────────────────────────────────────────────────────────────────────╮
13
│ Replication Report │
14
├──────────────────────────────┬────────┬────────────────┬─────────────────────┬────────┬─────┬───────┤
15
│ Stream │ Kind │ API Prefix │ Source Stream │ Active │ Lag │ Error │
16
├──────────────────────────────┼────────┼────────────────┼─────────────────────┼────────┼─────┼───────┤
17
│ aggregate-test-leaf-from-acc │ Mirror │ [email protected] │ aggregate-test-leaf │ 0.59s │ 0 │ │
18
╰──────────────────────────────┴────────┴────────────────┴─────────────────────┴────────┴─────┴───────╯
Copied!

Direct access of a durable pull consumer

The modified accounts.conf also includes a separate import for an existing pull consumer. Let's create a consumer by the name dur in the stream aggregate-test-leaf in the account acc.
1
> nats --server nats://acc:[email protected]:4222 consumer add --js-domain hub
2
? Consumer name dur
3
? Delivery target (empty for Pull Consumers)
4
? Start policy (all, new, last, 1h, msg sequence) all
5
? Replay policy instant
6
? Filter Stream by subject (blank for all)
7
? Maximum Allowed Deliveries -1
8
? Maximum Acknowledgements Pending 0
9
? Select a Stream aggregate-test-leaf
10
Information for Consumer aggregate-test-leaf > dur created 2021-06-28T17:16:51-04:00
11
12
Configuration:
13
14
Durable Name: dur
15
Pull Mode: true
16
Deliver All: true
17
Ack Policy: Explicit
18
Ack Wait: 30s
19
Replay Policy: Instant
20
Max Ack Pending: 20,000
21
Max Waiting Pulls: 512
22
23
State:
24
25
Last Delivered Message: Consumer sequence: 0 Stream sequence: 0
26
Acknowledgment floor: Consumer sequence: 0 Stream sequence: 0
27
Outstanding Acks: 0 out of maximum 20000
28
Redelivered Messages: 0
29
Unprocessed Messages: 1
30
Waiting Pulls: 0 of maximum 512
31
32
> nats --server nats://acc:[email protected]:4222 stream report --js-domain hub
33
Obtaining Stream stats
34
35
╭──────────────────────────────────────────────────────────────────────────────────────────╮
36
│ Stream Report │
37
├─────────────────────┬─────────┬───────────┬──────────┬───────┬──────┬─────────┬──────────┤
38
│ Stream │ Storage │ Consumers │ Messages │ Bytes │ Lost │ Deleted │ Replicas │
39
├─────────────────────┼─────────┼───────────┼──────────┼───────┼──────┼─────────┼──────────┤
40
│ backup-test-leaf │ File │ 0145 B │ 00 │ │
41
test │ File │ 0145 B │ 00 │ │
42
│ aggregate-test-leaf │ File │ 1198 B │ 00 │ │
43
╰─────────────────────┴─────────┴───────────┴──────────┴───────┴──────┴─────────┴──────────╯
44
45
╭────────────────────────────────────────────────────────────────────────────────────╮
46
│ Replication Report │
47
├─────────────────────┬────────┬──────────────┬───────────────┬────────┬─────┬───────┤
48
│ Stream │ Kind │ API Prefix │ Source Stream │ Active │ Lag │ Error │
49
├─────────────────────┼────────┼──────────────┼───────────────┼────────┼─────┼───────┤
50
│ backup-test-leaf │ Mirror │ $JS.leaf.API │ test1.85s │ 0 │ │
51
│ aggregate-test-leaf │ Source │ $JS.leaf.API │ test1.85s │ 0 │ │
52
╰─────────────────────┴────────┴──────────────┴───────────────┴────────┴─────┴───────╯
53
> nats --server nats://acc:[email protected]:4222 consumer report --js-domain hub
54
? Select a Stream aggregate-test-leaf
55
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
56
│ Consumer report for aggregate-test-leaf with 1 consumers │
57
├──────────┬──────┬────────────┬──────────┬─────────────┬─────────────┬─────────────┬───────────┬─────────────┤
58
│ Consumer │ Mode │ Ack Policy │ Ack Wait │ Ack Pending │ Redelivered │ Unprocessed │ Ack Floor │ Cluster │
59
├──────────┼──────┼────────────┼──────────┼─────────────┼─────────────┼─────────────┼───────────┼─────────────┤
60
│ dur │ Pull │ Explicit │ 30.00s │ 001 / 100% │ 0 │ hub-server* │
61
╰──────────┴──────┴────────────┴──────────┴─────────────┴─────────────┴─────────────┴───────────┴─────────────╯
Copied!
To retrieve the messages stored in the domain hub using nats while connected to the leaf node, provide the correct stream and durable name as well as the API prefix [email protected]
1
nats --server nats://import_client:[email protected]:4111 consumer next aggregate-test-leaf dur --js-api-prefix [email protected]
2
[17:44:16] subj: test / tries: 1 / cons seq: 1 / str seq: 1 / pending: 0
3
4
Headers:
5
6
Nats-Stream-Source: test:mSx7q4yJ 1
7
8
Data:
9
10
11
hello world
12
13
Acknowledged message
14
15
> nats --server nats://acc:[email protected]:4222 consumer report --js-domain hub
16
? Select a Stream aggregate-test-leaf
17
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
18
│ Consumer report for aggregate-test-leaf with 1 consumers │
19
├──────────┬──────┬────────────┬──────────┬─────────────┬─────────────┬─────────────┬───────────┬─────────────┤
20
│ Consumer │ Mode │ Ack Policy │ Ack Wait │ Ack Pending │ Redelivered │ Unprocessed │ Ack Floor │ Cluster │
21
├──────────┼──────┼────────────┼──────────┼─────────────┼─────────────┼─────────────┼───────────┼─────────────┤
22
│ dur │ Pull │ Explicit │ 30.00s │ 0 │ 0 │ 0 │ 1 │ hub-server* │
23
╰──────────┴──────┴────────────┴──────────┴─────────────┴─────────────┴─────────────┴───────────┴─────────────╯
Copied!
This works similarly when writing your own client. To avoid waiting for the ack timeout, a new message is sent on test from where it is copied into aggregate-test-leaf.
1
> nats --server nats://acc:[email protected]:4222 pub test "hello world 2"
2
17:51:05 Published 13 bytes to "test"
Copied!
The client is connected to the leaf node and receives the message just sent.
1
./main nats://import_client:[email protected]:4111
2
starting
3
&{Sequence:{Consumer:3 Stream:3} NumDelivered:1 NumPending:0 Timestamp:2021-06-28 17:51:05.186878 -0400 EDT Stream:aggregate-test-leaf Consumer:dur}
4
hello world 2
5
nats: timeout
6
^Cnats: timeout
Copied!
There the API prefix is communicated with setting the option nats.APIPrefix("[email protected]") when obtaining the JetStream object. Because the API access is limited, the subscribe call provides the option nats.Bind("aggregate-test-leaf", "dur") which prevents calls to infer the stream and durable name.
1
package main
2
3
import (
4
"fmt"
5
"os"
6
"os/signal"
7
"syscall"
8
"time"
9
10
"github.com/nats-io/nats.go"
11
)
12
13
func main() {
14
nc, err := nats.Connect(os.Args[1], nats.Name("JS test"))
15
defer nc.Close()
16
if err != nil {
17
fmt.Printf("nats connect: %v\n", err)
18
return
19
}
20
js, err := nc.JetStream(nats.APIPrefix("[email protected]"))
21
if err != nil {
22
fmt.Printf("JetStream: %v\n", err)
23
if js == nil {
24
return
25
}
26
}
27
s, err := js.PullSubscribe("", "dur", nats.Bind("aggregate-test-leaf", "dur"))
28
if err != nil {
29
fmt.Printf("PullSubscribe: %v\n", err)
30
return
31
}
32
33
shutdown := make(chan os.Signal, 1)
34
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
35
36
fmt.Printf("starting\n")
37
for {
38
select {
39
case <-shutdown:
40
return
41
default:
42
if m, err := s.Fetch(1, nats.MaxWait(time.Second)); err != nil {
43
fmt.Println(err)
44
} else {
45
46
if meta, err := m[0].Metadata(); err == nil {
47
fmt.Printf("%+v\n", meta)
48
}
49
fmt.Println(string(m[0].Data))
50
51
if err := m[0].Ack(); err != nil {
52
fmt.Printf("ack error: %+v\n", err)
53
}
54
}
55
}
56
}
57
}
Copied!
A push subscriber will need a similar setup. It will require the ACK subject. However, instead of exporting/importing the NEXT subject, the delivery subject shown for source/mirror needs to be used.
Last modified 3mo ago