From 792b6f43f4a247cf4b361d8dafc99f04f8339a4a Mon Sep 17 00:00:00 2001 From: Byron Ruth Date: Thu, 6 Apr 2023 07:26:21 -0400 Subject: [PATCH] Add export-import example Signed-off-by: Byron Ruth --- examples/auth/export-import/cli/main.sh | 105 ++++++++++++++++++++ examples/auth/export-import/cli/server.conf | 26 +++++ examples/auth/export-import/meta.yaml | 3 + 3 files changed, 134 insertions(+) create mode 100644 examples/auth/export-import/cli/main.sh create mode 100644 examples/auth/export-import/cli/server.conf create mode 100644 examples/auth/export-import/meta.yaml diff --git a/examples/auth/export-import/cli/main.sh b/examples/auth/export-import/cli/main.sh new file mode 100644 index 00000000..9da019d4 --- /dev/null +++ b/examples/auth/export-import/cli/main.sh @@ -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 diff --git a/examples/auth/export-import/cli/server.conf b/examples/auth/export-import/cli/server.conf new file mode 100644 index 00000000..df02a9a8 --- /dev/null +++ b/examples/auth/export-import/cli/server.conf @@ -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"}} + ] + } +} diff --git a/examples/auth/export-import/meta.yaml b/examples/auth/export-import/meta.yaml new file mode 100644 index 00000000..101473e7 --- /dev/null +++ b/examples/auth/export-import/meta.yaml @@ -0,0 +1,3 @@ +title: Exports and Imports +description: |- +