简体   繁体   English

如何为 Spring 引导应用程序配置端口

[英]How to configure port for a Spring Boot application

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 引导应用程序侦听的 TCP/IP 端口,使其不使用默认端口 8080。

As said in docs either set server.port as system property using command line option to jvm -Dserver.port=8090 or add application.properties in /src/main/resources/ with文档中所述,使用 jvm -Dserver.port=8090的命令行选项将server.port设置为系统属性,或者在/src/main/resources/中添加application.properties

server.port=8090

For a random port use:对于随机端口使用:

server.port=0

Similarly add application.yml in /src/main/resources/ with:同样在/src/main/resources/中添加application.yml

server:
  port: 8090

There are two main ways to change the port in the Embedded Tomcat in a Spring Boot Application.在 Spring Boot 应用程序中更改嵌入式 Tomcat 中的端口有两种主要方法。

Modify application.properties修改 application.properties

First you can try the application.properties file in the /resources folder:首先,您可以尝试 /resources 文件夹中的 application.properties 文件:

server.port = 8090

application.properties 文件

Modify a VM option修改 VM 选项

The second way, if you want to avoid modifying any files and checking in something that you only need on your local, you can use a vm arg:第二种方式,如果你想避免修改任何文件并签入你只需要在本地的东西,你可以使用 vm arg:

Go to Run -> Edit Configurations -> VM options转到运行 -> 编辑配置 -> 虚拟机选项

-Dserver.port=8090

使用 vm arg 更改端口

Additionally, if you need more information you can view the following blog post here: Changing the port on a Spring Boot Application此外,如果您需要更多信息,可以在此处查看以下博客文章: 更改 Spring Boot 应用程序上的端口

Since Spring Boot provides various configuration externalization mechanism (through various PropertySource implementations and/or processors wired into Environment object in order), you can set any property outside of your jar archive through following methods:由于 Spring Boot 提供了各种配置外部化机制(通过各种PropertySource实现和/或按顺序连接到Environment对象的处理器),您可以通过以下方法设置 jar 存档之外的任何属性:

  1. Pass property through command line argument as application argument通过命令行参数传递属性作为应用程序参数

    java -jar <path/to/my/jar> --server.port=7788
  2. From property in SPRING_APPLICATION_JSON (Spring Boot 1.3.0+)来自SPRING_APPLICATION_JSON (Spring Boot 1.3.0+) 中的属性

    • Define environment variable in U*IX shell:在 U*IX shell 中定义环境变量:

       SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar>
    • By using Java system property:通过使用 Java 系统属性:

       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}'
  3. Define JVM system property定义JVM 系统属性

    java -Dserver.port=7788 -jar <path/to/my/jar>
  4. Define OS environment variable定义操作系统环境变量

    • U*IX Shell U*IX 外壳

      SERVER_PORT=7788 java -jar <path/to/my/jar>
    • Windows视窗

      SET SERVER_PORT=7788 java -jar <path/to/my/jar>
  5. Place property in ./config/application.properties configuration file将属性放在./config/application.properties配置文件

    server.port=7788

    and run:并运行:

     java -jar <path/to/my/jar>
  6. Place property in ./config/application.yaml将属性放在./config/application.yaml

     server: port: 7788

    and run:并运行:

     java -jar <path/to/my/jar>
  7. Place property in ./application.properties将属性放在./application.properties

     server.port=7788

    and run:并运行:

     java -jar <path/to/my/jar>
  8. Place property in ./application.yaml将属性放在./application.yaml

     server: port: 7788

    and run:并运行:

     java -jar <path/to/my/jar>

You can combine above methods all together, and the former configuration in the list take precedence over the latter one.您可以将以上方法组合在一起,列表中的前一种配置优先于后一种配置。

For example:例如:

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

The server will start and listen on port 7788.服务器将启动并侦听端口 7788。

