簡體   English   中英

Web 服務器啟動失敗。 端口 8080 已被使用。 Spring 啟動微服務

[英]Web server failed to start. Port 8080 was already in use. Spring Boot microservice

我正在嘗試從 gradle 項目調用 webAPI。

我的 build.gradle 如下。

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compile 'org.springframework.boot:spring-boot-starter-webflux'
    compile 'org.projectreactor:reactor-spring:1.0.1.RELEASE'
}

如果我刪除以下依賴項

compile 'org.springframework.boot:spring-boot-starter-webflux'

它有效,但如果我把它加回去。 它給出了錯誤

Web server failed to start. Port 8080 was already in use.

那么,我該如何解決這個問題,以便我可以使用 webclient? 因為應用程序不是 web 需要端口才能運行的應用程序。 它是一種微服務。

我只想使用 Spring Boot 的 WebClient。 我如何在不將我的應用程序轉換為 web 應用程序的情況下使用它。

如果在 Windows 上並且每次運行應用程序時都得到這個,則需要繼續執行以下操作:

> netstat -ano | findstr *<port used>*

  TCP    0.0.0.0:*<port used>*  0.0.0.0:0              LISTENING       *<pid>*
  TCP    [::]:*<port used>*     [::]:0                 LISTENING       *<pid>*

> taskkill /F /PID *<pid>*
SUCCESS: The process with PID *<pid>* has been terminated.

如果上面的 netstat 包含這樣的內容;

TCP    [zzzz:e2ce:44xx:1:axx6:dxxf:xxx:xxxx]:540yy [zzzz:e2ce:44xx:1:axx6:dxxf:xxx:xxxx]:*<port used>* TIME_WAIT 0

然后您可以稍等片刻或重新配置以使用另一個端口。

我想我們可以編寫一些代碼來隨機生成並檢查應用程序運行時端口是否空閑。 盡管隨着它們開始用完,這將帶來收益遞減。 另一方面,可以添加一個資源清理代碼,一旦應用程序停止,它就會執行我們上面的操作。

您可以通過添加以下行來更改 application.properties 中應用程序的默認端口:

服務器端口 = 8090

如果您不想啟動嵌入式服務器,只需在application.properties (或.yml )中設置以下屬性:

spring.main.web-application-type=none

如果您的類路徑包含啟動 Web 服務器所需的位,Spring Boot 將自動啟動它。 要禁用此行為,請在 application.properties 中配置 WebApplicationType

來源: https : //docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html


如果您的應用程序確實一個 Web 應用程序,那么您可以使用server.port屬性輕松更改端口(在應用程序的.properties / .yaml文件中,作為啟動時的命令行參數等)。

您可以使用 npm 殺死端口

**npx kill-port 8080**   //8080 for example

要求:npm

閱讀更多: https : //www.npmjs.com/package/kill-port

  1. src/main 中創建/resources文件夾
  2. /resources 中創建application.properties文件
  3. write server.port=9090 //(使用您選擇的任何端口號)

您可以在 application.properties 文件中設置server.port= #some-available-port number

或在管理員模式下運行命令提示符並運行netstat -a -o -n 查找使用端口 8080 的進程 ID。

運行taskkill /F /PID #Processid命令

如果端口:8080 已在使用中發生錯誤:

  • goto command prompt .type command> .netstat -ano .Enter->這將顯示所有正在運行的端口檢查端口 8080 .type command> taskkill /F /PID 8080 .process 將終止。

以管理員身份打開命令提示符

第一步:netstat -ano | findstr:<輸入您的 4 位端口號>

netstat -ano | findstr :8080

TCP 0.0.0.0:8080 0.0.0.0:0 監聽 6436

TCP [::]:8080 [::]:0 監聽 6436

step2: taskkill /PID <enter above pid number(有時顯示3/4/5/6位)> /F

taskkill /PID 6436 /F

成功:PID 為 6436 的進程已終止。

您嘗試使用已使用的端口。

端口用於傳輸層 - tcphttp是應用層並使用傳輸層來發送和接收請求。

spring boot 應用程序公開的默認端口是8080 在您的情況下,您有兩種解決方案:

  • 更改應用程序的端口
  • 停止使用您要使用的端口的服務

只需自定義您的 springboot 應用程序以在任何端口啟動: https : //www.javadevjournal.com/spring-boot/change-the-default-port-in-spring-boot/

