构建多服务 springboot
问题背景
如何在一套系统系统中开启多个端口面向不同服务对象
当然,这里说的不是 springboot 多个监听端口的启动,而是在一套其他启动多个 springboot,分别运行在不同端口,各自有不同的逻辑
介绍
下面我们来看 org.springframework.boot.builder 中的 SpringApplicationBuilder, 这里为我们提供了这种实现方法
Builder for SpringApplication and ApplicationContext instances with convenient fluent API and context hierarchy support. Simple example of a context hierarchy:
new SpringApplicationBuilder(ParentConfig.class).child(ChildConfig.class).run(args);Another common use case is setting active profiles and default properties to set up the environment for an application:
new SpringApplicationBuilder(Application.class).profiles(“server”)
.properties(“transport=local”).run(args);If your needs are simpler, consider using the static convenience methods in SpringApplication instead.
SpringApplication 和 ApplicationContext 实例的构建器,具有便利的流利的 API 和上下文层次结构支持。 上下文层次结构的简单示例
这样的多启动器是具有上下文的,可以定义一个无 web 的父 springboot,以及多个子 springboot
比如
启动器配置
1 | fun main(args: Array<String>) { |
WebApplicationType 是一个枚举类,有三个可选项
1 |
|
多个 SpringApplication 实例
CoreApplication 父
1 |
|
WebApplication(子 1)
1 |
|
application-web.properties
1 | server.port=8080 |
WorkApplication(子 2)
1 |
|
application-work.properties
1 | server.port=12563 |
文件结构图
项目结构图

配置文件结构图

两个子 SpringApplication 是同级关系,继承于主的 yml 配置
- 标题: 构建多服务 springboot
- 作者: tsvico
- 创建于 : 2021-05-19 15:08:44
- 更新于 : 2022-05-22 16:05:38
- 链接: https://blog.tbox.fun/2021/3232640826.html
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。

