springboot 配置tomcat 开启多端口监听
Posted Freeguy88
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot 配置tomcat 开启多端口监听相关的知识,希望对你有一定的参考价值。
配置文件 (application.yml)中配置端口号
server:
port: 8008
additionalPorts: 8009,8010
新建一个配置类进行配置
@Configuration
public class TomcatMultiPortConfig
@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.parseInt(port));
result.add(connector);
return result.toArray(new Connector[] );
以上是关于springboot 配置tomcat 开启多端口监听的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring Boot 中配置 Tomcat 关闭端口?