• Springboot监听多个端口


    步骤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})

     
     
  • 相关阅读:
    maven项目的导包问题,已经加载jar包了可是idea检测不到
    spark MLlib矩阵四则运算,线性代数
    maven spark Scala idea搭建maven项目的 pom.xml文件配置
    tensorflow2.0 numpy.ndarray 与tenor直接互转
    TensorFlow2.0矩阵与向量的加减乘
    Ubuntu14.04下Nginx反向代理Odoo域名
    Geforce experience报错:something went wrong try restarting geforce
    github的代码上传成功但是不显示绿格子(一直拖....心痛的教训.....)
    mapper文件提示:No data sources are configured to run this sql
    Pycharm中SQL语句提示SQL Dialect is Not Configured
  • 原文地址:https://www.cnblogs.com/guoziyi/p/14139189.html
Copyright © 2020-2023  润新知