1
0
mirror of https://github.com/taigrr/nats.docs synced 2025-01-18 04:03:23 -08:00

GitBook: [master] 60 pages and 12 assets modified

This commit is contained in:
Ginger Collison
2021-03-15 14:08:37 +00:00
committed by gitbook-bot
parent 3429cc74ff
commit 0f1d9e01a8
59 changed files with 714 additions and 661 deletions

View File

@@ -1,8 +1,8 @@
## Administration and Usage from the CLI
# Administration & Usage from CLI
Once the server is running it's time to use the management tool. This can be downloaded from the [GitHub Release Page](https://github.com/nats-io/natscli/releases/) or you can use the `natsio/nats-box:latest` docker image. On OS X homebrew can be used to install the latest version:
```nohighlight
```text
$ brew tap nats-io/nats-tools
$ brew install nats-io/nats-tools/nats
$ nats --help
@@ -30,3 +30,4 @@ We'll walk through the above scenario and introduce features of the CLI and of J
Throughout this example, we'll show other commands like `nats pub` and `nats sub` to interact with the system. These are normal existing core NATS commands and JetStream is fully usable by only using core NATS.
We'll touch on some additional features but please review the section on the design model to understand all possible permutations.

View File

@@ -1,8 +1,10 @@
### Account Information
# Account Information
## Account Information
JetStream is multi-tenant so you will need to check that your account is enabled for JetStream and is not limited. You can view your limits as follows:
```nohighlight
```text
$ nats account info
Connection Information:
Client ID: 8
@@ -20,13 +22,13 @@ JetStream Account Information:
Max Consumers: unlimited
```
### Streams
## Streams
The first step is to set up storage for our `ORDERS` related messages, these arrive on a wildcard of subjects all flowing into the same Stream and they are kept for 1 year.
#### Creating
### Creating
```nohighlight
```text
$ nats str add ORDERS
? Subjects to consume ORDERS.*
? Storage backend file
@@ -64,31 +66,32 @@ Statistics:
You can get prompted interactively for missing information as above, or do it all on one command. Pressing `?` in the CLI will help you map prompts to CLI options:
```text
$ nats str add ORDERS --subjects "ORDERS.*" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1 --discard old --replicas 3
```
$ nats str add ORDERS --subjects "ORDERS.*" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1 --discard old --replicas 3```
Additionally, one can store the configuration in a JSON file, the format of this is the same as `$ nats str info ORDERS -j | jq .config`:
```
```text
$ nats str add ORDERS --config orders.json
```
#### Listing
### Listing
We can confirm our Stream was created:
```nohighlight
```text
$ nats str ls
Streams:
ORDERS
ORDERS
```
#### Querying
### Querying
Information about the configuration of the Stream can be seen, and if you did not specify the Stream like below, it will prompt you based on all known ones:
```nohighlight
```text
$ nats str info ORDERS
Information for Stream ORDERS
@@ -114,7 +117,7 @@ Statistics:
Most commands that show data as above support `-j` to show the results as JSON:
```nohighlight
```text
$ nats str info ORDERS -j
{
"config": {
@@ -144,7 +147,7 @@ This is the general pattern for the entire `nats` utility as it relates to JetSt
In clustered mode additional information will be included:
```nohighlight
```text
$ nats str info ORDERS
...
Cluster Information:
@@ -156,11 +159,11 @@ Cluster Information:
Here the cluster name is configured as `JSC`, there is a server `S1` that's the current leader with `S3` and `S2` are replicas. Both replicas are current and have been seen recently.
#### Copying
### Copying
A stream can be copied into another, which also allows the configuration of the new one to be adjusted via CLI flags:
```nohighlight
```text
$ nats str cp ORDERS ARCHIVE --subjects "ORDERS_ARCVHIVE.*" --max-age 2y
Stream ORDERS was created
@@ -174,11 +177,11 @@ Configuration:
...
```
#### Editing
### Editing
A stream configuration can be edited, which allows the configuration to be adjusted via CLI flags. Here I have a incorrectly created ORDERS stream that I fix:
A stream configuration can be edited, which allows the configuration to be adjusted via CLI flags. Here I have a incorrectly created ORDERS stream that I fix:
```nohighlight
```text
$ nats str info ORDERS -j | jq .config.subjects
[
"ORDERS.new"
@@ -197,24 +200,24 @@ Configuration:
Additionally one can store the configuration in a JSON file, the format of this is the same as `$ nats str info ORDERS -j | jq .config`:
```
```text
$ nats str edit ORDERS --config orders.json
```
#### Publishing Into a Stream
### Publishing Into a Stream
Now let's add in some messages to our Stream. You can use `nats pub` to add messages, pass the `--wait` flag to see the publish ack being returned.
You can publish without waiting for acknowledgement:
```nohighlight
```text
$ nats pub ORDERS.scratch hello
Published [sub1] : 'hello'
```
But if you want to be sure your messages got to JetStream and were persisted you can make a request:
```nohighlight
```text
$ nats req ORDERS.scratch hello
13:45:03 Sending request on [ORDERS.scratch]
13:45:03 Received on [_INBOX.M8drJkd8O5otORAo0sMNkg.scHnSafY]: '+OK'
@@ -222,7 +225,7 @@ $ nats req ORDERS.scratch hello
Keep checking the status of the Stream while doing this and you'll see it's stored messages increase.
```nohighlight
```text
$ nats str info ORDERS
Information for Stream ORDERS
...
@@ -237,11 +240,11 @@ Statistics:
After putting some throw away data into the Stream, we can purge all the data out - while keeping the Stream active:
#### Deleting All Data
### Deleting All Data
To delete all data in a stream use `purge`:
```nohighlight
```text
$ nats str purge ORDERS -f
...
Statistics:
@@ -253,19 +256,20 @@ Statistics:
Active Consumers: 0
```
#### Deleting A Message
### Deleting A Message
A single message can be securely removed from the stream:
```nohighlight
```text
$ nats str rmm ORDERS 1 -f
```
#### Deleting Sets
### Deleting Sets
Finally for demonstration purposes, you can also delete the whole Stream and recreate it so then we're ready for creating the Consumers:
```
```text
$ nats str rm ORDERS -f
$ nats str add ORDERS --subjects "ORDERS.*" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1
```

View File

@@ -1,14 +1,14 @@
### Consumers
# Consumers
Consumers is how messages are read or consumed from the Stream. We support pull and push-based Consumers and the example scenario has both, lets walk through that.
#### Creating Pull-Based Consumers
## Creating Pull-Based Consumers
The `NEW` and `DISPATCH` Consumers are pull-based, meaning the services consuming data from them have to ask the system for the next available message. This means you can easily scale your services up by adding more workers and the messages will get spread across the workers based on their availability.
The `NEW` and `DISPATCH` Consumers are pull-based, meaning the services consuming data from them have to ask the system for the next available message. This means you can easily scale your services up by adding more workers and the messages will get spread across the workers based on their availability.
Pull-based Consumers are created the same as push-based Consumers, just don't specify a delivery target.
```
```text
$ nats con ls ORDERS
No Consumers defined
```
@@ -17,7 +17,7 @@ We have no Consumers, lets add the `NEW` one:
I supply the `--sample` options on the CLI as this is not prompted for at present, everything else is prompted. The help in the CLI explains each:
```
```text
$ nats con add --sample 100
? Select a Stream ORDERS
? Consumer name NEW
@@ -48,29 +48,29 @@ State:
Redelivered Messages: 0
```
This is a pull-based Consumer (empty Delivery Target), it gets messages from the first available message and requires specific acknowledgement of each and every message.
This is a pull-based Consumer \(empty Delivery Target\), it gets messages from the first available message and requires specific acknowledgement of each and every message.
It only received messages that originally entered the Stream on `ORDERS.received`. Remember the Stream subscribes to `ORDERS.*`, this lets us select a subset of messages from the Stream.
A Maximum Delivery limit of 20 is set, this means if the message is not acknowledged it will be retried but only up to this maximum total deliveries.
Again this can all be done in a single CLI call, lets make the `DISPATCH` Consumer:
```
```text
$ nats con add ORDERS DISPATCH --filter ORDERS.processed --ack explicit --pull --deliver all --sample 100 --max-deliver 20
```
Additionally, one can store the configuration in a JSON file, the format of this is the same as `$ nats con info ORDERS DISPATCH -j | jq .config`:
```
```text
$ nats con add ORDERS MONITOR --config monitor.json
```
#### Creating Push-Based Consumers
## Creating Push-Based Consumers
Our `MONITOR` Consumer is push-based, has no ack and will only get new messages and is not sampled:
```
```text
$ nats con add
? Select a Stream ORDERS
? Consumer name MONITOR
@@ -101,21 +101,21 @@ State:
Again you can do this with a single non interactive command:
```
```text
$ nats con add ORDERS MONITOR --ack none --target monitor.ORDERS --deliver last --replay instant --filter ''
```
Additionally one can store the configuration in a JSON file, the format of this is the same as `$ nats con info ORDERS MONITOR -j | jq .config`:
```
```text
$ nats con add ORDERS --config monitor.json
```
#### Listing
## Listing
You can get a quick list of all the Consumers for a specific Stream:
```
```text
$ nats con ls ORDERS
Consumers for Stream ORDERS:
@@ -124,11 +124,11 @@ Consumers for Stream ORDERS:
NEW
```
#### Querying
## Querying
All details for an Consumer can be queried, lets first look at a pull-based Consumer:
```
```text
$ nats con info ORDERS DISPATCH
Information for Consumer ORDERS > DISPATCH
@@ -154,13 +154,13 @@ State:
More details about the `State` section will be shown later when discussing the ack models in depth.
#### Consuming Pull-Based Consumers
## Consuming Pull-Based Consumers
Pull-based Consumers require you to specifically ask for messages and ack them, typically you would do this with the client library `Request()` feature, but the `nats` utility has a helper:
First we ensure we have a message:
```
```text
$ nats pub ORDERS.processed "order 1"
$ nats pub ORDERS.processed "order 2"
$ nats pub ORDERS.processed "order 3"
@@ -168,7 +168,7 @@ $ nats pub ORDERS.processed "order 3"
We can now read them using `nats`:
```
```text
$ nats con next ORDERS DISPATCH
--- received on ORDERS.processed
order 1
@@ -186,7 +186,7 @@ You can prevent ACKs by supplying `--no-ack`.
To do this from code you'd send a `Request()` to `$JS.API.CONSUMER.MSG.NEXT.ORDERS.DISPATCH`:
```
```text
$ nats req '$JS.API.CONSUMER.MSG.NEXT.ORDERS.DISPATCH' ''
Published [$JS.API.CONSUMER.MSG.NEXT.ORDERS.DISPATCH] : ''
Received [ORDERS.processed] : 'order 3'
@@ -194,11 +194,11 @@ Received [ORDERS.processed] : 'order 3'
Here `nats req` cannot ack, but in your code you'd respond to the received message with a nil payload as an Ack to JetStream.
#### Consuming Push-Based Consumers
## Consuming Push-Based Consumers
Push-based Consumers will publish messages to a subject and anyone who subscribes to the subject will get them, they support different Acknowledgement models covered later, but here on the `MONITOR` Consumer we have no Acknowledgement.
```
```text
$ nats con info ORDERS MONITOR
...
Delivery Subject: monitor.ORDERS
@@ -207,7 +207,7 @@ $ nats con info ORDERS MONITOR
The Consumer is publishing to that subject, so lets listen there:
```
```text
$ nats sub monitor.ORDERS
Listening on [monitor.ORDERS]
[#3] Received on [ORDERS.processed]: 'order 3'
@@ -217,3 +217,4 @@ Listening on [monitor.ORDERS]
Note the subject here of the received message is reported as `ORDERS.processed` this helps you distinguish what you're seeing in a Stream covering a wildcard, or multiple subject, subject space.
This Consumer needs no ack, so any new message into the ORDERS system will show up here in real time.

View File

@@ -1,10 +1,10 @@
### Streams
# Streams
The first step is to set up storage for our `ORDERS` related messages, these arrive on a wildcard of subjects all flowing into the same Stream and they are kept for 1 year.
#### Creating
## Creating
```nohighlight
```text
$ nats str add ORDERS
? Subjects to consume ORDERS.*
? Storage backend file
@@ -41,32 +41,32 @@ Statistics:
You can get prompted interactively for missing information as above, or do it all on one command. Pressing `?` in the CLI will help you map prompts to CLI options:
```
```text
nats str add ORDERS --subjects "ORDERS.*" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1 --discard old --dupe-window="0s" --replicas 1
```
Additionally one can store the configuration in a JSON file, the format of this is the same as `$ nats str info ORDERS -j | jq .config`:
```
```text
$ nats str add ORDERS --config orders.json
```
#### Listing
## Listing
We can confirm our Stream was created:
```nohighlight
```text
$ nats str ls
Streams:
ORDERS
ORDERS
```
#### Querying
## Querying
Information about the configuration of the Stream can be seen, and if you did not specify the Stream like below, it will prompt you based on all known ones:
```nohighlight
```text
$ nats str info ORDERS
Information for Stream ORDERS created 2021-02-27T16:49:36-07:00
@@ -95,7 +95,7 @@ State:
Most commands that show data as above support `-j` to show the results as JSON:
```nohighlight
```text
$ nats str info ORDERS -j
{
"config": {
@@ -129,11 +129,11 @@ $ nats str info ORDERS -j
This is the general pattern for the entire `nats` utility as it relates to JetStream - prompting for needed information but every action can be run non-interactively making it usable as a cli api. All information output like seen above can be turned into JSON using `-j`.
#### Copying
## Copying
A stream can be copied into another, which also allows the configuration of the new one to be adjusted via CLI flags:
```nohighlight
```text
$ nats str cp ORDERS ARCHIVE --subjects "ORDERS_ARCVHIVE.*" --max-age 2y
Stream ORDERS was created
@@ -162,11 +162,11 @@ State:
Active Consumers: 0
```
#### Editing
## Editing
A stream configuration can be edited, which allows the configuration to be adjusted via CLI flags. Here I have a incorrectly created ORDERS stream that I fix:
A stream configuration can be edited, which allows the configuration to be adjusted via CLI flags. Here I have a incorrectly created ORDERS stream that I fix:
```nohighlight
```text
$ nats str info ORDERS -j | jq .config.subjects
[
"ORDERS.new"
@@ -185,24 +185,24 @@ Configuration:
Additionally one can store the configuration in a JSON file, the format of this is the same as `$ nats str info ORDERS -j | jq .config`:
```
```text
$ nats str edit ORDERS --config orders.json
```
#### Publishing Into a Stream
## Publishing Into a Stream
Now let's add in some messages to our Stream. You can use `nats pub` to add messages, pass the `--wait` flag to see the publish ack being returned.
You can publish without waiting for acknowledgement:
```nohighlight
```text
$ nats pub ORDERS.scratch hello
Published [sub1] : 'hello'
```
But if you want to be sure your messages got to JetStream and were persisted you can make a request:
```nohighlight
```text
$ nats req ORDERS.scratch hello
13:45:03 Sending request on [ORDERS.scratch]
13:45:03 Received on [_INBOX.M8drJkd8O5otORAo0sMNkg.scHnSafY]: '+OK'
@@ -210,7 +210,7 @@ $ nats req ORDERS.scratch hello
Keep checking the status of the Stream while doing this and you'll see it's stored messages increase.
```nohighlight
```text
$ nats str info ORDERS
Information for Stream ORDERS
...
@@ -225,11 +225,11 @@ Statistics:
After putting some throw away data into the Stream, we can purge all the data out - while keeping the Stream active:
#### Deleting All Data
## Deleting All Data
To delete all data in a stream use `purge`:
```nohighlight
```text
$ nats str purge ORDERS -f
...
State:
@@ -241,19 +241,20 @@ State:
Active Consumers: 0
```
#### Deleting A Message
## Deleting A Message
A single message can be securely removed from the stream:
```nohighlight
```text
$ nats str rmm ORDERS 1 -f
```
#### Deleting Sets
## Deleting Sets
Finally for demonstration purposes, you can also delete the whole Stream and recreate it so then we're ready for creating the Consumers:
```
```text
$ nats str rm ORDERS -f
$ nats str add ORDERS --subjects "ORDERS.*" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1 --discard old --dupe-window="0s" --replicas 1
```