This is very useful providing default properties in PropertySources with lower precedence (and usually packaged in the archive or coded in the source), and then override it in the runtime environment.这非常有用,可以在 PropertySources 中以较低的优先级提供默认属性(通常打包在存档中或在源代码中编码),然后在运行时环境中覆盖它。 And it is the design philosophy of Spring Boot:这就是 Spring Boot 的设计理念:

Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.开箱即用,但随着需求开始偏离默认值,请迅速摆脱困境。


SERVER_NAME to server.name conversion was done by Relaxed Binding . SERVER_NAMEserver.name的转换是由Relaxed Binding完成的。

Also, you can configure the port programmatically.此外,您可以通过编程方式配置端口。

For Spring Boot 2.xx:对于 Spring Boot 2.xx:

@Configuration
public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
  public void customize(ConfigurableServletWebServerFactory factory){
    factory.setPort(8042);
  }
}

For older versions:对于旧版本:

@Configuration
public class ServletConfig {
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {
            container.setPort(8012);
        });
    }
}

If you would like to run it locally, use this -如果您想在本地运行它,请使用它 -

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

You can set port in java code:您可以在 java 代码中设置端口:

HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);

new SpringApplicationBuilder()
    .sources(SampleController.class)                
    .properties(props)
    .run(args);

Or in application.yml:或者在 application.yml 中:

server:
    port: 9999

Or in application.properties:或在 application.properties 中:

server.port=9999

Or as a command line parameter:或作为命令行参数:

-Dserver.port=9999

In case you are using application.yml add the Following lines to it如果您使用的是application.yml ,请添加以下行

server:
     port: 9000

and of course 0 for random port.当然,随机端口为 0。

As explained in Spring documentation , there are several ways to do that:正如Spring 文档中所解释的,有几种方法可以做到这一点:

Either you set the port in the command line (for example 8888)您可以在命令行中设置端口(例如 8888)

-Dserver.port=8888 or --server.port=8888 -Dserver.port=8888--server.port=8888

Example : java -jar -Dserver.port=8888 test.jar示例: java -jar -Dserver.port=8888 test.jar

Or you set the port in the application.properties或者您在 application.properties 中设置端口

server.port=${port:4588}

or (in application.yml with yaml syntax)或(在 application.yml 中使用 yaml 语法)

server:
   port: ${port:4588}

If the port passed by -Dport (or -Dserver.port) is set in command line then this port will be taken into account.如果在命令行中设置了 -Dport(或 -Dserver.port)传递的端口,则将考虑该端口。 If not, then the port will be 4588 by default.如果不是,则默认端口为 4588。

If you want to enforce the port in properties file whatever the environment variable, you just have to write:如果你想在属性文件中强制使用任何环境变量的端口,你只需要写:

server.port=8888

application.properties中包含以下属性

server.port=8080

When you need a programatically way of doing it, you can set it during startup:当您需要以编程方式执行此操作时,可以在启动期间进行设置:

System.getProperties().put( "server.port", 80 );
SpringApplication.run(App.class, args);

This might help for things like environment dependent port.这可能有助于诸如环境相关端口之类的事情。 Have a nice day祝你今天过得愉快

To extend other answers:扩展其他答案:

There is a section in the docs for testing which explains how to configure the port on integration tests:测试文档中有一个部分解释了如何在集成测试中配置端口:


At integration tests, the port configuration is made using the annotation @SpringBootTest and the webEnvironment values.在集成测试中,使用注释@SpringBootTestwebEnvironment值进行端口配置。


Random port:随机端口:

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)

You can inject the value using @LocalServerPort which is the same as @Value("${local.server.port}") .您可以使用与@Value("${local.server.port}")相同的@LocalServerPort注入值。

  • Example:例子:

Random port test configuration:随机端口测试配置:

@RunWith(SpringRunner.class
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ExampleTest {
   ...
   @LocalServerPort //to inject port value
   int port;
}

Defined port:定义端口:

@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)

