拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 在Spring Boot中获取正在运行的端口

在Spring Boot中获取正在运行的端口

白鹭 - 2021-11-24 645 0 0

1.概述

Spring Boot应用程序嵌入了Web服务器,有时,我们可能希望在运行时发现HTTP端口。

在本教程中,我们将介绍如何在Spring Boot应用程序中以编程方式获取HTTP端口。

2.简介

2.1 我们的Spring Boot应用程序

我们将创建一个简单的Spring Boot应用程序示例,以快速显示在运行时发现HTTP端口的方法:

@SpringBootApplication

 public class GetServerPortApplication {

 public static void main(String[] args) {

 SpringApplication.run(GetServerPortApplication.class, args);

 }

 }

2.2 设置端口的两种方案

通常,配置Spring Boot应用程序的HTTP端口最直接的方法是在配置文件application.propertiesapplication.yml定义端口。

例如,在application.properties文件中,我们可以将7777设置为应用程序在其上运行的端口:

server.port=7777

另外,除了定义固定端口, 0 ”设置为“ server.port ”属性的值来让Spring Boot应用程序在随机端口上运行

server.port=0

接下来,让我们研究两种情况,并讨论在运行时以编程方式获取端口的不同方法。

在本教程中,我们将在单元测试中发现服务器端口。

3.在运行时获取固定端口

让我们创建一个属性文件application-fixedport.properties并在其中定义一个固定端口7777

server.port=7777

接下来,我们将尝试在单元测试类中获取端口:

@RunWith(SpringRunner.class)

 @SpringBootTest(classes = GetServerPortApplication.class,

 webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

 @ActiveProfiles("fixedport")

 public class GetServerFixedPortUnitTest {

 private final static int EXPECTED_PORT = 7777;

 ....

 }

在看到测试方法之前,让我们快速看一下测试类的注释:

  • @RunWith(SpringRunner.class) –这将通过Spring TestContext
  • @SpringBootTest( … SpringBootTest.WebEnvironment.DEFINED_PORT) –在SpringBootTest ,我们将DEFINED_PORT用于嵌入式Web服务器
  • @ActiveProfiles(“fixedport”) –使用此注释,我们启用了Spring概要文件“ fixedport ”,以便将我们的application-fixedport.properties加载

3.1 使用@Value(“${server.port}”)批注

由于application-fixedport.properties文件,因此我们可以@Value批注server.port ”属性:

@Value("${server.port}")

 private int serverPort;



 @Test

 public void givenFixedPortAsServerPort_whenReadServerPort_thenGetThePort() {

 assertEquals(EXPECTED_PORT, serverPort);

 }

3.2 使用ServerProperties

ServerProperties保留嵌入式Web服务器的属性,例如端口,地址和服务器标头。

我们可以注入ServerProperties组件并从中获取端口:

@Autowired

 private ServerProperties serverProperties;



 @Test

 public void givenFixedPortAsServerPort_whenReadServerProps_thenGetThePort() {

 int port = serverProperties.getPort();



 assertEquals(EXPECTED_PORT, port);

 }

到目前为止,我们已经学习了两种在运行时获取固定端口的方法。接下来,让我们看看如何在随机端口方案中发现端口。

4.在运行时获取随机端口

这次,让我们创建另一个属性文件application-randomport.properties

server.port=0

如上面的代码所示,当Web服务器启动时,我们允许Spring Boot随机选择一个空闲端口。

同样,让我们创建另一个单元测试类:

....

 @ActiveProfiles("randomport")

 public class GetServerRandomPortUnitTest {

 ...

 }

在这里,我们需要激活“ randomport ” Spring配置文件以加载相应的属性文件。

我们已经学习了两种在运行时发现固定端口的方法。但是,它们不能帮助我们获得随机端口:

@Value("${server.port}")

 private int randomServerPort;



 @Test

 public void given0AsServerPort_whenReadServerPort_thenGet0() {

 assertEquals(0, randomServerPort);

 }



 @Autowired

 private ServerProperties serverProperties;



 @Test

 public void given0AsServerPort_whenReadServerProps_thenGet0() {

 int port = serverProperties.getPort();



 assertEquals(0, port);

 }

如这两个测试方法所示, @Value(“${server.port}”)serverProperties.getPort() “ 0”报告为端口。显然,这不是我们期望的正确端口。

4.1 使用ServletWebServerApplicationContext

如果嵌入式Web服务器启动,Spring Boot将启动ServletWebServerApplicationContext

因此,我们可以WebServer以获得服务器信息或操纵服务器:

@Autowired

 private ServletWebServerApplicationContext webServerAppCtxt;



 @Test

 public void given0AsServerPort_whenReadWebAppCtxt_thenGetThePort() {

 int port = webServerAppCtxt.getWebServer().getPort();



 assertTrue(port > 1023);

 }

在上面的测试中,我们检查端口是否大于1023。这是因为0-1023是系统端口。

4.2 处理ServletWebServerInitializedEvent

Spring应用程序可以发布各种事件,而EventListeners处理这些事件。

嵌入式Web服务器启动后,将发布[ServletWebServerInitializedEvent](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/context/ServletWebServerInitializedEvent.html)此事件包含有关Web服务器的信息。

因此,我们可以创建一个EventListener来获取此事件的端口:

@Service

 public class ServerPortService {

 private int port;



 public int getPort() {

 return port;

 }



 @EventListener

 public void onApplicationEvent(final ServletWebServerInitializedEvent event) {

 port = event.getWebServer().getPort();

 }

 }

我们可以将服务组件注入到我们的测试类中,以快速获取随机端口:

@Autowired

 private ServerPortService serverPortService;



 @Test

 public void given0AsServerPort_whenReadFromListener_thenGetThePort() {

 int port = serverPortService.getPort();



 assertTrue(port > 1023);

 }

5.结论

通常,我们在属性文件或YAML文件中配置Spring Boot应用程序的服务器端口,我们可以在其中设置固定端口或随机端口。

在本文中,我们讨论了在运行时获取固定端口和随机端口的不同方法。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *