小编maw*_*awi的帖子

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

我正在寻找有关如何在集成测试中设置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 …
Run Code Online (Sandbox Code Playgroud)

spring spock spring-boot spring-boot-actuator

4
推荐指数
2
解决办法
6631
查看次数

标签 统计

spock ×1

spring ×1

spring-boot ×1

spring-boot-actuator ×1