JetStream 演练
Prerequisite: enabling JetStream
If you are running a local nats-server
stop it and restart it with JetStream enabled using nats-server -js
(if that's not already done)
You can then check that JetStream is enabled by using
If you see the below then JetStream is not enabled
1. Creating a stream
Let's start by creating a stream to capture and store the messages published on the subject "foo".
Enter nats stream add <Stream name>
(in the examples below we will name the stream "my_stream"), then enter "foo" as the subject name and hit return to use the defaults for all the other stream attributes:
You can then check the information about the stream you just created:
2. Publish some messages into the stream
Let's now start a publisher
As messages are being published on the subject "foo" they are also captured and stored in the stream, you can check that by using nats stream info my_stream
and even look at the messages themselves using nats stream view my_stream
3. Creating a consumer
Now at this point if you create a 'Core NATS' (i.e. non-streaming) subscriber to listen for messages on the subject 'foo', you will only receive the messages being published after the subscriber was started, this is normal and expected for the basic 'Core NATS' messaging. In order to receive a 'replay' of all the messages contained in the stream (including those that were published in the past) we will now create a 'consumer'
We can administratively create a consumer using the 'nats consumer add ' command, in this example we will name the consumer "pull_consumer", and we will leave the delivery subject to 'nothing' (i.e. just hit return at the prompt) because we are creating a 'pull consumer' and select all
for the start policy, you can then just use the defaults and hit return for all the other prompts. The stream the consumer is created on should be the stream 'my_stream' we just created above.
You can check on the status of any consumer at any time using nats consumer info
or view the messages in the stream using nats stream view my_stream
or even remove individual messages from the stream using nats stream rmm
3. Subscribing from the consumer
Now that the consumer has been created and since there are messages in the stream we can now start subscribing to the consumer:
This will print out all the messages in the stream starting with the first message (which was published in the past) and continuing with new messages as they are published until the count is reached.
Note that in this example we are creating a pull consumer with a 'durable' name, this means that the consumer can be shared between as many consuming processes as you want. For example instead of running a single nats consumer next
with a count of 1000 messages you could have started two instances of nats consumer
each with a message count of 500 and you would see the consumption of the messages from the consumer distributed between those instances of nats
Replaying the messages again
Once you have iterated over all the messages in the stream with the consumer, you can get them again by simply creating a new consumer or by deleting that consumer (nats consumer rm
) and re-creating it (nats consumer add
).
4. Cleaning up
You can clean up a stream (and release the resources associated with it (e.g. the messages stored in the stream)) using nats stream purge
You can also delete a stream (which will also automatically delete all of the consumers that may be defined on that stream) using nats stream rm
最后更新于