我有一个使用带有 SockJS 的 Websockets 的 Spring Boot 实时应用程序。目前我正在使用 LoadBalancer 和两个实例扩展我的应用程序。由于应用程序中的 cron 作业很少,我需要在两台服务器之间同步它,以便一次只运行一项任务。为此,我使用了 shedlock-spring 和 shedlock-provider-hazelcast 依赖项(我也在应用程序中使用 Hazelcast)。
如果我@EnableSchedulerLock(defaultLockAtMostFor = "PT1S")
向应用程序添加注释MainApplication.class
由于以下错误而无法启动:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stompWebSocketHandlerMapping' defined in class path resource [org/springframework/web/socket/config/annotation/DelegatingWebSocketMessageBrokerConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'stompWebSocketHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException:
@Bean method AbstractMessageBrokerConfiguration.messageBrokerTaskScheduler called as a bean reference for type [org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] but overridden by non-compatible bean instance of type [com.sun.proxy.$Proxy136].
Overriding bean of same name declared in: class path resource [org/springframework/web/socket/config/annotation/DelegatingWebSocketMessageBrokerConfiguration.class]
@SchedulerLock
注释工作正常,因为我在单独的应用程序中对其进行了测试,但它似乎与 Websockets 冲突并覆盖了一些 bean。我是 Websockets 配置的新手,所以如果有人知道此错误的原因以及如何修复它,请提供帮助。
这是我完美运行的 Websocket 配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app")
.enableSimpleBroker("/notification");
}
}