java.net.BindException: Address already in use: JVM

java.net.BindException: Address already in use: JVM,第1张

启动多个springboot项目时,控制台总是打印Address already in use: JVM_Bind的警告,但项目又都是正常运行。根据控制台打印的这条警告信息,会发现都是在启用35729端口时出错。错误信息如下:

2022-04-24 17:16:23.279 [restartedMain] DEBUG o.s.b.d.a.OptionalLiveReloadServer - [startServer,61] - Live reload start error
java.net.BindException: Address already in use: JVM_Bind
	at java.net.DualStackPlainSocketImpl.bind0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
	at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
	at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
	at java.net.ServerSocket.bind(ServerSocket.java:375)
	at java.net.ServerSocket.(ServerSocket.java:237)
	at java.net.ServerSocket.(ServerSocket.java:128)
	at org.springframework.boot.devtools.livereload.LiveReloadServer.start(LiveReloadServer.java:113)
	at org.springframework.boot.devtools.autoconfigure.OptionalLiveReloadServer.startServer(OptionalLiveReloadServer.java:55)
	at org.springframework.boot.devtools.autoconfigure.OptionalLiveReloadServer.afterPropertiesSet(OptionalLiveReloadServer.java:48)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:509)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
	at org.springframework.context.event.AbstractApplicationEventMulticaster.retrieveApplicationListeners(AbstractApplicationEventMulticaster.java:239)
	at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:196)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:134)
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402)
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359)
	at springfox.documentation.spring.web.ObjectMapperConfigurer.fireObjectMapperConfiguredEvent(ObjectMapperConfigurer.java:90)
	at springfox.documentation.spring.web.ObjectMapperConfigurer.configureMessageConverters(ObjectMapperConfigurer.java:66)
	at springfox.documentation.spring.web.ObjectMapperConfigurer.postProcessBeforeInitialization(ObjectMapperConfigurer.java:47)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
	at com.standard.exam.StandardAdminApplication.main(StandardAdminApplication.java:35)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

根据报错信息查看LiveReloadServer.class源码,可以看到默认端口为DEFAULT_PORT = 35729。

LiveReloadServer.class源码

public class LiveReloadServer {
    public static final int DEFAULT_PORT = 35729;
    private static final Log logger = LogFactory.getLog(LiveReloadServer.class);
    private static final int READ_TIMEOUT;
    private final ExecutorService executor;
    private final List connections;
    private final Object monitor;
    private final int port;
    private final ThreadFactory threadFactory;
    private ServerSocket serverSocket;
    private Thread listenThread;

    public LiveReloadServer() {
        this(35729);
    }

    public LiveReloadServer(ThreadFactory threadFactory) {
        this(35729, threadFactory);
    }

    public LiveReloadServer(int port) {
        this(port, Thread::new);
    }

    public LiveReloadServer(int port, ThreadFactory threadFactory) {
        this.executor = Executors.newCachedThreadPool(new LiveReloadServer.WorkerThreadFactory());
        this.connections = new ArrayList();
        this.monitor = new Object();
        this.port = port;
        this.threadFactory = threadFactory;
    }

    public int start() throws IOException {
        synchronized(this.monitor) {
            Assert.state(!this.isStarted(), "Server already started");
            logger.debug("Starting live reload server on port " + this.port);
            this.serverSocket = new ServerSocket(this.port);
            int localPort = this.serverSocket.getLocalPort();
            this.listenThread = this.threadFactory.newThread(this::acceptConnections);
            this.listenThread.setDaemon(true);
            this.listenThread.setName("Live Reload Server");
            this.listenThread.start();
            return localPort;
        }
    }

    public boolean isStarted() {
        synchronized(this.monitor) {
            return this.listenThread != null;
        }
    }

    public int getPort() {
        return this.port;
    }

    private void acceptConnections() {
        do {
            try {
                Socket socket = this.serverSocket.accept();
                socket.setSoTimeout(READ_TIMEOUT);
                this.executor.execute(new LiveReloadServer.ConnectionHandler(socket));
            } catch (SocketTimeoutException var2) {
            } catch (Exception var3) {
                if (logger.isDebugEnabled()) {
                    logger.debug("LiveReload server error", var3);
                }
            }
        } while(!this.serverSocket.isClosed());

    }

