Springboot启用多个监听端口代码实例
程序员文章站
2022-04-09 16:20:44
前段时间服务有这么个需求,web端的接口需要有web安全相关的整改,但是对内的接口并不需要。所以服务需要多个端口启动,在此记录下代码如下@configurationpublic class embed...
前段时间服务有这么个需求,web端的接口需要有web安全相关的整改,但是对内的接口并不需要。所以服务需要多个端口启动,在此记录下
代码如下
@configuration public class embeddedtomcatconfiguration { @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[] {}); } }
application.yml
server:
port: ${appport:8800}
additionalports: 8881,8882
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。