如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。


当前回答

在文件应用程序。属性添加以下内容: server.port = 8888 这里需要经常提到的项目

其他回答

大多数情况下,springboot运行在端口:8080上,因为使用了嵌入式Tomcat。在某些情况下,它可能抛出一个已经在使用的错误端口8080。为了避免这种问题,我们可以配置服务器端口。

使用application.properties

添加server.port = 9898

在运行时配置

使用以下参数运行应用程序。

spring-boot:跑-Drun.jvmArguments = ' -Dserver.port = 8081 '

如果你打算在命令环境中以jar文件的形式运行应用程序,只需输入“SERVER_PORT=***”作为前缀。要执行的完整命令如下所示:

SERVER_PORT=8080 java -jar ***.jar

如果你想在Linux下后台运行应用程序,使用'nohup'命令将如下所示:

SERVER_PORT=8080 nohup java -jar ***.jar &

由于Spring Boot提供了各种配置外部化机制(通过各种PropertySource实现和/或按顺序连接到Environment对象的处理器),您可以通过以下方法设置jar存档之外的任何属性:

Pass property through command line argument as application argument java -jar <path/to/my/jar> --server.port=7788 From property in SPRING_APPLICATION_JSON (Spring Boot 1.3.0+) Define environment variable in U*IX shell: SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar> By using Java system property: java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar> Pass through command line argument: java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}' Define JVM system property java -Dserver.port=7788 -jar <path/to/my/jar> Define OS environment variable U*IX Shell SERVER_PORT=7788 java -jar <path/to/my/jar> Windows SET SERVER_PORT=7788 java -jar <path/to/my/jar> Place property in ./config/application.properties configuration file server.port=7788 and run: java -jar <path/to/my/jar> Place property in ./config/application.yaml server: port: 7788 and run: java -jar <path/to/my/jar> Place property in ./application.properties server.port=7788 and run: java -jar <path/to/my/jar> Place property in ./application.yaml server: port: 7788 and run: java -jar <path/to/my/jar>


您可以将上述方法组合在一起,列表中的前一个配置优先于后一个配置。

例如:

SERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788

服务器将在端口7788上启动并监听。

这非常有用,在PropertySources中提供优先级较低的默认属性(通常打包在存档中或编码在源代码中),然后在运行时环境中覆盖它。这就是Spring Boot的设计理念:

坚持己见,但当需求开始偏离默认值时,要迅速离开。


SERVER_NAME到server.name的转换由放松绑定完成。

如果您使用gradle作为构建工具,您可以在应用程序中设置服务器端口。Yml文件为:

server:
  port: 8291

如果使用maven,则可以在应用程序中设置端口。属性文件为:

server.port: 8291

将此添加到应用程序中。属性文件

server.port= 8080