Java 如何为 Spring Boot 应用程序配置端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21083170/
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-13 06:27:49  来源:igfitidea点击:

How to configure port for a Spring Boot application

javaspringspring-bootserverport

提问by Paul Verest

How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.

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

采纳答案by Paul Verest

As said in docseither set server.portas system property using command line option to jvm -Dserver.port=8090or add application.propertiesin /src/main/resources/with

正如在所述的文档任一组server.port使用命令行选项来JVM中的系统属性-Dserver.port=8090或添加application.properties/src/main/resources/

server.port=8090

For random port use

用于随机端口使用

server.port=0

回答by Amandeep Singh

There are three ways to do it

有三种方法可以做到

1 Set server.port property in application.propertiesfile

1 在application.properties文件中设置 server.port 属性

server.port = 8090

2 Set server port property in application.ymlfile

2 在application.yml文件中设置服务器端口属性

server:
     port: 8090

3 Set the property as system property in mainmethod

3 在main方法中将属性设置为系统属性

System.setProperty("server.port","8090");

回答by Lova Chittumuri

The default port is : 8080 but we can customize the port number in application.properties as shown below

默认端口是:8080 但我们可以在 application.properties 中自定义端口号,如下所示

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port = 5050 -- #here we can give any port number.

回答by itwarilal

If you would like to run it locally, use this -

如果你想在本地运行它,使用这个 -

mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8085'

mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8085'

As of Spring Boot 2.0, here's the command that works (clues were here):

Spring Boot 2.0 开始,这是有效的命令(线索在这里):

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085

回答by Rakesh

  1. As everyone said, you can specify in application.properties
    server.port = 9000(could be any other value)

  2. If you are using spring actuator in your project, by default it points to
    8080, and if you want to change it, then in application.properties mention
    management.port = 9001(could be any other value)

  1. 正如大家所说,您可以在 application.properties
    server.port = 9000 中指定(可以是任何其他值)

  2. 如果您在项目中使用弹簧执行器,默认情况下它指向
    8080,如果您想更改它,则在 application.properties 中提及
    management.port = 9001(可以是任何其他值)

回答by nndru

You can specify port by overriding EmbeddedServletContainerFactorybean within your configuration (java based or xml). There you can specify port for used embedded servlet container. Please, see Spring Boot - Core"Embedded Servlet Container Support" paragraph and example there. Hope this helps.

您可以通过EmbeddedServletContainerFactory在您的配置(基于 Java 或 xml)中覆盖bean来指定端口。在那里您可以为使用的嵌入式 servlet 容器指定端口。请参阅Spring Boot - 核心“嵌入式 Servlet 容器支持”段落和示例。希望这可以帮助。

回答by NafazBenzema

By default, spring-boot provides an embedded tomcat server that is running under port number 8080. If you need to change the port number of the application then go to application.properties file and configure the port number by using server.port property.

默认情况下,spring-boot 提供了一个运行在 8080 端口下的嵌入式 tomcat 服务器。如果您需要更改应用程序的端口号,请转到 application.properties 文件并使用 server.port 属性配置端口号。

  server.port= 9876

then your application is running under there port 9876.

那么您的应用程序将在端口 9876 下运行。

回答by anandchaugule

By default spring boot app start with embedded tomcat server start at default port 8080. spring provides you with following different customization you can choose one of them.

默认情况下,spring boot 应用程序以嵌入式 tomcat 服务器启动,默认端口为 8080。spring 为您提供以下不同的自定义,您可以选择其中之一。

NOTE– you can use server.port=0spring boot will find any unassigned http random port for us.

注意– 您可以使用server.port=0spring boot 会为我们找到任何未分配的 http 随机端口。

1) application.properties

1) application.properties

server.port=2020

2) application.yml

2) 应用程序.yml

server:  
     port : 2020

3) Change the server port programatically

3)以编程方式更改服务器端口

3.1) By implementing WebServerFactoryCustomizer interface - Spring 2.x

3.1) 通过实现 WebServerFactoryCustomizer 接口 - Spring 2.x

@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        // customize the factory here
        factory.setPort(2020);
    }
}

3.2) By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x

3.2) 通过实现 EmbeddedServletContainerCustomizer 接口 - Spring 1.x

@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        // customize here
        container.setPort(2020);
    }
}

4) By using command line option

4) 通过使用命令行选项

 java -jar spring-boot-app.jar -Dserver.port=2020

回答by serv-inc

回答by Praneeth

There are three ways to do it depending on the application configuration file you are using

根据您使用的应用程序配置文件,有三种方法可以做到

a) If you are using application.propertiesfile set

a) 如果您使用的是application.properties文件集

server.port = 8090

server.port = 8090

b) If you are using application.ymlfile set server port property in YAML format as given below

b) 如果您使用application.yml文件以 YAML 格式设置服务器端口属性,如下所示

server:
     port: 8090

c) You can also Set the property as the System property in the main method

c) 也可以在main方法中将该属性设置为System属性

System.setProperty("server.port","8090");

回答by Sagar Mal Shankhala

Hope this one help

希望这个有帮助

application.properties=> 

server.port=8090

application.yml=> 

server
  port:8090