Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4HkWoC7hLzk61vZa8
53 lines
3.5 KiB
Bash
Executable File
53 lines
3.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Steps 4 and 5 on a running shop: change a value in the config repository and watch what refreshes.
|
|
# $1 = directory produced by `upgrade.sh 4` (or 5) and built. $2 = output file name inside output/
|
|
MOD=$(cd "$(dirname "$0")/.." && pwd)
|
|
cd "$1" || exit 1
|
|
unset JAVA_TOOL_OPTIONS
|
|
F=$MOD/output/$2
|
|
SECRET=demo-secret
|
|
rm -rf run; mkdir -p run/logs; cp -r config-server/src/main/resources/config run/config
|
|
trap 'scripts/stop.sh >/dev/null' EXIT
|
|
start() { # service port extra...
|
|
local name=$1 port=$2; shift 2
|
|
nohup java -Xmx200m -XX:+UseSerialGC -jar "$name/target/$name-1.0.0.jar" "$@" > "run/logs/$name.log" 2>&1 &
|
|
echo $! > "run/$name.pid"
|
|
for _ in $(seq 1 90); do curl -fs "localhost:$port/actuator/health" >/dev/null 2>&1 && return; sleep 1; done
|
|
echo "$name did not come up" >&2; tail -30 "run/logs/$name.log" >&2; exit 1
|
|
}
|
|
start config-server 8888 "--spring.cloud.config.server.native.search-locations=file:$PWD/run/config/" \
|
|
"--spring.cloud.config.server.monitor.github.webhook-secret=$SECRET"
|
|
start discovery-server 8761
|
|
start product-service 8081
|
|
start inventory-service 8083
|
|
start order-service 8082
|
|
for _ in $(seq 1 60); do
|
|
curl -s -H 'Accept: application/json' localhost:8761/eureka/apps | grep -q 'order-service:8082' && break; sleep 2
|
|
done
|
|
sleep 35 # the config server's Eureka client fetches the registry every 30 s
|
|
show() { echo "\$ curl localhost:$1$2"; curl -s "localhost:$1$2"; echo; }
|
|
{
|
|
echo "# order-service reads shop.order.* from the config server. $(grep -qE '^record' order-service/src/main/java/com/ankurm/shop/order/OrderProperties.java && echo 'OrderProperties is a Java record.' || echo 'OrderProperties is a class with setters.')"
|
|
echo; echo "--- before"; show 8082 /orders/config
|
|
echo; echo "--- an unsigned request to /monitor (the curl most tutorials show)"
|
|
echo "\$ curl -X POST localhost:8888/monitor -d path=order-service"
|
|
curl -s -o /dev/null -w 'HTTP %{http_code}\n' -X POST localhost:8888/monitor -d path=order-service
|
|
echo; echo "--- edit order-service.yml in the config repository: currency EUR -> USD, max-quantity 10 -> 3"
|
|
printf 'shop:\n order:\n currency: USD\n max-quantity: 3\n' > run/config/order-service.yml
|
|
sleep 12
|
|
echo "(no request sent: with a local file: repository the monitor module watches the directory itself)"
|
|
echo; echo "--- config server log"
|
|
grep -hE 'Monitoring for local|Refresh for: order-service|Failed to notify' run/logs/config-server.log | sed -E 's/^[^ ]+ +([A-Z]+) [0-9]+ --- (\[[^]]*\] )+/\1 /' | sed "s#$PWD/##" | head -4
|
|
echo; echo "--- order-service log"
|
|
grep -hE 'Refreshed keys' run/logs/order-service.log | sed -E 's/^[^ ]+ +([A-Z]+) [0-9]+ --- (\[[^]]*\] )+/\1 /' | tail -1
|
|
echo; echo "--- what order-service uses after the refresh"; show 8082 /orders/config
|
|
echo; echo "--- a signed GitHub-style push webhook, the way a remote Git repository would call"
|
|
BODY='{"commits":[{"modified":["order-service.yml"]}]}'
|
|
SIG=$(printf '%s' "$BODY" | openssl dgst -sha256 -hmac "$SECRET" | sed 's/^.* //')
|
|
echo "\$ curl -X POST localhost:8888/monitor -H 'X-GitHub-Event: push' -H 'X-Hub-Signature-256: sha256=<hmac>' -d '$BODY'"
|
|
curl -s -w '\nHTTP %{http_code}\n' -X POST localhost:8888/monitor -H 'Content-Type: application/json' -H 'X-GitHub-Event: push' -H "X-Hub-Signature-256: sha256=$SIG" -d "$BODY"
|
|
echo; echo "--- same webhook, wrong signature"
|
|
curl -s -o /dev/null -w 'HTTP %{http_code}\n' -X POST localhost:8888/monitor -H 'Content-Type: application/json' -H 'X-GitHub-Event: push' -H "X-Hub-Signature-256: sha256=0000" -d "$BODY"
|
|
} > "$F"
|
|
cat "$F"
|