Web 服务器无法启动。端口 8080 已被使用。 Spring Boot 微服务

新手上路,请多包涵

我正在尝试从 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 应用程序的情况下使用它。

原文由 Anonymous Creator 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

如果您不想启动嵌入式服务器,只需在 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 文件中,as 命令启动时的行参数等)。

原文由 Gustavo Passini 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

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

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

原文由 Alan Carlyle 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题