It takes the value from server.port if is defined.如果已定义,它将从server.port获取值。

  • If is defined using @TestPropertySource(properties = "server.port=9192") , it overrides other defined values.如果使用@TestPropertySource(properties = "server.port=9192")定义,它将覆盖其他定义的值。
  • If not, it takes the value from src/test/resources/application.properties (if exists).如果没有,它会从src/test/resources/application.properties中获取值(如果存在)。
  • And finally, if it is not defined it starts with the default 8080 .最后,如果未定义,则以默认的8080开头。

Example:例子:

Defined port test configuration:定义端口测试配置:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "server.port=9192")
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
    }

}

if you are using gradle as the build tool, you can set the server port in your application.yml file as:如果您使用 gradle 作为构建工具,您可以在application.yml文件中将服务器端口设置为:

server:
  port: 8291

If you are using maven then the port can be set in your application.properties file as:如果您使用的是 maven,则可以在application.properties文件中将端口设置为:

server.port: 8291

You can specify port by overriding EmbeddedServletContainerFactory bean within your configuration (java based or xml).您可以通过在您的配置(基于 java 或 xml)中覆盖EmbeddedServletContainerFactory bean 来指定端口。 There you can specify port for used embedded servlet container.在那里,您可以为使用的嵌入式 servlet 容器指定端口。 Please, see Spring Boot - Core "Embedded Servlet Container Support" paragraph and example there.请参阅Spring Boot - Core "Embedded Servlet Container Support" 段落和示例。 Hope this helps.希望这可以帮助。

在资源中存在的application.properties文件中:

server.port=8082

There are three ways to do it depending on the application configuration file you are using根据您使用的应用程序配置文件,有三种方法可以做到这一点

a) If you are using application.properties file set a) 如果您使用的是application.properties文件集

server.port = 8090

b) If you are using application.yml file 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");

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

将此添加到您的application.properties文件中

server.port= 8080

There are many other stuffs you can alter in server configuration by changing application.properties.您可以通过更改 application.properties 在服务器配置中更改许多其他内容。 Like session time out, address and port etc. Refer below post像会话超时,地址和端口等。请参阅下面的帖子

ref: http://docs.spring.io/spring-boot/docs/1.4.x/reference/html/common-application-properties.html参考: http ://docs.spring.io/spring-boot/docs/1.4.x/reference/html/common-application-properties.html

I used few of them as below.我使用了其中的几个,如下所示。

server.session.timeout=1
server.port = 3029
server.address= deepesh
  1. As everyone said, you can specify in application.properties正如大家所说,可以在application.properties中指定
    server.port = 9000 (could be any other value) server.port = 9000 (可以是任何其他值)

  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 8080,如果你想改变它,那么在application.properties中提到
    management.port = 9001 (could be any other value) management.port = 9001 (可以是任何其他值)

In the application.properties file, add this line:application.properties文件中,添加以下行:

server.port = 65535

where to place that fie:在哪里放置该文件:

24.3 Application Property Files 24.3 应用程序属性文件

SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment: SpringApplication 从以下位置的 application.properties 文件中加载属性并将它们添加到 Spring 环境中:

 A /config subdirectory of the current directory The current directory A classpath /config package The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).该列表按优先级排序(在列表中较高位置定义的属性会覆盖在较低位置定义的属性)。

In my case I put it in the directory where the jar file stands.就我而言,我将它放在jar文件所在的目录中。

From:从:

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

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=0 spring boot will find any unassigned http random port for us.注意– 您可以使用server.port=0 spring 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

Indeed, the easiest way is to set the server.port property.事实上,最简单的方法是设置server.port属性。

If you are using STS as IDE, from version 3.6.7 you actually have Spring Properties Editor for opening the properties file.如果您使用 STS 作为 IDE,从 3.6.7 版开始,您实际上有用于打开属性文件的Spring Properties Editor

This editor provides autocomplete for all Spring Boot properties.此编辑器为所有 Spring Boot 属性提供自动完成功能。 If you write port and hit CTRL + SPACE, server.port will be the first option.如果你写端口并按 CTRL + SPACE, server.port将是第一个选项。

By default, spring-web module provides an embedded tomcat server that is running under the 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-web 模块提供了一个在 8080 端口下运行的嵌入式 tomcat 服务器。如果您需要更改应用程序的端口号,请转到application.properties文件并使用server.port属性配置端口号.

  server.port= 9876

then your application is running under the port 9876.那么您的应用程序在端口 9876 下运行。

Using property server.port=8080 for instance like mentioned in other answers is definitely a way to go.例如,使用其他答案中提到的属性 server.port=8080 绝对是一种方法。 Just wanted to mention that you could also expose an environment property:只是想提一下,您还可以公开环境属性:

SERVER_PORT=8080

Since spring boot is able to replace "."由于弹簧靴能够替换“。” for "_" and lower to UPPER case for environment variables in recent versions.在最近的版本中,环境变量的“_”和小写为大写。 This is specially useful in containers where all you gotta do is define that environment variable without adding/editing application.properties or passing system properties through command line (ie -Dserver.port=$PORT )这在容器中特别有用,您只需定义环境变量而不添加/编辑application.properties或通过命令行传递系统属性(即-Dserver.port=$PORT

Hope this one help希望这个有帮助

application.properties=> 

server.port=8090

application.yml=> 

server
  port:8090

You can add the port in below methods.您可以通过以下方法添加端口。

  1. Run -> Configurations section运行 -> 配置部分

  2. In application.xml add server.port=XXXXapplication.xml添加server.port=XXXX

Just have a application.properties in src/main/resources of the project and give there只需在项目的src/main/resources中有一个application.properties并提供

server.port=****

where **** refers to the port number.其中****指的是端口号。

1.1 Update via a properties file. 1.1 通过属性文件更新。

/src/main/resources/application.properties /src/main/resources/application.properties

server.port=8888服务器端口=8888

Update via a yaml file.通过 yaml 文件更新。

   server:

     port: 8888

EmbeddedServletContainerCustomizer EmbeddedServletContainerCustomizer

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {

        container.setPort(8888);

    }

}

Providing the port number in application.properties file will resolve the issue在 application.properties 文件中提供端口号将解决问题

 server.port = 8080

"port depends on your choice, where you want to host the application" “端口取决于您的选择,您希望在哪里托管应用程序”

您可以在 /src/main/resources/ 下的 application.properties 中进行设置

server.port = 8090

By Default Spring-web module provides an embedded tomcat server runs on port number 8080.默认情况下,Spring-web 模块提供了一个在端口号 8080 上运行的嵌入式 tomcat 服务器。

You can change it as follows -您可以按如下方式更改它 -

A) If you are using gradle then use can set the property in your application.yml : A)如果您使用的是 gradle,那么使用可以在您的 application.yml 中设置属性:

 server:  
      port: 8042

B) If you are using maven then you can set the property in your application.properties : B)如果您使用的是 maven,那么您可以在 application.properties 中设置属性:

server.port: 8042

C) When you have port in your own config file and want to set it during runtime. C)当您在自己的配置文件中有端口并希望在运行时设置它时。

By implementing WebServerFactoryCustomizer interface - Spring 2.x通过实现 WebServerFactoryCustomizer 接口 - Spring 2.x

@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

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

By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x通过实现 EmbeddedServletContainerCustomizer 接口 - Spring 1.x

