首页 > 解决方案 > 如何解决 Spring boot 2.2.6 无法启动已使用端口的应用程序?

问题描述

我正在尝试启动一个 spring boot v2.2.6 web 应用程序,但它失败了:

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

有问题的端口,无论数量多少,始终是免费的,并且没有其他应用程序正在使用它,尽管 Spring 总是报告上述错误。更改端口号没有任何作用。我很想听听有关可能导致此类问题的任何意见。问题是如何解决这个问题。

配置:

application.yaml

spring:
  datasource:
    testWhileIdle: true
    validationQuery: SELECT 1
    hikari:
      connectionTimeout: 50000
      idleTimeout: 300000
      maxLifetime: 900000
      maximumPoolSize: 20
      minimumIdle: 3
      poolName: MyPool
      connectionTestQuery: select 1 from dual
      autoCommit: true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
    url: ${DB_URL}
  jpa:
    database-platform: org.hibernate.dialect.MySQL8Dialect
    properties:
      hibernate:
        dialect:
            storage_engine: innodb
        temp.use_jdbc_metadata_defaults: false
        default_schema: ${DB_SCHEMA}
    hibernate:
      ddl-auto: none
    database: mysql
  flyway:
    enabled: true
    user: ${MIGRATION_USER}
    password: ${MIGRATION_PASSWORD}
    schemas: ${DB_SCHEMA}
    placeholders:
      migration_user: ${SPS_MIGRATION_USER}
      app_user_name: ${SPS_DB_USERNAME}
      app_user_password: ${SPS_DB_PASSWORD}
      app_user_location: ${SPS_DB_USER_LOCATION}

security:
  jwt:
    access:
      token:
        expire-length: ${JWT_TOKEN_EXPIRATION}
        secret-key: ${JWT_SECRET_KEY}

springdoc:
  api-docs:
    path: /docs
  swagger-ui:
    path: /docs/ui
logging:
  level:
    root: info
server:
  address: "80"


鉴于应用程序的结构:(为简洁起见,省略了一些文件夹)

│   │   └── com
│   │   │       └── my
│   │   │           └── application
│   │   │               └── server
│   │   │                   ├── app 
│   │   │                   ├── common
│   │   │                   │   ├── config
│   │   │                   ├── firebase
│   │   │                   │   ├── config
│   │   │                   ├── security
│   │   │                   │   ├── config
│   │   │                   ├── sentry
│   │   │                   │   └── config
│   │   │                   ├── session
│   │   │                   │   ├── config

配置类是:

package com.my.app.server.app

@SpringBootApplication(scanBasePackages = "com.my.application.server.*")
@Slf4j
public class ServerApp {
    public static void main(String[] args) {
        SpringApplication.run(ServerApp.class, args);
    }
}

package com.my.app.server.app

@EnableAsync
@Configuration
public class AsyncConfiguration implements AsyncConfigurer {
.....
@Configuration
@AllArgsConstructor
@Slf4j
public class RestConfiguration implements WebMvcConfigurer {
@Configuration
public class CommonConfig {

package com.my.app.server.firebase.config;

@Configuration
@EnableConfigurationProperties(FirebaseProperties.class)
public class FirebaseConfig {

com.my.app.server.security.config

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Slf4j
public class SecurityConfig extends WebSecurityConfigurerAdapter {

package com.my.app.server.session.config;

@Configuration
@EnableConfigurationProperties({
        SessionProperties.class, SessionValidity.class
})
@Slf4j
public class SessionConfig {

标签: javaspring-boot

解决方案


我发现了问题。在我的 application.yaml 我有这一行

server:
  address: "80"

我对此事的猜测如下:地址需要是一个 IP,并且通过提供这个"80"字符串(我的错误,我实际上想要/需要server.port)我搞砸了 IP 限制机制,并且显示的异常可能是不正确的,或者更确切地说过于笼统,无法提供更好的信息。


推荐阅读