Spring/Spring Boot
(SpringBoot) 내장 웹 서버(1) - 컨테이너와 포트
주누
2020. 1. 13. 22:59

기존 tomcat 제외시키고 jetty 등록
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>


기존 tomcat 제외시키고 undertow 등록
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
항상 tomcat은 가지고 들고 오기 때문에 다른 웹 서버를 사용하면 빼고 사용해야 한다.
설정으로 웹 서버 사용하지 않는 방법
spring.main.web-application-type=none

설정으로 포트 변경 및 랜덤 포트 설정
#포트설정
server.port=7070
#랜덤포트설정
server.port=0
ApplicationListener를 통한 웹 서버 정보 가져오는 방법
@Component
public class PortListener implements ApplicationListener<ServletWebServerInitializedEvent> {
@Override
public void onApplicationEvent(ServletWebServerInitializedEvent servletWebServerInitializedEvent) {
ServletWebServerApplicationContext applicationContext = servletWebServerInitializedEvent.getApplicationContext();
System.out.println(applicationContext.getWebServer().getPort());
}
}

코드 참조
https://github.com/mike6321/Spring/tree/master/SpringBoot/web-server-show-case
mike6321/Spring
Contribute to mike6321/Spring development by creating an account on GitHub.
github.com