spring boot 不占用端口方式启动
随着微服务架构的流行,想要启动一个微服务架构项目就要开启好多端口,有时候一台机器上部署的项目多的时候,端口资源就比较紧张了,其实有的微服务组件仅仅只是提供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 不占用端口方式启动的更多相关文章
- spring boot 以jar的方式启动常用shell脚本
用spring boot框架做的项目,将第三方包全部打在jar里面,通过shell脚本启动和停止服务,常用的shell脚本模板如下: #!/bin/bashJAVA_OPTIONS_INITIAL=- ...
- Spring boot 整合hive-jdbc导致无法启动的问题
使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: 经过排查,是maven的包冲突引起的,具体做法,排除:jetty-all.hive-shims依赖包.对应的po ...
- 使用Spring boot整合Hive,在启动Spring boot项目时,报错
使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: java.lang.NoSuchMethodError: org.eclipse.jetty.servlet.S ...
- spring boot不要放在tomcat下启动,因为自身就带了集成tomcat
spring boot不要放在tomcat下启动,因为自身就带了集成tomcat
- Spring Boot 以 jar 包方式运行在后台
spring-boot jar 包方式启动: 首先,为了防止和常用的 Tomcat 8080 端口冲突,将 Spring-boot 项目的端口号设置为 9090. 具体方法:在 application ...
- Spring Boot应用的启动和停止(Spring Boot应用通过start命令启动)
Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行.产品级别的基于S ...
- spring boot 服务 正确关闭方式
引言 Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行.产品级别的 ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- Spring Boot2.0之注解方式启动Springmvc
回顾下springmvc原理图: DispatcherServlet是Spring MVC的核心,每当应用接受一个HTTP请求,由DispatcherServlet负责将请求分发给应用的其他组件. 在 ...
随机推荐
- Element类
ElementTree API主要包含在Element类中,ElementTree API就是XML树相关的函数 追加子节点有两种方式,一种是使用append(),另一种是使用SubElement() ...
- IT兄弟连 Java语法教程 流程控制语句 循环结构语句2
双重for循环 如果把一个循环放在另一个循环体中,那么就可以形成嵌套循环,也就是双重for循环,当然嵌套循环也可以是for循环嵌套while循环,也可以是while循环嵌套while循环……,即各种类 ...
- rpmrebuild 下载安装
下载 https://jaist.dl.sourceforge.net/project/rpmrebuild/rpmrebuild/2.14/rpmrebuild-2.14.tar.gz 安装 将其做 ...
- 【mysql】You must reset your password using ALTER USER statement before executing this statement. 报错处理
1.问题:登陆mysql以后,不管运行任何命令,总是提示这个 mysql> select user,authentication from mysql.user; ERROR 1820 (HY0 ...
- Git分布式版本控制器使用
前言: 使用Git版本控制器差不多有一年多的时间了,在这一年多的时间里对这个传说的的分布式版本控制工具有了一定的了解.在实战项目开发中,对关于如何在通过Git提交项目,以及如何使用Git命令对提交的文 ...
- IDA中查看某函数引用问题
按X键,即可列出哪个部分引用该函数.
- 爬取bilibili首页菜单获取li内容
代码: import requests from bs4 import BeautifulSoup def html_save(s): with open('哔哩哔哩.csv','a')as f: f ...
- 死磕 java同步系列之ReentrantLock VS synchronized——结果可能跟你想的不一样
问题 (1)ReentrantLock有哪些优点? (2)ReentrantLock有哪些缺点? (3)ReentrantLock是否可以完全替代synchronized? 简介 synchroniz ...
- 2019-9-23-win10-uwp-睡眠唤醒
原文:2019-9-23-win10-uwp-睡眠唤醒 title author date CreateTime categories win10 uwp 睡眠唤醒 lindexi 2019-9-23 ...
- \" 转义字符, \a系统警报,逐字字符串(verbatim string)
string str="The key factors are \"focus\" and \"perseverance\""; strin ...