0Pricing
GraphQL APIs with Spring Boot · 课时

订阅的过滤与扩展

超越基础订阅:通过服务端过滤,只向每个客户端发送其关注的事件,并在多个 Spring Boot 实例之间扩展订阅能力。

订阅的过滤与扩展 是 CoddyKit 上的免费 GraphQL APIs with Spring Boot 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 GraphQL APIs with Spring Boot 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 GraphQL APIs with Spring Boot 课程共包含 4 节课。

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

Not Every Client Wants Everything

A naive subscription pushes every event to every subscriber. But a user watching order #42 does not care about order #99.

Filtering ensures each subscriber only receives the events relevant to them.

Subscription Arguments

Subscriptions can take arguments just like queries. A client passes the ID it cares about, and the server uses it to filter the stream.

type Subscription {
  orderUpdated(orderId: ID!): Order
}

Reactive Streams Recap

Spring for GraphQL represents a subscription as a Reactor Flux, an asynchronous stream of items. Filtering is just stream operations applied to that Flux.

Filtering with filter()

Apply .filter() to the publisher so only matching events flow to the subscriber.

@SubscriptionMapping
public Flux<Order> orderUpdated(@Argument String orderId) {
    return orderPublisher.flux()
        .filter(o -> o.getId().equals(orderId));
}

A Shared Event Sink

Use a Reactor Sinks.Many as a hub. Your service emits events into it, and every subscription builds a filtered view of its stream.

private final Sinks.Many<Order> sink =
    Sinks.many().multicast().onBackpressureBuffer();

Emitting Events

When your business logic changes an order, push it into the sink so all matching subscribers are notified.

public void updateOrder(Order order) {
    repository.save(order);
    sink.tryEmitNext(order);
}

The Scaling Problem

An in-memory sink only knows about events on its own JVM. With multiple Spring Boot instances behind a load balancer, an event emitted on instance A never reaches subscribers connected to instance B.

External Pub/Sub to the Rescue

Route events through an external broker like Redis Pub/Sub or Kafka. Every instance publishes to and subscribes from the broker, so all instances see all events.

Bridging Redis to a Flux

Subscribe to a Redis channel and feed incoming messages into the same Flux your GraphQL subscription exposes, unifying local and remote events.

redisTemplate.listenToChannel("orders")
    .map(msg -> deserialize(msg.getMessage()))
    .subscribe(sink::tryEmitNext);

Backpressure and Cleanup

Slow clients can fall behind. Use a bounded buffer or drop strategy, and ensure resources are released when a client disconnects so memory does not leak.

Sinks.many().multicast().onBackpressureBuffer(1024, false);

Best Practices

Keep subscriptions efficient:

  • Filter on the server, never push everything
  • Use an external broker to scale across instances
  • Handle backpressure for slow consumers
  • Clean up on disconnect to avoid leaks

Quick Check

Test your subscription scaling knowledge.

Recap

You scaled real-time subscriptions:

  • Filter streams with .filter() using subscription arguments
  • Use a Sinks.Many hub to broadcast events
  • Route through Redis or Kafka to scale across instances
  • Manage backpressure and clean up on disconnect

Filtered, distributed subscriptions deliver the right data at scale.

常见问题解答

「订阅的过滤与扩展」课时是免费的吗?

是的 — 「订阅的过滤与扩展」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 GraphQL APIs with Spring Boot 课程的其余内容,请升级到 CoddyKit PRO。 GraphQL APIs with Spring Boot 课程共包含 4 节课。

「订阅的过滤与扩展」这节课中我会学到什么?

超越基础订阅:通过服务端过滤,只向每个客户端发送其关注的事件,并在多个 Spring Boot 实例之间扩展订阅能力。 你通过在浏览器中直接运行的动手代码来练习 GraphQL APIs with Spring Boot,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 GraphQL APIs with Spring Boot 需要有经验吗?

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

「订阅的过滤与扩展」课时需要多长时间?

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

我能在这节 GraphQL APIs with Spring Boot 课中编写并运行代码吗?

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

此课程中的所有课时

  1. 理解 GraphQL 订阅
  2. 实现实时更新
  3. 将 WebSockets 集成到 Spring
  4. 订阅的过滤与扩展
← 返回 GraphQL APIs with Spring Boot