Red envelope rain feature implementation description
Demand background

Support marketing activities, attract new, improve brand influence; Red envelopes are flowers or physical prizes, which can be exchanged with offline operation colleagues after the event.

Refer to the implementation scheme of mature technology:

  • Based on theNetty NIOThe implementation supports more concurrent connections with fewer resources.
  • Based on theWebsocketRealize duplex communication, the client can request, the server can push data in reverse.
Implementation logic

1. After the operation management console has configured the data, persist the activity data and red envelope data to MySQL

2. Initialize data and scheduled tasks

  • Initialization interface: Cache red envelope data to Redis, Hash structure, Key is active ID, Value Hash structure Key: red envelope object ID Value: serialized red envelope object.
  • Update and delete interfaces:RedisIn the red envelope data.
  • Set up theQuartzTiming task

Third, red envelope processing logic

  • Query red envelope rain activity: the user enters the scene and finds the red envelope emitter connected to the clientnetty+ webSocketImmediately invoke the interface. Query whether the current scenario is active.
  • Countdown of red envelope Rain activity: the user will register into the red envelope rain scene after inquiring the red envelope rain activity.
  • Red Envelope rain campaign Red envelope message: When the red envelope rain campaign starts, the server will push the red envelope rain message to the end once per second.
  • End of Red Envelope Rain: After the end of red envelope Rain, the server will push a message to the end of red envelope Rain.
  • Grab a red envelope:set ex nxAdd distributed lock to grab red packets, the lock isHashStructural cacheRedis
  • Push the message that the red envelope is robbed: when other users grab the red envelope at the same time, the server pushes the message that some red envelopes have been robbed to the user. Business Services (Netty, Websocket) toKafkaSend messages while each business service subscribesKafka.consumer group idSet unique for broadcast effect, after consuming the message to the client connected to the service push “red envelope robbed message”, the client receives the message to be robbed red envelope visual effect (explosion, open, etc.).

Four, Netty Websocket

  1. The client establishes a long connection by shaking hands with any Netty node, and the node adds it to the long connection queue for memory maintenance. The client periodically sends heartbeat messages to the server. If the client does not receive heartbeat messages within the specified time, the server considers that the long connection between the client and the server is disconnected, and the server closes the connection to clear the sessions in the memory.
  2. NettyNode throughFeignInvoke the business interface to process the logic, return the response data, and then send the message as a producerKafka.
  3. The gateway acts as a consumer and consumes messages in broadcast mode, which are received by all nodes.
  4. After receiving the message, the node determines whether the message target is in the long connection queue maintained in its own memory. If there is, the node pushes data through the long connection. Otherwise, the data is ignored.
  5. Gateways form a cluster in multi-node mode. Each node is responsible for some long connections to achieve load balancing. When a large number of connections are connected, gateways can be added to share the load to achieve horizontal expansion. In addition, when a node is down, the client tries to establish long-term connections with other nodes to ensure overall service availability.