1. 程式人生 > 程式設計 >解決SpringBoot webSocket 資源無法載入、tomcat啟動報錯的問題

解決SpringBoot webSocket 資源無法載入、tomcat啟動報錯的問題

問題描述:

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了,是不是很方便,至於原理,這裡不解釋,篇幅太長,自己去百度吧

總結:

1. 最近做了一個簡單的IM功能,遇到了這些問題,作為記錄,防止哪天我忘了

2. 網上解決方法的可能有用,但是寫的不明不白,不友好,而且不見得好用,在解決問題的時候一定要考慮到通用性,開發一定要為了便利,不然坑了你也坑了被人,改這改那誰尼瑪記得住

3. 此博文為本人自身角度進行編寫,作為記錄。如對他人有幫助,可以點個贊,本人不回覆,不評論。

到此這篇關於解決SpringBoot webSocket 資源無法載入、tomcat啟動報錯的問題的文章就介紹到這了,更多相關SpringBoot webSocket 資源無法載入內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!