@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {

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

You can also use SERVER_PORT environment variable to configure Spring Boot port.您还可以使用SERVER_PORT环境变量来配置 Spring Boot 端口。 Just set the environment variable and restart the app:只需设置环境变量并重新启动应用程序:

set SERVER_PORT=9999 // on windows machine
export SERVER_PORT=9999 // on linux

Please note that if you do not set those environment variables system wide, you should run the boot app on the same session.请注意,如果您未在系统范围内设置这些环境变量,则应在同一会话上运行引导应用程序。

Mostly springboot runs on port:8080 because of embedded Tomcat.由于嵌入式 Tomcat,springboot 大多在port:8080上运行。 In some it may throw an error port 8080 already in use .在某些情况下,它可能会抛出错误port 8080 already in use To avoid this kind of issues we can config the server port.为避免此类问题,我们可以配置服务器端口。

Using application.properties使用 application.properties

add server.port=9898添加server.port=9898

On runtime config在运行时配置

run your application with below arguments.使用以下参数运行您的应用程序。

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

您可以在 spring boot 项目的资源文件夹中的 application.properties 文件中配置您的端口。

server.port="port which you need"

" server.port=8080 " will only works if your running application as a jar through main method, server.port=8080 ” 仅在您通过 main 方法将应用程序作为 jar 运行时才有效,

This configuration will not work if your running this application as a war file through tomcat container.如果您通过 tomcat 容器将此应用程序作为 war 文件运行,则此配置将不起作用。

If you are going to run apps as jar file in command environment, just type "SERVER_PORT=*** " as prefix.如果您要在命令环境中将应用程序作为 jar 文件运行,只需键入“SERVER_PORT=***”作为前缀。 The full command to execute will look like below:要执行的完整命令如下所示:

SERVER_PORT=8080 java -jar ***.jar

If you wanna run app in background in Linux, command with 'nohup' will look like below:如果您想在 Linux 中在后台运行应用程序,带有“nohup”的命令将如下所示:

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

There are three ways to do it有三种方法可以做到

1 Set server.port property in application.properties file 1 在application.properties文件中设置 server.port 属性

server.port = 8090

2 Set server port property in application.yml file 2 在application.yml文件中设置服务器端口属性

server:
     port: 8090

3 Set the property as system property in main method 3 在main方法中设置属性为系统属性

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

Just set the environment variable SERVER_PORT .只需设置环境变量SERVER_PORT
(The examples works on Linux ) (示例适用于Linux

  • Start via java -jar :通过java -jar启动:
    SERVER_PORT=9093 java -jar target/eric-sc-dummy.jar

  • Start via maven spring-boot plugin :通过maven spring-boot 插件启动
    SERVER_PORT=9093 mvn spring-boot:run

Tips:提示:

  • If you add other sub commands before the java -jar or mvn command, then you need to add export to set env in a separate command, and split them via ;如果在java -jarmvn命令之前添加其他子命令,则需要在单独的命令中添加export以设置 env,并通过;将它们拆分。 , to make sure it's available to sub process. ,以确保它可用于子进程。
    eg:例如:
    export SERVER_PORT=9093; export MAVEN_OPTS="-Xmx256m -Xms64m"; mvn spring-boot:run

使用 mvn shell 命令行,spring-boot 2:

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

Apart from all the answers, I would like to point out that most IDE (IntelliJ with Spring plugin, STS) have a feature where it suggests all the configuration keys supported by SpringBoot.除了所有答案之外,我想指出大多数 IDE(带有 Spring 插件的 IntelliJ,STS)都有一个功能,它建议 SpringBoot 支持的所有配置键。 (ie all the opinionated configuration keywords) (即所有自以为是的配置关键字)

在此处输入图像描述

Spring Plugin for Intellij Intellij 的 Spring 插件

You can configure the port in the application.property file or application.yaml file which is in src/main/resources .您可以在src/main/resources中的application.property文件或application.yaml文件中配置端口。

server.port=8080 server.port=8080

在此处输入图像描述

Server port declare in two types服务器端口声明有两种

1.static type 1.静态类型

   server.port=8080. // your port number
  1. Dynamic type动态类型

     server.port=0. // randomly generate port number. server.port=${PORT:0}

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.

This question is the first result if you google for Gradle Spring Port .如果您搜索Gradle Spring Port ,这个问题是第一个结果。

If you use gradle you can do something like this if you have the Spring Boot Gradle Plugin already applied:如果您使用 gradle,如果您已经应用了 Spring Boot Gradle 插件,则可以执行以下操作:

bootRun {
    args += ["--server.port=[PORT]"]
}

For a more sophisticated answer please see my answer here .如需更复杂的答案,请在此处查看我的答案。

If you are working over boot projects and you wanna configure the port you can give the input in the application.properties file like NOTE:properties file should be under src/main/resource如果您正在处理引导项目并且想要配置端口,您可以在 application.properties 文件中提供输入,例如 注意:properties 文件应该在 src/main/resource 下

Spring properties弹簧特性

server.port=9999 If you using the CMD then follow this command -Dserver.port=9999 For default port its server.port=0 Make sure no port is using this port number server.port=9999如果你使用 CMD 然后按照这个命令-Dserver.port=9999对于默认端口它的 server.port=0 确保没有端口正在使用这个端口号

https://stackoverflow.com/a/36865796/1587329https://stackoverflow.com/a/40799750/1587329类似, gradle one-liner将是

SERVER_PORT=9090 gradle bootRun

在应用程序属性中只需添加 1 行

server.port = 8090

Programmatically, with spring boot 2.1.5:以编程方式,使用 spring boot 2.1.5:

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(9000);
    }

}

