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


当前回答

在应用程序中。属性文件,添加这一行:

server.port = 65535

该把照片放在哪里?

24.3应用程序属性文件 SpringApplication从应用程序加载属性。属性文件 并将它们添加到Spring环境中: 当前目录下的/config子目录 当前目录 一个类路径/配置包 类路径根 列表按优先级(在位置中定义的属性)排序 列表中较高的位置将覆盖在较低位置定义的那些)。

在我的例子中,我把它放在jar文件所在的目录中。

来自:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files

其他回答

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

server:
  port: 8291

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

server.port: 8291

在应用程序中。属性文件,添加这一行:

server.port = 65535

该把照片放在哪里?

24.3应用程序属性文件 SpringApplication从应用程序加载属性。属性文件 并将它们添加到Spring环境中: 当前目录下的/config子目录 当前目录 一个类路径/配置包 类路径根 列表按优先级(在位置中定义的属性)排序 列表中较高的位置将覆盖在较低位置定义的那些)。

在我的例子中,我把它放在jar文件所在的目录中。

来自:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files

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

使用application.properties

添加server.port = 9898

在运行时配置

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

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

把这段代码放在uapplicatop中。属性文件

1.1通过属性文件更新。

/ src / main /资源/ application.properties

server.port=8888

通过yaml文件更新。

   server:

     port: 8888

EmbeddedServletContainerCustomizer

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {

        container.setPort(8888);

    }

}