[SPRING] management.port = 0 일 때 런타임시 스프링 부트 관리 포트 가져 오기
SPRINGmanagement.port = 0 일 때 런타임시 스프링 부트 관리 포트 가져 오기
통합 테스트에서 management.port 속성을 0으로 설정할 때 액츄에이터 끝점을 처리하는 임베디드 tomcat에 할당 된 포트를 얻는 방법에 대한 조언을 찾고 있습니다.
다음 application.yml 설정과 함께 Spring Boot 1.3.2를 사용하고있다.
server.port: 8080
server.contextPath: /my-app-context-path
management.port: 8081
management.context-path: /manage
...
그런 다음 통합 테스트에 @WebIntegrationTest 주석을 달고 위의 포트를 0으로 설정합니다.
@WebIntegrationTest({ "server.port=0", "management.port=0" })
전체 통합 테스트를 수행 할 때 응용 프로그램 구성에 액세스하려면 다음 유틸리티 클래스를 사용해야합니다.
@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}"
}
}
나는 이미 표준 포트가 local.server.port를 사용하여 얻을 수 있다는 것을 이미 알고 있으며 local.management.port라는 관리 끝점에 해당하는 것으로 보입니다. 그러나 그 의미는 다른 것 같습니다.
편집하다: 공식 문서에는이 작업을 수행하는 방법이 나와 있지 않습니다. (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-discover-the-http-port-at-runtime )
현재 관리 포트에 손을다는 방법에 대해 문서화되지 않은 방법이 있습니까?
Spring-Boot 애플리케이션을 테스트 할 때 Spock-Framework과 Spock-Spring을 사용하기 때문에 다음을 사용하여 애플리케이션을 초기화해야합니다.
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MyApplication.class)
어떻게 든 Spock-Spring이나 테스트 초기화가 @Value Annotation의 평가에 영향을 미쳐 @Value ( "$ {local.management.port}")가
java.lang.IllegalArgumentException: Could not resolve placeholder 'local.management.port' in string value "${local.management.port}"
당신의 솔루션으로 나는 속성이 존재한다는 것을 알고 있었기 때문에 Spring 환경을 직접 사용하여 테스트 런타임에 속성 값을 검색합니다.
@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}"
}
해결법
-
==============================
1.이것은 내가했던 방식이다. 테스트 클래스에서 바로 복사된다 (나는 Assassion을 위해 RestAssured를 사용한다).
이것은 내가했던 방식이다. 테스트 클래스에서 바로 복사된다 (나는 Assassion을 위해 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")); } }
-
==============================
2.Spring Boot 1.4.0부터는 더 쉬운 방법이 있습니다 :
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;
from https://stackoverflow.com/questions/36567207/get-spring-boot-management-port-at-runtime-when-management-port-0 by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] Equinox (OSGi) 및 JPA / Hibernate - 엔티티 찾기 (0) | 2019.04.11 |
---|---|
[SPRING] Spring Boot 401 Unauthorized 보안 기능 없음 (0) | 2019.04.11 |
[SPRING] 오류 : HHH000299 : 스키마 업데이트를 완료 할 수 없습니다. java.lang.NullPointerException (0) | 2019.04.11 |
[SPRING] Gradle 1.0 + Spring + AspectJ 빌드 문제 (0) | 2019.04.11 |
[SPRING] Spring 트랜잭션을 다른 쓰레드에 전달하는 방법은? (0) | 2019.04.11 |