步骤1:
application.yml配置文件中添加需要监听的端口如下所示additionalPorts为该服务添加的其他端口
server:
port: 8088
additionalPorts: 8089,8087
步骤2:
新建TomcatUtil.java类,详细代码如下
Springboot 1X
package com.demo; import org.apache.catalina.connector.Connector; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; @Configuration public class TomcatUtil { @Value("${server.additionalPorts}") private String additionalPorts; @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); Connector[] additionalConnectors = this.additionalConnector(); if (additionalConnectors != null && additionalConnectors.length > 0) { tomcat.addAdditionalTomcatConnectors(additionalConnectors); } return tomcat; } private Connector[] additionalConnector() { if (StringUtils.isBlank(this.additionalPorts)) { return null; } String[] ports = this.additionalPorts.split(","); List<Connector> result = new ArrayList<>(); for (String port : ports) { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(Integer.valueOf(port)); result.add(connector); } return result.toArray(new Connector[] {}); } }
Springboot 2X
package com.demo; import org.apache.catalina.connector.Connector; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; @Configuration public class TomcatUtil { @Value("${server.additionalPorts}") private String additionalPorts; @Bean public TomcatServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); Connector[] additionalConnectors = this.additionalConnector(); if (additionalConnectors != null && additionalConnectors.length > 0) { tomcat.addAdditionalTomcatConnectors(additionalConnectors); } return tomcat; } private Connector[] additionalConnector() { if (StringUtils.isBlank(this.additionalPorts)) { return null; } String[] ports = this.additionalPorts.split(","); List<Connector> result = new ArrayList<>(); for (String port : ports) { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(Integer.valueOf(port)); result.add(connector); } return result.toArray(new Connector[] {}); } }
步骤3:
服务启动类中添加如下代码,添加位置如下图所示
@Import({TomcatUtil.class})