简体   繁体   中英

Clustering Websockets Tyrus Java

I implemented a cluster on Payara Server with 3 Nodes (A, B & C) as Standalone Instances. I use HAproxy as load balancer and everything works fine.

I added WebSockets in my project and HAProxy switches automatically from HTTP to tunnel mode when the client request a websocket.

My problem is this : I need to retrieve websocket session regardless of connected node. For example: "I have 3 users connected to application. User 1 and User 2 are connected to the Node A. User 3 is connected to the Node B. An event occurs on the Node C that requires user 1 and 3 to be notified. But I haven't on the Node C User 1 and User 2 sessions."

I tried to use a Hazelcast distributed List to store sessions at the User connection (OnOpen method). But when I called "add" method on the list, this throws a HazelcastSerializationException because Tyrus Session Object isn't Serializable.

How can I resolve my problem?

Thanks

Please have a look at the StreamSerializer if you don't want-to/can't add any serialization logic to your classes.

http://docs.hazelcast.org/docs/3.7/manual/html-single/index.html#custom-serialization

If you can add serialization logic, please have a look at the IdentifiedDataSerializable:

http://docs.hazelcast.org/docs/3.7/manual/html-single/index.html#identifieddataserializable

There are full examples in the reference manual.

You need to make sure that the object can really be serialized. Some objects should just never be serialized.

I doubt that transferring WebSocket sessions would help you, as you need to notify the users from the node they are connected to.

You probably want to notify nodes A and B from the node C in order to update the users connected to them. To do that, you could use Hazelcast Topic to send a message to all nodes in the cluster and retrieve the message by the node that has the WebSocket session ( edited ). You need to get the information from node C to the other nodes that have the session, not the other way around. Passing session to another node would be of no use, as the session is applicable only to an open WebSocket connection.

(edited) Before I suggested to use Hazelcast executor service instead of the Topic, but that causes classloader issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM