SpringBoot自动寻找可用端口启动

首先实现这个需求需要解决2个问题

1.如何在代码内动态的制定端口

2.如何在启动时测试端口占用

 

1.动态配置端口:

参考:https://segmentfault.com/a/1190000014610478

springboot 2.x 方法:Application类实现

WebServerFactoryCustomizer

接口,覆写customize方法

@Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        ((TomcatServletWebServerFactory)factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                //在这里设置端口号,使用setPort方法
                connector.setPort(8081);

                //一些自定义调整
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                protocol.setMaxConnections(200);
                protocol.setMaxThreads(200);
                protocol.setSelectorTimeout(3000);
                protocol.setSessionTimeout(3000);
                protocol.setConnectionTimeout(3000);
            }
        });
    }

 

2.测试端口占用,找寻可用端口

这个简单,参考这篇:https://blog.csdn.net/lcr_happy/article/details/73433508

 

最后结果:工具类

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

import static com.sobte.cqp.jcq.event.JcqApp.CQ;

public class ConnectorUtil {
    private void bindPort(String host, int port) throws Exception {
        Socket s = new Socket();
        s.bind(new InetSocketAddress(host, port));
        s.close();
    }
    public boolean isPortAvailable(int port) {
        Socket s = new Socket();
        try {
            bindPort("0.0.0.0", port);
            bindPort(InetAddress.getLocalHost().getHostAddress(), port);
            return true;
        } catch (Exception e) {
            CQ.logInfo("Port can't use!",""+port);
            return false;
        }
    }
    public int findAvailablePort(int start, int end){
        int s=startend?start:end;

        for (int i=s;i

Application类实现WebServerFactoryCustomizer接口,覆写customize方法

 @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        ((TomcatServletWebServerFactory)factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                //获取可用端口,指定端口范围,如果返回-1则范围内没有可用的,此时会使用80端口
                int port = new ConnectorUtil().findAvailablePort(8083,8084);
                try {
                    if(port<0)
                        throw new Exception("no available port !");
                    else
                        connector.setPort(port);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                //一些调优参数,别处抄来的
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                protocol.setMaxConnections(200);
                protocol.setMaxThreads(200);
                protocol.setSelectorTimeout(3000);
                protocol.setSessionTimeout(3000);
                protocol.setConnectionTimeout(3000);
            }
        });
    }

 

你可能感兴趣的:(SpringBoot踩坑笔记,javaWeb学习笔记)