很容易我們有兩種方法可以解決。 第一個是更改application.properties 中的端口號,即

server.port=9999 //  something like this...

其次是,首先停止可用的正在運行的服務器,然后再次重新運行您的服務器。

我確定它有效:)

如果您之前已經啟動了 spring boot 應用程序並且在再次點擊播放之前忘記停止,那么轉到 windows任務管理器並找到 java 應用程序(類似於“OpenJDK 平台二進制文件”並單擊結束任務。(Java 應用程序不是 eclipse)。然后再次嘗試運行。它對我有用。

您的客戶端應用程序也是 spring boot 應用程序,為什么有兩個 spring boot 應用程序在 8080 端口上運行。 更改其中一個端口或使用主類創建一個獨立的 Java 應用程序,將您的 Web 客戶端放入其中並運行。 作為 http 客戶端,您可以使用 Apache Http 客戶端。

今天我在 Spring Boot 項目中工作,在我的項目中遇到了同樣的錯誤。 所以,我所做的,我點擊了運行最后一個工具按鈕旁邊的停止按鈕並再次運行。 然后,我的項目開始工作得很好。

你也可以在windows的搜索欄中寫services.msc,然后你就可以找到Apache Tomcat,然后停止這個apache。 我認為它會起作用。 找到 Apache TomCat 並停止它

有時間如果可以手動kill the that port問題就可以解決了。

使用CurrPorts軟件,您可以查看機器中正在運行的所有端口,如果需要,您可以終止該端口。

您可以從這里下載 CurrPorts。 (下載鏈接在頁面底部)

在此處輸入圖片說明

============== 或 ==============

如果沒有 CurrPorts,您也可以使用如下方法執行此操作。

  1. CMD以管理員身份運行
  2. 輸入netstat -a -o -n並按回車鍵。 現在你可以看到如下所示。 端口可以​​在:符號后看到Local Address列。

在此處輸入圖片說明

  1. 選擇您的端口運行的進程 ID (not port)並鍵入taskkill /F /PID <process_id_here>命令並按回車鍵。

在此處輸入圖片說明

捕獲帶有消息的 java.net.BindException e:地址已在使用中並在其他可用端口上啟動並使用 webclient 和 2 個端口之一。

try {
            SpringApplication.run(Application.class, args);
        } catch (org.springframework.boot.web.server.PortInUseException e) {
//Runtime.exec("pkil")..
//or
SpringApplication.run(Application.class, otherargs);
//SpringApplication.run(Application.class, new String[]{"--server.port=8444"});
//when invoked recursively it is a port rebalancer for port usage among port pool with server as from client for startup stage via application restarts within many busy ports which are used before or without querying.

}

另一種方法是首先檢查哪些進程正在使用該特定端口,然后使用其進程 ID 殺死它:

  1. 運行lsof -i :8080這將識別哪個進程正在偵聽端口 8080。

  2. 記下進程 ID (PID),例如 63262

  3. 運行kill -9 <PID>例如kill -9 63262

這是一個簡單的答案。如果您收到此錯誤,請轉到您的項目

src/main/resources 並打開 application.properties 文件並在那里提及

server.port=8045 你可以在這里給出你自己的號碼而不是 8045

謝謝在此處輸入圖片說明

重置端口 8080。

在此處重置圖像描述

該錯誤基本上意味着您的端口 8080 被占用。 如果您收到此錯誤,請轉到您的項目並打開 application.properties 並添加以下行,它應該可以正常工作:

服務器端口 = 8090

  1. 打開 cmd 並輸入“netstat -ano -p tcp”。
  2. 然后,查找端口號和 PID。
  3. 打開資源監視器並搜索 PID 號
  4. 右鍵單擊並“結束進程”。

重新啟動我的機器解決了這個問題,盡管我遇到了以下問題:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8081 is already in use

如果您一次又一次地收到此錯誤,請確保 server.port=(your port no) 是 application.properties 文件中的第一行。

對此的最佳答案是始終使用“重新啟動應用程序”按鈕。 這將停止 Web 服務器並在與以前相同的端口上重新啟動整個應用程序。

它是紅色方塊和綠色播放圖標組合在一起的按鈕。

https://i.stack.imgur.com/ICGtX.png

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM