随着微服务架构的流行,想要启动一个微服务架构项目就要开启好多端口,有时候一台机器上部署的项目多的时候,端口资源就比较紧张了,其实有的微服务组件仅仅只是提供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. C#判断dataGridView1 点击的是哪一列上的按钮

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { ) { DataGr ...

  2. wpf listview images

    <ListView x:Name="lv"> <ListView.ItemsPanel> <ItemsPanelTemplate> <St ...

  3. 对Python中函数参数类型及排序问题,三个方面的总结

    Python中函数的参数问题有点复杂,主要是因为参数类型问题导致的情况比较多,下面来分析一下. 参数类型:缺省参数,关键字参数,不定长位置参数,不定长关键字参数. 其实总共可以分为 位置参数和关键字参 ...

  4. css精灵图&字体图标

    精灵图 为什么需要精灵图 为了有效的减少服务器接收和发送请求的次数,提高页面的加载速度.出现了CSS精灵技术 精灵图(sprites)的使用 精灵技术主要针对背景图片.就是把多个小背景图片整合到一张大 ...

  5. vue浏览器全屏实现

    1.项目中使用的是sreenfull插件,执行命令安装 npm install --save screenfull 2.安装好后,引入项目,用一个按钮进行控制即可,按钮方法如下: toggleFull ...

  6. webpack打包配置禁止html标签全部转为小写

    用webpack打包页面,发现html中特别写的用来给后端识别的大写标签全部被转为了小写标签,这时候需要将加一个配置 ,caseSensitive:true ,禁止大小写转换. webpack配置: ...

  7. JavaScript初探 四 (程序结构)

    JavaScript 结构 JavaScript 程序结构 JavaScript支持几乎和C语言一样的程序结构 分支结构 循环结构 分支结构 条件分支 if-else if语句:判断条件为true则执 ...

  8. QTextStream 读取文件乱码的解决办法

    通常都是编码转换的问题,注意如以下红字代码那样设置正确的编码 QFile _file(_f_path);    try{        if(_file.open(QIODevice::ReadOnl ...

  9. ADB常用命令(adb常用命令)

    基本用法 命令语法 adb 命令的基本语法如下: adb [-d|-e|-s <serialNumber>] <command> 如果只有一个设备/模拟器连接时,可以省略掉 [ ...

  10. MySQL通过SHOW TABLE STATUS查看库中所有表的具体信息

    有时候我们想看下指定库下所有表的使用情况,比如,查询表的Table大小,什么时候创建的,数据最近被更新的时间(即最近一笔insert/update/delete的时间).这些信息对我们进行库表维护很有 ...