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

谁说我不能喝 提交于 2019-12-11 13:40:48

问题


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?


回答1:


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 !



来源:https://stackoverflow.com/questions/32861645/spring-websocket-sendtouser-from-a-cluster-does-not-work-from-backup-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!