Configurare il broker relay STOMP
Sostituisca il simple broker in memoria con un broker relay STOMP, così Spring inoltra i messaggi a un broker reale come RabbitMQ per la scalabilità orizzontale.
Configurare il broker relay STOMP è una lezione WebSockets & Real-Time Systems with Spring gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento WebSockets & Real-Time Systems with Spring, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso WebSockets & Real-Time Systems with Spring include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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_stompApplication vs Broker Destinations
Keep the prefixes distinct:
/app→ routed to your@MessageMappingcontrollers/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
enableStompBrokerRelayforwards to RabbitMQ/ActiveMQ over STOMP- Enable the broker's STOMP plugin (port 61613)
- Keep
/appfor controllers,/topicand/queuefor the broker - The relay gives clustering, persistence, and durable subscriptions
Domande Frequenti
La lezione «Configurare il broker relay STOMP» è gratuita?
Sì — il testo completo di «Configurare il broker relay STOMP» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso WebSockets & Real-Time Systems with Spring, passa a CoddyKit PRO. Il corso WebSockets & Real-Time Systems with Spring include 4 lezioni in totale.
Cosa imparerò in «Configurare il broker relay STOMP»?
Sostituisca il simple broker in memoria con un broker relay STOMP, così Spring inoltra i messaggi a un broker reale come RabbitMQ per la scalabilità orizzontale. Eserciti WebSockets & Real-Time Systems with Spring con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare WebSockets & Real-Time Systems with Spring?
Non è richiesta alcuna esperienza precedente. WebSockets & Real-Time Systems with Spring su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Configurare il broker relay STOMP»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione WebSockets & Real-Time Systems with Spring?
Sì. Ogni lezione WebSockets & Real-Time Systems with Spring include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Necessità di broker di messaggi esterni
- Integrazione con RabbitMQ/Kafka
- Architetture WebSocket distribuite
- Configurare il broker relay STOMP