Add gateway-mvc: Spring Cloud Gateway Server MVC 5.1 on Boot 4.2.0 + virtual threads, retry, Redis rate limiting
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b3W3uHbHAmM3orhn7b
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
7211b0fb80
commit
32086c1f5b
@@ -0,0 +1,90 @@
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* A deliberately boring backend for the gateway to proxy to. No Spring, no dependencies:
|
||||
* java scripts/Upstream.java [port]
|
||||
* Endpoints: /hello, /limited, /slow?ms=500, /flaky?failFirst=2, /flaky/reset, /headers
|
||||
*/
|
||||
public class Upstream {
|
||||
|
||||
static final AtomicInteger flakyCalls = new AtomicInteger();
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
int port = args.length > 0 ? Integer.parseInt(args[0]) : 9091;
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
|
||||
server.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
|
||||
|
||||
server.createContext("/hello", ex ->
|
||||
send(ex, 200, "{\"service\":\"upstream\",\"message\":\"hello from the backend\"}"));
|
||||
|
||||
server.createContext("/limited", ex ->
|
||||
send(ex, 200, "{\"service\":\"upstream\",\"message\":\"you got through the rate limiter\"}"));
|
||||
|
||||
server.createContext("/slow", ex -> {
|
||||
long ms = Long.parseLong(param(ex, "ms", "500"));
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
send(ex, 200, "{\"slept\":" + ms + "}");
|
||||
});
|
||||
|
||||
server.createContext("/flaky/reset", ex -> {
|
||||
flakyCalls.set(0);
|
||||
send(ex, 200, "{\"reset\":true}");
|
||||
});
|
||||
|
||||
server.createContext("/flaky", ex -> {
|
||||
int failFirst = Integer.parseInt(param(ex, "failFirst", "2"));
|
||||
int n = flakyCalls.incrementAndGet();
|
||||
if (n <= failFirst) {
|
||||
send(ex, 503, "{\"call\":" + n + ",\"result\":\"unavailable\"}");
|
||||
} else {
|
||||
send(ex, 200, "{\"call\":" + n + ",\"result\":\"ok\"}");
|
||||
}
|
||||
});
|
||||
|
||||
server.createContext("/headers", ex -> {
|
||||
Map<String, String> sorted = new TreeMap<>();
|
||||
ex.getRequestHeaders().forEach((k, v) -> sorted.put(k.toLowerCase(), String.join(",", v)));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sorted.forEach((k, v) -> sb.append(k).append(": ").append(v).append('\n'));
|
||||
ex.getResponseHeaders().set("Content-Type", "text/plain");
|
||||
byte[] b = sb.toString().getBytes(StandardCharsets.UTF_8);
|
||||
ex.sendResponseHeaders(200, b.length);
|
||||
ex.getResponseBody().write(b);
|
||||
ex.close();
|
||||
});
|
||||
|
||||
server.start();
|
||||
System.out.println("upstream listening on " + port);
|
||||
}
|
||||
|
||||
static String param(HttpExchange ex, String name, String def) {
|
||||
String q = ex.getRequestURI().getQuery();
|
||||
if (q == null) return def;
|
||||
for (String kv : q.split("&")) {
|
||||
String[] p = kv.split("=", 2);
|
||||
if (p[0].equals(name) && p.length == 2) return p[1];
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
static void send(HttpExchange ex, int status, String body) throws IOException {
|
||||
byte[] b = body.getBytes(StandardCharsets.UTF_8);
|
||||
ex.getResponseHeaders().set("Content-Type", "application/json");
|
||||
ex.sendResponseHeaders(status, b.length);
|
||||
ex.getResponseBody().write(b);
|
||||
ex.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user