WebSockets & Real-Time Systems with Spring · 课时

粘性会话与 WebSocket 路由

配置负载均衡器,使用粘性会话正确路由有状态的 WebSocket 连接,并了解共享代理何时可以免除这一需求。

第 4 / 4 课13 个步骤

粘性会话与 WebSocket 路由 是 CoddyKit 上的免费 WebSockets & Real-Time Systems with Spring 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 WebSockets & Real-Time Systems with Spring 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 WebSockets & Real-Time Systems with Spring 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

WebSockets Are Long-Lived

Unlike a stateless HTTP request, a WebSocket is a persistent connection pinned to one server instance for its entire life. This breaks the assumption load balancers make about HTTP.

The Routing Problem

If the in-memory simple broker holds a user's subscriptions on instance A, a message that lands on instance B has nowhere to deliver. The connection state lives only where the socket terminated.

Sticky Sessions (Session Affinity)

Sticky sessions make the load balancer always route a given client to the same backend instance. This keeps the socket and its server-side state co-located.

Affinity by Cookie

Cloud load balancers commonly implement affinity with a cookie they set on the first response and read on later requests.

# AWS ALB target group attribute
stickiness.enabled = true
stickiness.type = lb_cookie
stickiness.lb_cookie.duration_seconds = 86400

Forwarding the Upgrade

The load balancer must support and forward the HTTP Upgrade handshake. Most modern L7 balancers (ALB, GCP HTTPS LB, Nginx) do, but it must be enabled.

# Nginx
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

Idle Timeouts Bite WebSockets

Load balancers close connections idle beyond a timeout (ALB default 60s). A mostly-quiet WebSocket gets dropped unless you send heartbeats or raise the idle timeout.

idle_timeout.timeout_seconds = 300

Health Checks and Draining

During a deploy, instances are removed. Enable connection draining so in-flight WebSockets finish (or clients reconnect) instead of being cut abruptly.

The Better Answer: Shared Broker

Sticky sessions are a workaround. A cleaner design uses a shared external broker (the STOMP relay): any instance can deliver to any subscriber, so affinity is no longer required for routing.

Combining Both

Even with a shared broker, sticky sessions can still help by keeping a client on one instance to avoid mid-stream reconnects. Use them as an optimization, not a correctness crutch.

Kubernetes Ingress Notes

On Kubernetes, the ingress controller needs WebSocket support and affinity annotations. Nginx ingress, for example, uses cookie-based affinity.

nginx.ingress.kubernetes.io/affinity: cookie
nginx.ingress.kubernetes.io/proxy-read-timeout: "300"

Scaling Out Safely

To add capacity, scale instances behind the balancer and rely on the shared broker for fan-out. Keep sessions evenly spread and use draining on scale-down.

Quick Check

Test your routing knowledge.

Recap

You routed WebSockets correctly:

  • WebSockets are long-lived and pinned to one instance
  • Sticky sessions (cookie affinity) keep a client on its instance
  • The balancer must forward the Upgrade header and tolerate idle WebSockets
  • Use connection draining on deploys
  • A shared broker removes the routing need; affinity then becomes an optimization
免费开始

用 AI 导师学习 WebSockets & Real-Time Systems with Spring — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「粘性会话与 WebSocket 路由」课时是免费的吗?

是的 — 「粘性会话与 WebSocket 路由」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebSockets & Real-Time Systems with Spring 课程的其余内容,请升级到 CoddyKit PRO。 WebSockets & Real-Time Systems with Spring 课程共包含 4 节课。

「粘性会话与 WebSocket 路由」这节课中我会学到什么?

配置负载均衡器,使用粘性会话正确路由有状态的 WebSocket 连接,并了解共享代理何时可以免除这一需求。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Real-Time Systems with Spring,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebSockets & Real-Time Systems with Spring 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebSockets & Real-Time Systems with Spring 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「粘性会话与 WebSocket 路由」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebSockets & Real-Time Systems with Spring 课中编写并运行代码吗?

能。每节 WebSockets & Real-Time Systems with Spring 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 微服务架构中的 WebSockets
  2. 云端部署策略(AWS/GCP)
  3. 负载均衡与高可用性
  4. 粘性会话与 WebSocket 路由
← 返回 WebSockets & Real-Time Systems with Spring