Via通过

application.properties应用程序属性

server.port = 8082 (or any new port number) server.port = 8082 (或任何新的端口号)

via通过

application.yml应用程序.yml

server
  port: 8082

Running by Gradle:通过 Gradle 运行:

  • Run in default port(8080): ./gradlew bootRun在默认端口(8080)中运行: ./gradlew bootRun
  • Run in provided port(8888): ./gradlew bootRun --args='--server.port=8888'在提供的端口(8888)中运行: ./gradlew bootRun --args='--server.port=8888'
  • If we have any variable in the application.properties file named PORT , run this: PORT=8888 ./gradlew bootRun如果我们在名为PORTapplication.properties文件中有任何变量,请运行: PORT=8888 ./gradlew bootRun

Running by Maven:由 Maven 运行:

  • Run in default port(8080): mvnw spring-boot:run在默认端口(8080)中运行: mvnw spring-boot:run
  • Run in provided port(8888): mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'在提供的端口(8888)中运行: mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
  • Run in provided port(8888): mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'在提供的端口(8888)中运行: mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'
  • Run in provided port(8888) with other custom property: mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"使用其他自定义属性在提供的端口(8888)中运行: mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
  • If we have any variable in the application.properties file named PORT , run this: SERVER_PORT=9093 mvn spring-boot:run如果我们在名为PORTapplication.properties文件中有任何变量,请运行: SERVER_PORT=9093 mvn spring-boot:run

Using java -jar :使用java -jar

  • Create the .jar file:创建 .jar 文件:
    • For Gradle: ./gradlew clean build .对于 Gradle: ./gradlew clean build We will find the jar file inside: build/libs/ folder.我们会在里面找到jar文件: build/libs/文件夹。
    • For Maven: mvn clean install .对于 Maven: mvn clean install We will find the jar file inside: target folder.我们会在里面找到jar文件: target文件夹。
  • Run in default port(8080): java -jar myApplication. jar在默认端口(8080)中运行: java -jar myApplication. jar java -jar myApplication. jar
  • Run in provided port(8888): java -jar myApplication.jar --port=8888在提供的端口(8888)中运行: java -jar myApplication.jar --port=8888
  • Run in provided port(8888): java -jar -Dserver.port=8888 myApplication.jar在提供的端口(8888)中运行: java -jar -Dserver.port=8888 myApplication.jar
  • Run in provided port(8888) having variable SERVER_PORT in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar在 application.properties 文件中具有变量SERVER_PORT的提供的端口(8888)中运行: SERVER_PORT=8888 java -jar target/myApplication.jar

This worked for me :这对我有用:

Added a custom container class :添加了一个自定义容器类:

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
       configurableEmbeddedServletContainer.setPort(8888);
    }

}

But this was still not using port 8888.但这仍然没有使用端口 8888。

Then I set "scanBasePackages" property like this on "@SpringBootApplication" class on my main method: (scanBasePackages = {"custom container package"})然后我在我的主要方法的“@SpringBootApplication”类上设置“scanBasePackages”属性: (scanBasePackages = {"custom container package"})

@SpringBootApplication(scanBasePackages = {"com.javabrains.util"})
public class CourseApiApp {

