WebSockets & Real-Time Systems with Spring · บทเรียน

การกำหนดค่ารีเลย์นายหน้าของ STOMP

แทนที่นายหน้าแบบง่ายในหน่วยความจำด้วยรีเลย์นายหน้าของ STOMP เพื่อให้ Spring ส่งต่อข้อความไปยังนายหน้าจริงอย่าง RabbitMQ สำหรับการขยายระบบแนวนอน

บทเรียน 4 จาก 413 ขั้นตอน

การกำหนดค่ารีเลย์นายหน้าของ STOMP เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Real-Time Systems with Spring และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Limits of the Simple Broker

The default enableSimpleBroker keeps subscriptions and message routing in memory on one JVM. It is great for demos but cannot share state across instances, so it does not scale horizontally.

Enter the Broker Relay

A STOMP broker relay tells Spring to forward messages to a dedicated broker (RabbitMQ, ActiveMQ) that speaks STOMP. The broker, not the JVM, now handles routing and fan-out.

Why This Scales

With a central broker, every Spring instance connects to the same broker. A message published on node A reaches a subscriber on node B because both share the broker's topics and queues.

Enabling the Relay

Swap enableSimpleBroker for enableStompBrokerRelay in your config, pointing at the broker host and port.

registry.enableStompBrokerRelay("/topic", "/queue")
  .setRelayHost("rabbitmq")
  .setRelayPort(61613)
  .setClientLogin("guest")
  .setClientPasscode("guest");

Enabling STOMP on RabbitMQ

RabbitMQ speaks STOMP through a plugin you must enable. Port 61613 is the STOMP default.

rabbitmq-plugins enable rabbitmq_stomp

Application vs Broker Destinations

Keep the prefixes distinct:

  • /app → routed to your @MessageMapping controllers
  • /topic, /queue → routed straight to the broker relay
registry.setApplicationDestinationPrefixes("/app");

System Connection

Spring keeps a separate system connection to the broker for server-initiated messages (e.g. convertAndSend from a service). Configure its credentials too.

relay.setSystemLogin("guest")
     .setSystemPasscode("guest")
     .setSystemHeartbeatSendInterval(10000);

Heartbeats with the Relay

The relay maintains heartbeats with the broker to detect a dead broker connection. If the broker goes down, Spring attempts to reconnect automatically.

What the Broker Brings

A real broker adds capabilities the simple broker lacks:

  • Durable subscriptions that survive restarts
  • Message persistence to disk
  • Cross-node fan-out for clustering
  • Monitoring and management UIs

Reactive/Virtual Thread Note

The relay uses a Reactor Netty TCP client under the hood. Ensure the reactor-netty dependency is present, or the relay fails to start with a missing-class error.

<dependency>
  <groupId>io.projectreactor.netty</groupId>
  <artifactId>reactor-netty</artifactId>
</dependency>

Graceful Degradation

If the broker is unreachable at startup, the relay keeps retrying. Design clients to handle temporary delivery gaps and reconnect, rather than assuming the broker is always up.

Quick Check

Test your relay knowledge.

Recap

You moved routing to a real broker:

  • The simple broker is in-memory and single-node only
  • enableStompBrokerRelay forwards to RabbitMQ/ActiveMQ over STOMP
  • Enable the broker's STOMP plugin (port 61613)
  • Keep /app for controllers, /topic and /queue for the broker
  • The relay gives clustering, persistence, and durable subscriptions
เริ่มต้นได้ฟรี

เรียนรู้ WebSockets & Real-Time Systems with Spring ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การกำหนดค่ารีเลย์นายหน้าของ STOMP” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การกำหนดค่ารีเลย์นายหน้าของ STOMP” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดค่ารีเลย์นายหน้าของ STOMP”

แทนที่นายหน้าแบบง่ายในหน่วยความจำด้วยรีเลย์นายหน้าของ STOMP เพื่อให้ Spring ส่งต่อข้อความไปยังนายหน้าจริงอย่าง RabbitMQ สำหรับการขยายระบบแนวนอน คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การกำหนดค่ารีเลย์นายหน้าของ STOMP” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Real-Time Systems with Spring นี้ได้ไหม

ได้ บทเรียน WebSockets & Real-Time Systems with Spring ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ความจำเป็นของตัวกลางข้อความภายนอก
  2. การผสาน RabbitMQ/Kafka
  3. สถาปัตยกรรม WebSocket แบบกระจาย
  4. การกำหนดค่ารีเลย์นายหน้าของ STOMP
← กลับไปที่ WebSockets & Real-Time Systems with Spring