spring boot 配置不占用端口方式启动

在配置文件中加入:

spring.main.web-application-type=none

不过这里有个点需要注意,如果配置成不占用端口的方式启动,若main方法执行完后,没其他的deamon线程在跑,应用就会自动关闭了,有些新同学最容易放这种错误,并且还不清楚错误在哪;

在使用阻塞线程时,这里也有个坑,有人使用System.in.read();进行阻塞,这种写法在window环境下是没问题的,但是在linux下会出现不阻塞的情况,具体可参考这篇文章:https://blog.csdn.net/zistrong/article/details/84758138

推荐写法:

@SpringBootApplication
@EnableScheduling
public class GameDataServerApplication implements CommandLineRunner{
    private static final Logger logger = LoggerFactory.getLogger(GameDataServerApplication.class);
    public static void main(String[] args) {
       SpringApplication.run(GameDataServerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {        //这里也可以添加一些业务处理方法,比如一些初始化参数等
        while(true){
            try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (InterruptedException e) {
                logger.error("oi进程意外结束",e);
            }
        }
    }
}

参考:

spring boot 不占用端口方式启动

你可能感兴趣的:(Java,Spring,java)