Spring Framework

Spring Boot Multiple Port 사용 (멀티 커넥트, 다중 포트)

Junuuu 2023. 7. 31. 00:01
반응형

 개요

Spring Boot Application을 개발하며 여러 개의 Multipl Port로 접근가능하도록 설계할 순 없을까?라는 생각이 들었습니다.

예를 들어 8080, 8081, 8082로 모두 요청을 받고 싶은 경우 즉, Multiple Port를 사용하려면 어떻게 해야 할까요?

 

또는 HTTPS 접속, HTTP 접속 모두 해야 하는 경우에 사용할 수 있습니다.

 

Tomcat의 Multiple Connectors

@Configuration
class EmbeddedTomcatConfiguration {

    @Bean
    fun servletContainer(): ServletWebServerFactory {
        val tomcat = TomcatServletWebServerFactory()
        tomcat.addAdditionalTomcatConnectors(
            createStandardConnector(8081),
            createStandardConnector(8082),
            createStandardConnector(8083),
        )

        return tomcat
    }

    private fun createStandardConnector(portNumber: Int): Connector {
        val connector = Connector("org.apache.coyote.http11.Http11NioProtocol")
        connector.port = portNumber
        return connector
    }
}

TomcatServletWebServletFactory는 기본적으로 8080 포트를 수신하는 컨테이너를 생성합니다.

사용자는 이를 활용하여 내장톰켓을 커스터마이징할 수 있습니다.

addAddtionalTomcatConnectors메서드를 통해 기본 커넥터 외에 커넥터를 추가할 수 있습니다.

Connector에서 reuqest를 listen 할 port를 설정할 수 있습니다.

 

Connector는 클라이언트에서 들어오는 요청을 수락하고 처리를 위해 서버 내의 적절한 구성 요소로 전달하는 역할을 수행합니다.

서블릿 컨테이너에서 저수준의 네트워크를 처리하고 관리합니다.

 

실행 시 로그

2023-07-04T22:46:53.712+09:00  INFO 53600 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : 
Tomcat started on port(s): 8080 (http) 8081 (http) 8082 (http) 8083 (http) with context path ''

8080, 8081, 8082, 8083까지 띄워서 보았습니다 실제로 호출 테스트해 보면 잘 동작합니다.

 

 

 

 

 

 

참고자료

https://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/howto-embedded-servlet-containers.html

 

64. Embedded servlet containers

Spring Boot provides two mechanisms for enabling compression of HTTP compression; one that is Tomcat-specific and another that uses a filter and works with Jetty, Tomcat, and Undertow. 64.18.2 Enable HTTP response compression using GzipFilter If you’re

docs.spring.io