    public static void main (String args []) {
        SpringApplication.run(CourseApiApp.class, args);
    }
}

And it started picking up port set in Custom Container.它开始拾取自定义容器中设置的端口。

In spring boot you can easily configure the service exposing port in application.properties file.在 Spring Boot 中,您可以轻松地在application.properties文件中配置服务公开端口。

server.port=portnumber

if you don't set a port specifically, then it will try to open the service with port 80 .如果您没有专门设置端口,那么它将尝试使用端口80打开服务。 in case if it is already used, service will not be started on the embedded container.如果它已被使用,则不会在嵌入式容器上启动服务。

如果您的端口号可以是随机的,您可以在application.properties server.port=${random.int(4)}中使用 random 函数

server.port = 0 for random port server.port = 0表示随机端口

server.port = 8080 for custom 8080 port server.port = 8080 用于自定义 8080 端口

在 application.properties 文件中添加以下内容: server.port=8888 此处需要始终提及的 PROT

Configure the port details in Config file or application properties.配置文件或应用程序属性中配置端口详细信息。

eg例如

port =8876

put this code in u applicatop.properties file将此代码放入您的 applicatop.properties 文件中
在此处输入图像描述

If you are use the spring command line interface (CLI) use the -- to separate commands from the spring command arguments, to change the port:如果您使用 spring 命令行界面 (CLI),请使用--将命令与spring命令参数分开,以更改端口:

spring run hello.groovy -- --server.port=9000

spring-boot cli 弹簧启动 cli

You need to type on application.properties file您需要在application.properties文件中输入

server.port=8500

You need to type on application.yml file您需要在application.yml文件中输入

server:
  port : 8500

Open the application.propertise file in your spring bot application.在您的 spring bot 应用程序中打开 application.propertise 文件。 And add the below property in the propertise file.并在属性文件中添加以下属性。

server.port= 1443

This will be working fine and you can set any port number as per your wish.这将正常工作,您可以根据需要设置任何端口号。

Aside from adding the port on application.properties, you can also easily achieve multiple ports for different environments, by combining the property files approach with Spring profiles.除了在 application.properties 上添加端口外,您还可以通过将属性文件方法与 Spring 配置文件相结合,轻松实现不同环境的多个端口。 Specifically, we can create a property file for each environment.具体来说,我们可以为每个环境创建一个属性文件。

For example, we'll have an application-dev.properties file with this content:例如,我们将有一个包含以下内容的 application-dev.properties 文件:

server.port=8081

Then you can add another application-qa.properties file with a different port:然后,您可以添加另一个具有不同端口的 application-qa.properties 文件:

server.port=8082

In my case adding statement在我的情况下添加声明

server.port=${port:8081}

override the default tomcat server port.覆盖默认的 tomcat 服务器端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spring Boot应用程序中配置PageableHandlerMethodArgumentResolver - How to configure PageableHandlerMethodArgumentResolver in spring boot application 如何使用 Swing 应用程序配置 spring-boot - How to configure spring-boot with swing application 如何在Spring Boot应用程序中配置DispatcherServlet? - How to configure DispatcherServlet in a Spring Boot application? Spring Boot,如何使用application.proerties配置spring安全性 - Spring Boot, how to configure spring security using application.proerties 如何在端口 80 上运行 Spring Boot 应用程序 - How can I run a Spring Boot application on port 80 如何避免在Spring Boot应用程序中写入显式端口号(8443) - How to avoid writing explicit port number (8443) in Spring Boot application 如何使用Spring Boot应用程序更改嵌入式tomcat连接器端口 - How to change embedded tomcat connector port using spring boot application 如何通过Spring Boot应用程序和Spock测试在运行时更改服务器端口 - How to change server port in runtime with a spring boot application and spock testing 如何使用Spring Boot应用程序配置tnsnames.ora文件? - How to configure tnsnames.ora file with Spring Boot application? 如何配置Spring启动应用程序以通过MySQL使用SSL / TLS? - How to configure spring boot application to use SSL/TLS over MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM