Springboot 项目开启多个端口

通常一个springboot只启动一个端口,想要在把实例启动另外的端口可以采用如下配置:

/**
 * 给实例再开一个端口
 *
 * @author dxy
 * @version 1.0
 * @date 2021/10/18 20:46
 */

@Component
public class TomcatPortConfig {
    @Bean
    public TomcatServletWebServerFactory getFactory() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        Connector[] connectors = this.addtionalConnetors();
        if (connectors != null && connectors.length > 0) {
            tomcat.addAdditionalTomcatConnectors(connectors);
        }
        return tomcat;
    }

    @Value("${server.addtional-ports:22211}")
    String ports;

    private Connector[] addtionalConnetors() {
        if (StringUtils.isEmpty(ports)) {
            return null;
        }
        String[] port = ports.split(",");
        List connectors = new ArrayList<>();
        for (String s : port) {
            Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
            connector.setPort(Integer.parseInt(s));
            connector.setScheme("http");
            connector.setRedirectPort(8041);
            connectors.add(connector);
        }
        return connectors.toArray(new Connector[]{});
    }
}

 

效果:

你可能感兴趣的:(tomcat,java,spring,boot)