Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add export-import example #104

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions examples/auth/export-import/cli/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/bash

set -euo pipefail

NATS_URL=nats://localhost:4222

cat <<- EOF > "server.conf"
http_port: 8222
jetstream: {}
accounts: {
A: {
jetstream: true
users: [
{user: usera, password: usera}
]
exports: [
# For account B to publish messages to the orders stream.
{service: "orders.*"}

# For account B to bind to the worker-B consumer on orders stream.
# This is specific to a push consumer with flow control enabled.
{stream: "worker-B.orders"}
{service: "\$JS.ACK.orders.worker-B.>"}
{service: "\$JS.FC.orders.worker-B.>"}
]
}

B: {
jetstream: true
users: [
{user: userb, password: userb}
]
imports: [
# Import ability to publish (request) messages into the orders stream.
{service: {account: A, subject: "orders.*"}}

# Import ability to subscribe to the target subject on worker-B consumer.
{stream: {account: A, subject: "worker-B.orders"}}
{service: {account: A, subject: "\$JS.ACK.orders.worker-B.>"}}
{service: {account: A, subject: "\$JS.FC.orders.worker-B.>"}}
]
}
}
EOF

# Start the server.
echo 'Starting the server...'
nats-server -c server.conf > /dev/null 2>&1 &

nats context save A \
--user usera \
--password usera

nats context save B \
--user userb \
--password userb

# Add the stream in account A
nats --context A stream add orders \
--subjects orders.* \
--retention=limits \
--storage=file \
--replicas=1 \
--discard=old \
--dupe-window=2m \
--max-age=-1 \
--max-msgs=-1 \
--max-bytes=-1 \
--max-msg-size=-1 \
--max-msgs-per-subject=-1 \
--max-consumers=-1 \
--allow-rollup \
--no-deny-delete \
--no-deny-purge

# Confirm account B can publish to the stream.
nats --context B req orders.1 "{}"

# Report the stream.
#nats --context A stream report

# Create a consumer in account A whose API will be exported for B.
# Specifically, this requires the `target` subject as a stream, and
# the `$JS.ACK` subject to acknowledge messages.
nats --context A consumer add orders worker-B \
--replicas 1 \
--deliver all \
--deliver-group "" \
--target worker-B.orders \
--ack explicit \
--replay instant \
--filter "" \
--heartbeat "2s" \
--flow-control \
--max-deliver="-1" \
--max-pending 128 \
--no-headers-only \
--backoff=none

# Subscribe and consume one message from the imported subject
# and ack it.
nats --context B sub --ack worker-B.orders --count 1

# Notice the consumer report shows no unprocessed messages.
nats --context A consumer report orders
26 changes: 26 additions & 0 deletions examples/auth/export-import/cli/server.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
http_port: 8222
jetstream: {}
accounts: {
A: {
jetstream: true
users: [
{user: usera, password: usera}
]
exports: [
{service: "orders.*"}
{service: "$JS.API.STREAM.INFO.orders"}
{stream: "worker-B"}
]
}
B: {
jetstream: true
users: [
{user: userb, password: userb}
]
imports: [
{service: {account: A, subject: "orders.*"}}
{service: {account: A, subject: "$JS.API.STREAM.INFO.orders"}}
{stream: {account: A, subject: "worker-B"}}
]
}
}
3 changes: 3 additions & 0 deletions examples/auth/export-import/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: Exports and Imports
description: |-