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

การผสาน RabbitMQ/Kafka

ตั้งค่า Spring WebSockets ให้ใช้ตัวกลางข้อความภายนอกสำหรับการสื่อสารระหว่างเซิร์ฟเวอร์

บทเรียน 2 จาก 411 ขั้นตอน

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

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

Scaling with External Brokers

When your WebSocket application runs on multiple server instances, an in-memory message broker isn't enough. You need an external message broker to coordinate messages across all instances.

This ensures that a message sent to one server can be received by a client connected to *any* server in your cluster, enabling true horizontal scaling.

Spring's Broker Bridge

Spring provides a powerful abstraction called the STOMP Broker Relay. This allows your Spring WebSocket application to delegate message routing to an external STOMP-compatible message broker.

Your application acts as a client to the external broker, sending and receiving messages on behalf of connected WebSocket clients.

RabbitMQ: A STOMP Broker

RabbitMQ is a popular open-source message broker that can be configured to act as a STOMP broker by enabling its STOMP plugin. This makes it an excellent choice for scaling Spring WebSocket applications.

  • Producers: Your Spring app sends messages to RabbitMQ.
  • Consumers: Your Spring app receives messages from RabbitMQ.
  • STOMP Plugin: Translates WebSocket STOMP frames to AMQP and vice-versa.

Add RabbitMQ Dependency

To enable Spring to communicate with RabbitMQ, you need to add the spring-boot-starter-websocket and spring-boot-starter-amqp dependencies to your project's build.gradle or pom.xml.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-websocket'
    implementation 'org.springframework.boot:spring-boot-starter-amqp'
    // ... other dependencies
}

Configure RabbitMQ Broker Relay

In your WebSocketConfig, use enableStompBrokerRelay() to tell Spring to use RabbitMQ's STOMP plugin as the message broker. Remember to set the correct host, port, and credentials.

Try running this full Spring Boot application:

package com.coddykit.websocket;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
        System.out.println("Spring Boot WebSocket app with RabbitMQ broker relay started.");
        System.out.println("Ensure RabbitMQ with STOMP plugin is running on localhost:61613.");
    }

    @Configuration
    @EnableWebSocketMessageBroker
    static class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

        @Override
        public void configureMessageBroker(MessageBrokerRegistry config) {
            config.enableStompBrokerRelay("/topic", "/queue")
                  .setRelayHost("localhost") // Or your RabbitMQ host
                  .setRelayPort(61613) // Default STOMP port for RabbitMQ plugin
                  .setClientLogin("guest")
                  .setClientPasscode("guest");
            config.setApplicationDestinationPrefixes("/app");
            config.setUserDestinationPrefix("/user");
        }

        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
            registry.addEndpoint("/ws").withSockJS();
        }
    }
}

Kafka for Inter-Server Sync

While RabbitMQ can act as a direct STOMP broker relay, Apache Kafka is typically used for different types of inter-server communication in a scalable WebSocket architecture.

Instead of Kafka being the direct STOMP broker, individual Spring WebSocket instances might use Kafka to broadcast internal events or messages to other instances in the cluster.

Kafka's Role in a Cluster

Imagine you have multiple WebSocket servers. When a message arrives at Server A, and the recipient is connected to Server B, Server A can publish the message to a Kafka topic.

Server B (and all other servers) can consume from this topic, identify the message for its client, and forward it. This makes Kafka an excellent backend for distributed message coordination.

Add Kafka Dependency

To enable Spring to interact with Kafka for backend messaging, you need the spring-kafka dependency.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-websocket'
    implementation 'org.springframework.kafka:spring-kafka'
    // ... other dependencies
}

Kafka Application Properties

When using Kafka for inter-server messaging, you'd configure Kafka broker details and consumer/producer properties in your application.properties file.

spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.consumer.group-id=websocket-cluster
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer

Broker Configuration Check

You're setting up a Spring Boot WebSocket application to use an external STOMP-compatible message broker to support multiple server instances. Which method in WebSocketMessageBrokerConfigurer is primarily used to configure Spring to connect to this external broker?

Recap: External Brokers

We've learned how external message brokers are crucial for scaling WebSocket applications. Spring's enableStompBrokerRelay() simplifies integrating with brokers like RabbitMQ (when its STOMP plugin is enabled).

For Kafka, while not a direct STOMP broker relay, it serves as a powerful backend for inter-server communication, allowing WebSocket instances to coordinate and distribute messages across a cluster, enabling robust scaling.

เริ่มต้นได้ฟรี

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

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

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

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

บทเรียน “การผสาน RabbitMQ/Kafka” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การผสาน RabbitMQ/Kafka”

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

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

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

บทเรียน “การผสาน RabbitMQ/Kafka” ใช้เวลานานแค่ไหน

บทเรียน 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