这篇文章将为大家详细讲解有关SpringBoot中无法加载webSocket 资源如何解决,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1. 项目集成WebSocket,且打包发布tomcat时出现websocket is already in CLOSING or CLOSE state这样的问题,建议参考“解决方法二”,但是“解决方法一”请要了解查看 ,因为解决方法二是在一的基础上进行更正
2. 如果出现javax.websocket.server.ServerContainer not available这样的错误,请参考“解决方法一”中步骤3
步骤1:在BootApplication中修改:
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(BootApplication.class);
springApplication.addListeners(new ApplicationPidFileWriter());
ConfigurableApplicationContext applicationContext = springApplication.run(args);
WebSocketServer.setApplicationContext(applicationContext);
注:这里的WebSocketServer是指你自定义的websocket接受消息的服务类
步骤2:修改WebSocketServer
private static ManageUserMapper manageUserMapper;
public static void setApplicationContext(ConfigurableApplicationContext applicationContext) {
WebSocketServer.manageUserMapper = applicationContext.getBean(ManageUserMapper.class);
}
步骤3: 修改pom.xml
由于我们在开发过程中,如果按照以上步骤1、2进行修改,一般不会出现问题,
但是如果我们打包发布tomcat,就会出现:javax.websocket.server.ServerContainer not available这样的错误,步骤3为常规解决一下问题方式
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 去除内嵌tomcat -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
需要去除web-starter的tomcat,因为如果不去除会导致冲突,
如果出现这种问题,你还需要在websocketConfig中做如下修改:(websocket为自定义配置类)
/**
* 服务器节点
*
* 如果使用独立的servlet容器,而不是直接使用springboot的内置容器,就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理
* @return
*/
@Bean
@Profile("dev")
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
在application.properties做如下配置
##prod:生产环境 dev:开发环境(WINDOWS)
spring.profiles.active=dev
这里加入了@Profile("dev") 这个注解。意思是在开发的过程中去调用
.记住如果开发过程中,记得一定要把pom.xml中的去除tomcat那句话给注释掉,上线才需要去除
问题反思:(为什么不建议这么解决问题)
这种方式确实可以常规解决websocket打包tomcat不报错问题,同时也解决了在资源无法加载的问题,但是这样却十分的麻烦,理由如下:
1. 繁琐:生产环境和开发环境要一直切换是否注释tomcat
2. 局限性大:我们在BootApplication中添加了websocketserver去访问资源的语句,但是其他地方难道没有需要调用的吗,如果有,难道像这样一直累计添加下去,每个server设置一下
核心思想是资源无法加载的问题,如果你有写线程,且线程你也调用了数据库资源,那么解决方法是一致,编写一个公用的工具类解决
步骤1:新建一个工具类,getbeanTool
此工具类为之后加载资源的工具类,公用
@Component
public class GetBeanTool implements ApplicationContextAware{
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
GetBeanTool.applicationContext = applicationContext;
}
public static Object getBeanByName(String beanName) {
if (applicationContext == null){
return null;
}
return applicationContext.getBean(beanName);
}
public static <T> T getBean(Class<T> type) {
return applicationContext.getBean(type);
}
}
步骤2:在线程、websocket等server中,加载资源
以WebSocketServer作为参考,如下修改:
private ManageUserMapper manageUserMapper;
public WebSocketServer() {
this.manageUserMapper=GetBeanTool.getBean(ManageUserMapper.class);
}
这么做你就不用在BootApplication中添加语句,来让你的server去调用资源了,一步到位,大家都能用,需要的时候直接通过getbeanTool去请求就行了
步骤3:解决tomcat开发环境,生产环境pom.xml中注释和打开内置tomcat的问题
这里需要修改BootApplication,如下修改:
@SpringBootApplication
@Configuration
public class BootApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(BootApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}
可以看到这里我继承了SpringBootServletInitializer ,同时重写了configure方法。采用这种写法,如论是开发还是测试,你都不用管是否需要注释内置的tomcat了
关于SpringBoot中无法加载webSocket 资源如何解决就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。