    public void stop() throws IOException {
        synchronized(this.monitor) {
            if (this.listenThread != null) {
                this.closeAllConnections();

                try {
                    this.executor.shutdown();
                    this.executor.awaitTermination(1L, TimeUnit.MINUTES);
                } catch (InterruptedException var5) {
                    Thread.currentThread().interrupt();
                }

                this.serverSocket.close();

                try {
                    this.listenThread.join();
                } catch (InterruptedException var4) {
                    Thread.currentThread().interrupt();
                }

                this.listenThread = null;
                this.serverSocket = null;
            }

        }
    }

    private void closeAllConnections() throws IOException {
        synchronized(this.connections) {
            Iterator var2 = this.connections.iterator();

            while(var2.hasNext()) {
                Connection connection = (Connection)var2.next();
                connection.close();
            }

        }
    }

    public void triggerReload() {
        synchronized(this.monitor) {
            synchronized(this.connections) {
                Iterator var3 = this.connections.iterator();

                while(var3.hasNext()) {
                    Connection connection = (Connection)var3.next();

                    try {
                        connection.triggerReload();
                    } catch (Exception var8) {
                        logger.debug("Unable to send reload message", var8);
                    }
                }
            }

        }
    }

    private void addConnection(Connection connection) {
        synchronized(this.connections) {
            this.connections.add(connection);
        }
    }

    private void removeConnection(Connection connection) {
        synchronized(this.connections) {
            this.connections.remove(connection);
        }
    }

    protected Connection createConnection(Socket socket, InputStream inputStream, OutputStream outputStream) throws IOException {
        return new Connection(socket, inputStream, outputStream);
    }

    static {
        READ_TIMEOUT = (int)TimeUnit.SECONDS.toMillis(4L);
    }

    private static class WorkerThreadFactory implements ThreadFactory {
        private final AtomicInteger threadNumber;

        private WorkerThreadFactory() {
            this.threadNumber = new AtomicInteger(1);
        }

        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            thread.setName("Live Reload #" + this.threadNumber.getAndIncrement());
            return thread;
        }
    }

    private class ConnectionHandler implements Runnable {
        private final Socket socket;
        private final InputStream inputStream;

        ConnectionHandler(Socket socket) throws IOException {
            this.socket = socket;
            this.inputStream = socket.getInputStream();
        }

        public void run() {
            try {
                this.handle();
            } catch (ConnectionClosedException var2) {
                LiveReloadServer.logger.debug("LiveReload connection closed");
            } catch (Exception var3) {
                if (LiveReloadServer.logger.isDebugEnabled()) {
                    LiveReloadServer.logger.debug("LiveReload error", var3);
                }
            }

        }

        private void handle() throws Exception {
            try {
                try {
                    OutputStream outputStream = this.socket.getOutputStream();
                    Throwable var2 = null;

                    try {
                        Connection connection = LiveReloadServer.this.createConnection(this.socket, this.inputStream, outputStream);
                        this.runConnection(connection);
                    } catch (Throwable var27) {
                        var2 = var27;
                        throw var27;
                    } finally {
                        if (outputStream != null) {
                            if (var2 != null) {
                                try {
                                    outputStream.close();
                                } catch (Throwable var26) {
                                    var2.addSuppressed(var26);
                                }
                            } else {
                                outputStream.close();
                            }
                        }

                    }
                } finally {
                    this.inputStream.close();
                }
            } finally {
                this.socket.close();
            }

        }

        private void runConnection(Connection connection) throws IOException, Exception {
            try {
                LiveReloadServer.this.addConnection(connection);
                connection.run();
            } finally {
                LiveReloadServer.this.removeConnection(connection);
            }

        }
    }
}

原来是我的每个项目中都使用了热部署插件,所以,每个项目启动时都默认会使用35729端口。所以,只有第一个启动的项目,会成功运行热部署插件,其余项目均会出现这个端口启用失败的警告。

项目截图:

 

 

解决办法:
在application.propertie文件中配置热部署的端口(顺延就可),保证每个项目使用的端口不一致即可:
例:

spring.devtools.livereload.port=35730

spring.devtools.livereload.port=35731

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/729314.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-27
下一篇2022-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存