Skip to content

Instantly share code, notes, and snippets.

@johanrhodin
Created March 1, 2025 03:52
Show Gist options
  • Save johanrhodin/a6d9f6c339ddfd19954727fc12cd3af9 to your computer and use it in GitHub Desktop.
Save johanrhodin/a6d9f6c339ddfd19954727fc12cd3af9 to your computer and use it in GitHub Desktop.
Minimum Viable Exchange Federation
#!/bin/bash
# Minimum Viable Exchange Federation
# Upstream servers are the servers towards where messages are originally published
# Downstream servers are where the messages get forwarded to
# Two clusters
CREDS="nbqblhiv:PASSWORD_GOES_HERE"
VHOST="nbqblhiv"
UPSTREAM_URL="amqps://$CREDS@test-brisk-white-giraffe.rmq6.cloudamqp.com/$VHOST"
HTTPS_DOWNSTREAM="https://$CREDS@test-merry-lime-owl.rmq6.cloudamqp.com"
HTTPS_UPSTREAM="https://$CREDS@test-brisk-white-giraffe.rmq6.cloudamqp.com"
# 2. Create a federation policy on downstream
curl -i -X PUT -H "Content-Type: application/json" $HTTPS_DOWNSTREAM/api/policies/$VHOST/fedit/ -d '{"pattern":"^myexchange$", "definition": {"federation-upstream-set":"all"}, "priority":0, "apply-to": "exchanges"}'
# Optional: 3. Create queue policy on downstream (matching all queues in the vhost!)
curl -i -X PUT -H "Content-Type: application/json" $HTTPS_DOWNSTREAM/api/policies/$VHOST/maxlen/ -d '{"pattern":".", "definition": {"max-length":10000}, "priority":0, "apply-to": "queues"}'
# 4. Create federation-upstream on downstream
curl -i -X PUT -H "Content-Type:application/json" -d '{"value":{"uri":"'$UPSTREAM_URL'","expires":36000000}}' $HTTPS_DOWNSTREAM/api/parameters/federation-upstream/$VHOST/upstream
# Create 1 queue on both sides
curl -i -X PUT -H "Content-Type: application/json" $HTTPS_DOWNSTREAM/api/queues/$VHOST/q1/ -d '{"auto_delete":false,"durable":true,"arguments":{}}'
curl -i -X PUT -H "Content-Type: application/json" $HTTPS_UPSTREAM/api/queues/$VHOST/q1/ -d '{"auto_delete":false,"durable":true,"arguments":{}}'
# Create 1 exchange on downstream
curl -i -X PUT -H "Content-Type: application/json" -d '{"type":"topic","auto_delete":false,"durable":true,"internal":false}' $HTTPS_DOWNSTREAM/api/exchanges/$VHOST/myexchange/
# Create bindings from exchange to queues
curl -i -XPOST -H 'Content-Type: application/json' $HTTPS_DOWNSTREAM/api/bindings/$VHOST/e/myexchange/q/q1 -d '{"routing_key":"#"}'
curl -i -XPOST -H 'Content-Type: application/json' $HTTPS_UPSTREAM/api/bindings/$VHOST/e/myexchange/q/q1 -d '{"routing_key":"#"}'
#Publish a message to upstream - will end up on both upstream and downstream
curl -i -X POST -H "Content-Type: text/plain" \
-d '{"properties":{},"routing_key":"mykey","payload":"my body","payload_encoding":"string"}' \
"$HTTPS_UPSTREAM/api/exchanges/$VHOST/myexchange/publish"
@camac2025
Copy link

Needed a few tweaks from me - I am working on the default vhost (could that be an issue?!) and not using TLS... I still don't see any traffic on the downstream altho I can see all the federation config in place successfully...

@camac2025
Copy link

For the record...

#!/bin/bash
# Minimum Viable Exchange Federation
# Upstream servers are the servers towards where messages are originally published
# Downstream servers are where the messages get forwarded to

# Two clusters
CREDS="guest:guest"
VHOST="%2F"
UPHOST="192.168.0.200"
DOWNHOST="192.168.0.200"
EXCHANGE="fedtest"
RKEY="fedq00"

UPSTREAM_URL="amqp://$CREDS@${UPHOST}:5672"
HTTPS_DNSTREAM="http://$CREDS@${DOWNHOST}:15673"
HTTPS_UPSTREAM="http://$CREDS@${UPHOST}:15672"

printf "\nCreate a federation policy on downstream\n---------------------------------------\n"
curl -i -X PUT -H "Content-Type: application/json" $HTTPS_DNSTREAM/api/policies/$VHOST/fedpolicy/ -d '{"pattern":"^fedtest$", "definition": {"federation-upstream-set":"all"}, "priority":0, "apply-to": "exchanges"}'

printf "\nCreate queue policy on downstream (matching all queues in the vhost!)\n---------------------------------------\n"
curl -i -X PUT -H "Content-Type: application/json" $HTTPS_DNSTREAM/api/policies/$VHOST/maxlen/ -d '{"pattern":".", "definition": {"max-length":10000}, "priority":0, "apply-to": "queues"}'

printf "\nCreate upstream on downstream\n---------------------------------------\n"
curl -i -X PUT -H "Content-Type:application/json" $HTTPS_DNSTREAM/api/parameters/federation-upstream/$VHOST/upstream -d '{"value":{"uri":"'$UPSTREAM_URL'","expires":36000000}}' 

printf "\ndownstream queue\n---------------------------------------\n"
curl -i -X PUT -H "Content-Type: application/json" $HTTPS_DNSTREAM/api/queues/$VHOST/fedq00/ -d '{"auto_delete":false,"durable":true,"arguments":{}}'
printf "\nupstream queue\n---------------------------------------\n"
curl -i -X PUT -H "Content-Type: application/json" $HTTPS_UPSTREAM/api/queues/$VHOST/fedq00/ -d '{"auto_delete":false,"durable":true,"arguments":{}}'

printf "\ndownstream exchange\n---------------------------------------\n"
curl -i -X PUT -H "Content-Type: application/json" $HTTPS_DNSTREAM/api/exchanges/$VHOST/${EXCHANGE}/ -d '{"type":"topic","auto_delete":false,"durable":true,"internal":false}'

printf "\ndownstream binding\n---------------------------------------\n"
curl -i -X POST -H 'Content-Type: application/json' $HTTPS_DNSTREAM/api/bindings/$VHOST/e/${EXCHANGE}/q/fedq00 -d '{"routing_key":"'${RKEY}'"}'
printf "\nupstream binding\n---------------------------------------\n"
curl -i -X POST -H 'Content-Type: application/json' $HTTPS_UPSTREAM/api/bindings/$VHOST/e/${EXCHANGE}/q/fedq00 -d '{"routing_key":"'${RKEY}'"}'

#Publish a message to upstream - will end up on both upstream and downstream
printf "\npublish\n---------------------------------------\n"
curl -i -X POST -H "Content-Type: text/plain" "$HTTPS_UPSTREAM/api/exchanges/$VHOST/${EXCHANGE}/publish" \
      -d '{"properties":{},"routing_key":"'${RKEY}'","payload":"my body","payload_encoding":"string"}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment