Java Web 服务器无法启动。端口 8080 已被使用。弹簧靴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56499928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:16:46  来源:igfitidea点击:

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

javaspring-bootgradle

提问by Anonymous Creator

I am trying to call webAPI from gradle project.

我正在尝试从 gradle 项目调用 webAPI。

My build.gradle is as following.

我的 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'
}

If I remove following dependency

如果我删除以下依赖项

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

It works, but if I add it back. it gives error as

它有效,但如果我将其添加回来。它给出了错误

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

So, how do I fix this, so that I can use webclient? Because application is not web application which requires port to run. it is a sort of microservice.

那么,我该如何解决这个问题,以便我可以使用 webclient?因为应用程序不是需要端口才能运行的 Web 应用程序。它是一种微服务。

I just want to use WebClient of Spring Boot. How do i use it without converting my spplication into web application.

我只想使用 Spring Boot 的 WebClient。如何在不将我的应用程序转换为 Web 应用程序的情况下使用它。

采纳答案by Gustavo Passini

If you don't want the embedded server to start, just set the following property in you application.properties(or .yml):

如果您不想启动嵌入式服务器,只需在您application.properties(或.yml)中设置以下属性:

spring.main.web-application-type=none

If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it. To disable this behaviour configure the WebApplicationType in your application.properties

如果您的类路径包含启动 Web 服务器所需的位,Spring Boot 将自动启动它。要禁用此行为,请在 application.properties 中配置 WebApplicationType

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

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

回答by Dominik

You try to use an already used port.

您尝试使用已使用的端口。

Ports are used on the transport layer - tcp, httpis application layer and uses a transport layer to send and receive requests.

端口是传输层上使用- tcphttp是应用层,并使用传输层发送和接收请求。

Default port exposed by spring boot app is 8080. In your case you have two solutions:

spring boot 应用程序公开的默认端口是8080. 在您的情况下,您有两种解决方案:

  • change port for your application
  • stop the service that uses the port you want to use
  • 更改应用程序的端口
  • 停止使用您要使用的端口的服务

回答by Armen Arzumanyan

Your client application also spring boot application, whats why you have two spring boot application run in 8080 port. Change port one of them or create a standalone java application with main class, put your web client in it and run. As http client you can use Apache Http Client.

您的客户端应用程序也是 spring boot 应用程序,为什么有两个 spring boot 应用程序在 8080 端口上运行。更改其中一个端口或使用主类创建一个独立的 Java 应用程序,将您的 Web 客户端放入其中并运行。作为 http 客户端,您可以使用 Apache Http 客户端。

回答by Mitzy Valencia

Just customize your springboot app to start in any port: https://www.javadevjournal.com/spring-boot/change-the-default-port-in-spring-boot/

只需自定义您的 springboot 应用程序以在任何端口启动:https: //www.javadevjournal.com/spring-boot/change-the-default-port-in-spring-boot/

回答by jayesh

You can change the default port of your application in application.properties by adding the following line:

您可以通过添加以下行来更改 application.properties 中应用程序的默认端口:

server.port = 8090

服务器端口 = 8090

回答by entiy0

If on windows and your getting this every time you run the application you need to keep doing:

如果在 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.

If netstat above includes something like this;

如果上面的 netstat 包含这样的内容;

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

Then you can either wait for a little while or reconfigure to use another port.

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

I suppose we could write some code to randomly generate and check if a port is free when the application runs. Though this will have diminishing returns as they start to get used up. On the other hand could add a resource clean up code that does what we have above once the application stops.

我想我们可以编写一些代码来随机生成并检查应用程序运行时端口是否空闲。尽管随着它们开始用完,这将带来收益递减。另一方面,可以添加一个资源清理代码,一旦应用程序停止,它就会执行我们上面的操作。