简体   繁体   中英

Spring websocket - sendToUser from a cluster does not work from backup server

We have a spring mvc with spring websocket configured ('spring-websocket', version:'4.1.0.RELEASE').

We have a simple class that executes the following code:

 @Autowired
private SimpMessagingTemplate template;


@Override
public void push(Long userId, Object message, WebsocketQueue queue) {

   String loginName = this.userRepository.getLoginName(userId);

   this.template.convertAndSendToUser(
                   loginName,
                   queue.getMapping(),
                   message);

}

This works great with a single tomcat (non clustered environment)

The problem we are facing is when working with a cluster of two tomcats.

If we try to push the message to a user from the actual tomcat he was logged in - it works.

But if we try to push the message from the other tomcat node - it does not reach the user.

1) What can we do?

2) Does changing the websocket queue names to the user login names instead of the default behavior can help us?

This can be solved with a feature added in Spring Framework 4.2 :

Resolve user destinations across cluster of servers

User registries can broadcast and share their content using a specific topic destination. You can set this up by configuring the StompBrokerRelay for a cluster deployment; see setUserDestinationBroadcast and setUserRegistryBroadcast .

Don't hesitate to send feedback / improvement requests on https://jira.spring.io !

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