当management.port = 0时,在运行时获取Spring Boot管理端口

maw*_*awi 4 spring spock spring-boot spring-boot-actuator

我正在寻找有关如何在集成测试中设置management.port属性时如何获取分配给为执行器端点提供服务的嵌入式tomcat的端口的建议0.

我使用Spring Boot 1.3.2进行以下application.yml配置:

server.port: 8080
server.contextPath: /my-app-context-path

management.port: 8081
management.context-path: /manage

...
Run Code Online (Sandbox Code Playgroud)

然后使用@WebIntegrationTest上面显示的端口设置我的集成测试0

@WebIntegrationTest({ "server.port=0", "management.port=0" })
Run Code Online (Sandbox Code Playgroud)

在进行完整集成测试时,应使用以下实用程序类来访问应用程序配置:

@Component
@Profile("testing")
class TestserverInfo {

    @Value( '${server.contextPath:}' )
    private String contextPath;

    @Autowired
    private EmbeddedWebApplicationContext server;

    @Autowired
    private ManagementServerProperties managementServerProperties


    public String getBasePath() {
        final int serverPort = server.embeddedServletContainer.port

        return "http://localhost:${serverPort}${contextPath}"
    }

    public String getManagementPath() {
        // The following wont work here:
        // server.embeddedServletContainer.port -> regular server port
        // management.port -> is zero just as server.port as i want random ports

        final int managementPort = // how can i get this one ?
        final String managementPath = managementServerProperties.getContextPath()

        return "http://localhost:${managementPort}${managementPath}"
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经知道标准端口可以通过使用,local.server.port并且似乎有一些等效的管理端点命名local.management.port.但那个似乎有不同的含义.

编辑:官方文档没有提到这样做的方法:(http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-discover-the-http-port-at -runtime)

目前是否有任何未记录的方式来管理该管理端口?


解决方案编辑

当我使用Spock-Framework和Spock-Spring测试我的spring-boot应用程序时,我必须使用以下命令初始化应用程序:

@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MyApplication.class)
Run Code Online (Sandbox Code Playgroud)

某种程度上,Spock-Spring或测试初始化​​似乎会影响@ValueAnnotation 的评估,从而@Value("${local.management.port}")导致

java.lang.IllegalArgumentException: Could not resolve placeholder 'local.management.port' in string value "${local.management.port}"
Run Code Online (Sandbox Code Playgroud)

使用您的解决方案我知道该属性存在,所以我只是直接使用spring Environment来检测测试运行时的属性值:

@Autowired
ManagementServerProperties managementServerProperties

@Autowired
Environment environment

public String getManagementPath() {
    final int managementPort = environment.getProperty('local.management.port', Integer.class)
    final String managementPath = managementServerProperties.getContextPath()

    return "http://localhost:${managementPort}${managementPath}"
}
Run Code Online (Sandbox Code Playgroud)

mag*_*ter 6

从Spring Boot 1.4.0开始,有一种更简单的方法:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
    "management.port=0", "management.context-path=/admin" })
@DirtiesContext
public class SampleTest {

    @LocalServerPort
    int port;

    @LocalManagementPort
    int managementPort;
Run Code Online (Sandbox Code Playgroud)

  • 在Spring Boot 2.0.0上,属性`management.port`被更改为`management.server.port` (3认同)

Dav*_*wer 5

我就是这样做的,直接从我的测试类复制(我使用RestAssured进行断言):

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static com.jayway.restassured.RestAssured.get;
import static org.hamcrest.CoreMatchers.equalTo;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
@WebIntegrationTest(randomPort = true, value = {"management.port=0", "management.context-path=/admin"})
@DirtiesContext
public class ActuatorEndpointTest {

    @Value("${local.management.port}")
    private int localManagementPort;

    @Test
    public void actuatorHealthEndpointIsAvailable() throws Exception {

        String healthUrl = "http://localhost:" + localManagementPort + "/admin/health";
        get(healthUrl)
                .then()
                .assertThat().body("status", equalTo("UP"));
    }



}
Run Code Online (Sandbox Code Playgroud)