非web工程

在服务架构中,有些springboot工程只是简单的作为服务,并不提供web服务

这个时候不需要依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

但是启动springboot的话,启动之后就会自动关闭,可以通过如下方式解决

实现CommandLineRunner,重写run方法即可,这样启动后就不会关闭

@SpringBootApplication
@EnableDubbo
public class SeaProviderLogApplication implements CommandLineRunner { public static void main(String[] args) {
SpringApplication.run(SeaProviderLogApplication.class, args);
} @Override
public void run(String... args) throws Exception {
System.out.println("SeaProviderLogApplication正在启动。。。");
while(true) {
Thread.sleep(600000000);
System.out.println("sleep....");
}
}
}

有人可能会说,引入spring-boot-starter-web主要是为了方便测试,其实完全可以使用单元测试进行操作

使用@SpringBootTest@RunWith(SpringRunner.class)注解即可进行单元测试代码如下

@SpringBootTest
@RunWith(SpringRunner.class)
public class IndexControllerTest { @Reference(version = "1.0.1")
private ErrorLogService errorLogService; @Test
public void bbb() {
ErrorLog errorLog = new ErrorLog();
errorLog.setName("error");
System.out.println(errorLogService.sendMsg(errorLog));
}
}

web工程

但是有时候由于maven聚合工程,会依赖common或者parent,会自然的引入了

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

这个时候启动的话,默认端口是,当然是可以在application.properties中配置

server.port=8081 来进行修改,但是比较麻烦,因为本就不暴露http请求,没必要添加spring-boot-starter-web依赖,服务多的话也端口设置也让人头疼,会产生端口占用问题

由于不提供web服务,属实没必要暴露端口,可以通过如下两种方式进行启动不设置端口号

第一种:

修改application配置文件

spring:
main:
allow-bean-definition-overriding: true
web-application-type: none

第二种:

修改启动入口

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

OK,完美解决,再也不用考虑端口分配问题了

springboot整合dubbo可以参考 springboot2.x纯注解整合dubbo

springboot启动不设置端口的更多相关文章

  1. SpringBoot 启动参数设置环境变量、JVM参数、tomcat远程调试

    java命令的模版:java [-options] -jar jarfile [args...] 先贴一下我的简单的启动命令: java -Xms128m -Xmx256m -Xdebug -Xrun ...

  2. tomcat端口修改以及jvm启动参数设置

    1.端口更改:找到config目录下server.xml文件 如下 <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to t ...

  3. mac启动springboot失败,8080端口被占用,mac命令行关闭端口

    如下图,idea启动springboot失败,8080端口被占用 Error starting ApplicationContext. To display the conditions report ...

  4. SpringBoot学习笔记(八):SpringBoot启动端口+访问路、SpringBoot配置文件yml、SpringBoot多环境区分、SpringBoot打包发布

    SpringBoot启动端口+访问路径 配置文件: server.port=9090 server.context-path=/springboot 现在只能用http://127.0.0.1:909 ...

  5. SpringBoot(三):springboot启动参数

    springboot默认启动入口函数是支持接收参数,并且在整个应用程序内部也可以获取到这些参数,并且如果传递的参数是一些内部定义的参数将会被映射到springboot内部配置项,从而达到配置效果. s ...

  6. SpringBoot启动原理及相关流程

    一.springboot启动原理及相关流程概览 springboot是基于spring的新型的轻量级框架,最厉害的地方当属自动配置.那我们就可以根据启动流程和相关原理来看看,如何实现传奇的自动配置 二 ...

  7. (四)SpringBoot启动过程的分析-预处理ApplicationContext

    -- 以下内容均基于2.1.8.RELEASE版本 紧接着上一篇(三)SpringBoot启动过程的分析-创建应用程序上下文,本文将分析上下文创建完毕之后的下一步操作:预处理上下文容器. 预处理上下文 ...

  8. 从SpringBoot启动,阅读源码设计

    目录 一.背景说明 二.SpringBoot工程 三.应用上下文 四.资源加载 五.应用环境 六.Bean对象 七.Tomcat服务 八.事件模型 九.配置加载 十.数据库集成 十一.参考源码 服务启 ...

  9. Springboot启动源码详解

    我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static voi ...

随机推荐

  1. QML被系统缓存的原理是比较时间戳

    Gunnar Roth January 25, 2017 at 17:07 Afaik the cached qml file contains a checksum of the original ...

  2. Delphi xe5调用外部扫描程序——谷歌 zxing

    unit uZXing; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Va ...

  3. 微信公众号开发系列-13、基于RDIFramework.NET框架整合微信开发应用效果展示

    1.前言 通过前面一系列文章的学习,我们对微信公众号开发已经有了一个比较深入和全面的了解. 微信公众号开发为企业解决那些问题呢? 我们经常看到微信公众号定制开发.微信公众平台定制开发,都不知道这些能给 ...

  4. netcore mvc快速开发系统(菜单,角色,权限[精确到按钮])开源

    AntMgr https://github.com/yuzd/AntMgr 基于netcore2.0 mvc 开发的 快速搭建具有如下特色的后台管理系统 特色: 用户管理 菜单管理 角色管理 权限管理 ...

  5. 基于 Roslyn 实现动态编译

    基于 Roslyn 实现动态编译 Intro 之前做的一个数据库小工具可以支持根据 Model 代码文件生成创建表的 sql 语句,原来是基于 CodeDom 实现的,最近改成使用基于 Roslyn ...

  6. 你需要了解的HTTP协议

    了解HTTP协议 HTTP (超文本传输协议,HyperText Transfer Protocol),是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP 是万维网的数据通信基础. 通常, ...

  7. Enter passphrase

    提示“Enter passphrase for key /root/.ssh/id_rsa.pub”让输入私钥,可不论输与不输都不能直接登录 解决方法: 在本地执行: eval `ssh-agent` ...

  8. pybind11在Windows下的使用

      Pybind11算是目前最方便的Python调用C++的工具了, 介绍一下在vs2019上写Python的扩展的HelloWorld   1. 去下载pybind11 https://github ...

  9. C#简单的连接数据库

  10. 常用的方法论-5why