文章目录
本页将介绍如何改变spring boot
的默认服务端口。
当spring boot
应用程序启动时,嵌入式服务器(如Tomcat
)以默认端口启动。
嵌入式Tomcat
默认以8080
端口启动。
有很多方法可以改变默认服务器端口。
我们可以使用属性文件、系统变量和java
命令的参数来改变嵌入式servlet
容器的设置,如默认服务器端口。
我们也可以通过编程来改变嵌入式servlet
容器的设置。
在属性文件中,我们将server.port
属性配置为服务器端口值,并将SERVER_PORT
配置为系统变量。现在找到这个例子。
使用属性文件(.properties/.yml)
要使用属性文件改变服务器端口,我们需要配置server.port
属性。
a. 在类路径中使用application.properties
,如 src\main\resources\application.properties
。
server.port = 8585
服务器将从8585
端口开始。要获得随机的服务器端口,请为该属性赋值0
。
server.port = 0
现在spring boot
将在一个目前没有被系统中任何服务器使用的端口上启动服务器。
b. 在类路径中使用application.yml
,如src\main\resources\application.yml
。
server:
port: 8585
服务器将从8585
端口开始。对于随机端口,可指定为0
。
server:
port: 0
使用 -server.port 或 -Dserver.port 的 java 命令
为了使--server.port
和--Dserver.port
简短,我们可以删除server
关键字,使其成为任何简短的关键字,如--port
和--Dport
。
我们可以使用任何短的关键字。在这里,我们使用端口作为简短的关键词。
为了实现这一点,我们需要在属性文件中配置占位符,如下所示。
使用application.properties
server.port=${port:8282}
使用application.yml
server:
port: ${port:8282}
如果我们不把端口作为参数,那么默认情况下,服务器将以8282
开始。
如果我们想要不同的端口,那么我们需要在参数中传递想要的端口,如下所示。
假设我们有一个名为my-app.jar
的可执行JAR
。
使用--port
java -jar my-app.jar --port=8585
使用-Dport
java -jar -Dport=8585 my-app.jar
服务器将以8585
端口启动。
使用 SERVER_PORT 作为操作系统环境变量
我们可以通过设置SERVER_PORT
作为操作系统(如Windows
和Linux
)环境变量来改变spring boot
的默认服务器端口。
在我的例子中,我有Windows 7
。找到配置环境变量的步骤。
第1步:右击计算机图标,然后进入属性->高级系统设置->环境变量,设置变量如下
SERVER_PORT = 8585
第2步:打开命令提示符,构建项目。假设我们得到一个可执行的JAR
,即my-app.jar
,然后用java
命令运行它,如下所示。
java -jar my-app.jar
服务器将以8585
端口启动。
如果我们想从eclipse
控制台运行spring boot
应用程序,首先重新启动eclipse
,然后运行应用程序。
以编程方式在 SpringApplication 中配置 SERVER.PORT
SpringApplication
有一个setDefaultProperties()
方法,用于改变spring boot
默认属性。
假设我们想改变默认端口,那么我们需要创建一个Map
,并在SERVER.PORT
键中放入一个端口。找到这个例子。
MyApplication.java
package com.concretepage;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
Map<String, Object> map = new HashMap<>();
map.put("SERVER.PORT", "8585");
application.setDefaultProperties(map);
application.run(args);
}
}
Spring boot
将以8585
端口启动服务器。
使用 EmbeddedServletContainerCustomizer
我们可以改变嵌入式Servlet
容器的默认设置,注册一个实现EmbeddedServletContainerCustomizer
接口的bean
。我们需要重写其customize()
方法。
找到这个例子。
ContainerCustomizerBean.java
package com.concretepage.bean;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;
@Component
public class ContainerCustomizerBean implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8585);
}
}
Spring boot
将以8585
端口启动服务器。
通过改变 Eclipse 运行配置的环境变量
我们可以在eclipse
中通过配置运行配置中的环境变量来改变spring boot
的默认设置。
第1步:右键单击类,进入 Run As -> Run Configurations
第2步:点击Environment标签设置服务器端口,如下所示。
SERVER_PORT = 8585
如图所示:
第三步:从eclipse
控制台运行该应用程序。服务器将以8585
端口启动。
通过改变 Eclipse 运行配置的 Arguments
为了在eclipse
中运行spring boot
应用程序,我们用SpringApplication
作为java
应用程序运行包含main()
方法的类。要改变默认端口,请遵循以下步骤。
第1步:右键单击类,进入 Run As -> Run Configurations
第2步:点击Arguments标签设置服务器端口,如下所示。
--server.port=8585
如图所示:
第三步:从eclipse
控制台运行该应用程序。服务器将以8585
端口启动。找到控制台的输出。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)
2017-03-20 20:08:15.851 INFO 3888 --- [ main] com.concretepage.MyApplication : Starting MyApplication on Renu-PC with PID 3888 (F:\arvind\PROJECT\mars\spring-boot\spring-boot-demo\target\classes started by Renu in F:\arvind\PROJECT\mars\spring-boot\spring-boot-demo)
2017-03-20 20:08:15.856 INFO 3888 --- [ main] com.concretepage.MyApplication : No active profile set, falling back to default profiles: default
2017-03-20 20:08:15.955 INFO 3888 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@57f23557: startup date [Mon Mar 20 20:08:15 IST 2017]; root of context hierarchy
2017-03-20 20:08:17.833 INFO 3888 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8585 (http)
参考文献
【1】Change the HTTP port
【2】Customizing embedded servlet containers
【3】Spring Boot Change Default Server Port