随着微服务架构的流行,想要启动一个微服务架构项目就要开启好多端口,有时候一台机器上部署的项目多的时候,端口资源就比较紧张了,其实有的微服务组件仅仅只是提供RPC服务,可以不用占用web启动的端口,此时spring boot 不占用web端口的方式就派上用场了,但是spring boot 1.x与spring boot 2.x的配置是有区别的,在使用时一定要注意一下自己所使用的版本

spirngboot 2.x之前(代码方式实现):

 @SpringBootApplication
public class Application { public static void main(String[] args) {
new SpringApplicationBuilder().sources(Application.class).web(false).run(args);
}
}

spinrboot 2.x之前(另外一种代码实现方式)

 @Configuration
@EnableAutoConfiguration
public class MyClass{
public static void main(String[] args) throws JAXBException {
SpringApplication app = new SpringApplication(MyClass.class);
app.setWebEnvironment(false);
ConfigurableApplicationContext ctx = app.run(args);
}
}

spinrboot 2.x之前(配置方式)

spring.main.web-environment=false

springboot 2.x之后(代码方式)

 @SpringBootApplication
public class MyApplication { public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
.run(args);
}
}

springboot 2.x之后(配置方式)

spring.main.web-application-type=none

  

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

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

推荐写法:

/**
* @Description: TODO
* @Author Mr.huang
* @Date 2019/10/28 0028
* @Version V1.0
**/
@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 不占用端口方式启动的更多相关文章

  1. spring boot 以jar的方式启动常用shell脚本

    用spring boot框架做的项目,将第三方包全部打在jar里面,通过shell脚本启动和停止服务,常用的shell脚本模板如下: #!/bin/bashJAVA_OPTIONS_INITIAL=- ...

  2. Spring boot 整合hive-jdbc导致无法启动的问题

    使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: 经过排查,是maven的包冲突引起的,具体做法,排除:jetty-all.hive-shims依赖包.对应的po ...

  3. 使用Spring boot整合Hive,在启动Spring boot项目时,报错

    使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: java.lang.NoSuchMethodError: org.eclipse.jetty.servlet.S ...

  4. spring boot不要放在tomcat下启动,因为自身就带了集成tomcat

    spring boot不要放在tomcat下启动,因为自身就带了集成tomcat

  5. Spring Boot 以 jar 包方式运行在后台

    spring-boot jar 包方式启动: 首先,为了防止和常用的 Tomcat 8080 端口冲突,将 Spring-boot 项目的端口号设置为 9090. 具体方法:在 application ...

  6. Spring Boot应用的启动和停止(Spring Boot应用通过start命令启动)

    Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行.产品级别的基于S ...

  7. spring boot 服务 正确关闭方式

    引言 Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行.产品级别的 ...

  8. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

  9. Spring Boot2.0之注解方式启动Springmvc

    回顾下springmvc原理图: DispatcherServlet是Spring MVC的核心,每当应用接受一个HTTP请求,由DispatcherServlet负责将请求分发给应用的其他组件. 在 ...

随机推荐

  1. Linux 下编写一个 PHP 扩展

        假设需求 开发一个叫做 helloWord 的扩展. 扩展里有一个函数,helloWord(). echo helloWord('Tom'); //返回:Hello World: Tom 本地 ...

  2. sql慢查询工具(配置代码)

    # 在mysql的配置文件/etc/mysql/mysql.conf.d/mysqld.cnf[mysqld]中配置懒查询 slow_query_log = ON # 是否已经开启慢查询 long_q ...

  3. Java8新特性——集合底层源码实现的改变

    ArrayList 源码分析: jdk7: ArrayList list = new ArrayList();//初始化一个长度为10的Object[] elementData sysout(list ...

  4. 【maven】【idea】使用idea的maven进行deploy操作失败,报错:Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project proengine-db-sdk: Failed to deploy artifacts 错误码401

    使用idea的maven进行deploy操作失败,报错: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:- f ...

  5. nginx目录遍历漏洞复现

    nginx目录遍历漏洞复现 一.漏洞描述 Nginx的目录遍历与apache一样,属于配置方面的问题,错误的配置可导致目录遍历与源码泄露. 二.漏洞原理 1. 修改nginx.conf,在如下图位置添 ...

  6. python基础(31):进程(一)

    1. 什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行 ...

  7. java基础(22):File、递归

    1. File 1.1 IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序 ...

  8. LinuxShell脚本——循环结构

    LinuxShell脚本——循环结构 摘要:本文主要学习了Shell脚本中的循环结构. while循环 基本语法 while循环是最简单的一种循环,如果条件满足则执行循环里的语句,如果条件不满足则退出 ...

  9. Java生鲜电商平台-App系统架构开发与设计

    Java生鲜电商平台-App系统架构开发与设计 说明:阅读此文,你可以学习到以下的技术分享 1.Java生鲜电商平台-App架构设计经验谈:接口的设计2.Java生鲜电商平台-App架构设计经验谈:技 ...

  10. C# Stocket

    介绍 1.TCP/IP(Transmission Control Protocol/Internet Protocol) 即传输控制协议/网间协议,是一个工业标准的协议集,它是为广域网(WANs)设计 ...