How to Change the Default Port in Spring Boot – 如何改变Spring Boot中的默认端口

最后修改: 2018年 5月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Spring Boot provides sensible defaults for many configuration properties. But we sometimes need to customize these with our case-specific values.

Spring Boot为许多配置属性提供了合理的默认值。但我们有时需要用我们特定的值来定制这些属性。

And a common use case is changing the default port for the embedded server.

而一个常见的用例是改变嵌入式服务器的默认端口。

In this quick tutorial, we’ll cover several ways to achieve this.

在这个快速教程中,我们将介绍实现这一目标的几种方法。

2. Using Property Files

2.使用属性文件

The fastest and easiest way to customize Spring Boot is by overriding the values of the default properties.

定制Spring Boot的最快和最简单的方法是覆盖默认属性的值。

For the server port, the property we want to change is server.port.

对于服务器端口,我们要改变的属性是server.port

By default, the embedded server starts on port 8080.

默认情况下,嵌入式服务器在8080端口启动。

So, let’s see how to provide a different value in an application.properties file:

因此,让我们看看如何在application.properties文件中提供一个不同的值

server.port=8081

Now the server will start on port 8081.

现在,服务器将在8081端口启动。

And we can do the same if we’re using an application.yml file:

如果我们使用一个application.yml文件,我们也可以这样做。

server:
  port : 8081

Both files are loaded automatically by Spring Boot if placed in the src/main/resources directory of a Maven application.

如果放在Maven应用程序的src/main/resources目录下,Spring Boot会自动加载这两个文件。

2.1. Environment-Specific Ports

2.1.特定环境的端口

If we have an application deployed in different environments, we may want it to run on different ports on each system.

如果我们有一个部署在不同环境中的应用程序,我们可能希望它在每个系统的不同端口上运行。

We can easily achieve this by combining the property files approach with Spring profiles. Specifically, we can create a property file for each environment.

我们可以通过将属性文件方法与Spring配置文件相结合来轻松实现这一目标。具体而言,我们可以为每个环境创建一个属性文件。

For example, we’ll have an application-dev.properties file with this content:

例如,我们将有一个application-dev.properties文件,其内容如下。

server.port=8081

Then we’ll add another application-qa.properties file with a different port:

然后我们将添加另一个application-qa.properties文件,使用不同的端口。

server.port=8082

Now, the property files configuration should be sufficient for most cases. However, there are other options for this goal, so let’s explore them as well.

现在,属性文件的配置对于大多数情况来说应该是足够的。然而,对于这个目标还有其他选择,所以我们也来探讨一下。

3. Programmatic Configuration

3.程序化配置

We can configure the port programmatically either by setting the specific property when starting the application or by customizing the embedded server configuration.

我们可以通过在启动应用程序时设置特定的属性,或通过定制嵌入式服务器配置,以编程方式配置端口。

First, let’s see how to set the property in the main @SpringBootApplication class:

首先,让我们看看如何在主@SpringBootApplication类中设置该属性。

@SpringBootApplication
public class CustomApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(CustomApplication.class);
        app.setDefaultProperties(Collections
          .singletonMap("server.port", "8083"));
        app.run(args);
    }
}

Next, to customize the server configuration, we have to implement the WebServerFactoryCustomizer interface:

接下来,为了定制服务器配置,我们必须实现WebServerFactoryCustomizer接口。

@Component
public class ServerPortCustomizer 
  implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
 
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        factory.setPort(8086);
    }
}

Note that this applies to the Spring Boot 2.x version.

注意,这适用于Spring Boot 2.x版本。

For Spring Boot 1.x, we can similarly implement the EmbeddedServletContainerCustomizer interface.

对于Spring Boot 1.x,我们可以类似地实现EmbeddedServletContainerCustomizer接口。

4. Using Command-Line Arguments

4.使用命令行参数

When packaging and running our application as a jar, we can set the server.port argument with the java command:

当把我们的应用程序打包并作为jar运行时,我们可以用java命令设置server.port参数。

java -jar spring-5.jar --server.port=8083

or by using the equivalent syntax:

或使用相应的语法。

java -jar -Dserver.port=8083 spring-5.jar

5. Order of Evaluation

5.评价的顺序

As a final note, let’s look at the order in which these approaches are evaluated by Spring Boot.

最后,让我们看看Spring Boot对这些方法的评估顺序。

Basically, the configurations priority is

基本上,配置的优先级是

  • embedded server configuration
  • command-line arguments
  • property files
  • main @SpringBootApplication configuration

6. Conclusion

6.结语

In this article, we saw how to configure the server port in a Spring Boot application.

在这篇文章中,我们看到了如何在Spring Boot应用程序中配置服务器端口。

As always, the source code for the examples is available over on GitHub.

像往常一样,这些例子的源代码可以在GitHub上找到