強(qiáng)大!Spring Boot全新模塊化管理方式
環(huán)境:SpringBoot3.2.5
1. 簡介
默認(rèn)情況下,Spring Boot項(xiàng)目會內(nèi)置一個(gè)Tomcat服務(wù)器,用于快速啟動(dòng)和運(yùn)行Web應(yīng)用程序。這一特性極大地簡化了開發(fā)和部署流程,開發(fā)者無需單獨(dú)配置和安裝Tomcat服務(wù)器。Spring Boot還提供了豐富的服務(wù)配置選項(xiàng),允許開發(fā)者根據(jù)需要調(diào)整Tomcat服務(wù)器的默認(rèn)行為,如端口,線程數(shù)等等,這極大地提升了開發(fā)效率。
當(dāng)我們在Spring Boot項(xiàng)目中引入Actuator后,并且進(jìn)行了如下的配置,你是否注意到啟動(dòng)的應(yīng)用有什么不同呢?
management:
server:
port: 7000
當(dāng)我們修改了Actuator端口后,啟動(dòng)服務(wù)后控制臺輸出如下:
圖片
應(yīng)用啟動(dòng)了2個(gè)Tomcat,所有關(guān)于Actuator的端點(diǎn),都將運(yùn)行在該7000端口的Tomcat服務(wù)上(/ac前綴的請求,我修改了默認(rèn)的前綴)。
開發(fā)中,我們可以通過 SpringApplicationBuilder 類實(shí)現(xiàn)啟動(dòng)多個(gè)Tomcat實(shí)例,將不同的服務(wù)(模塊)分別運(yùn)行在不同的Tomcat實(shí)例上,以實(shí)現(xiàn)資源隔離并共享主應(yīng)用的配置。
接下來,我們將詳細(xì)介紹如何在Spring Boot應(yīng)用中啟動(dòng)多個(gè)Tomcat實(shí)例,通過這些實(shí)例來管理不同的服務(wù)或模塊。
2. 實(shí)戰(zhàn)案例
我們將按照如下來建立我們的項(xiàng)目及模塊:
說明:
- 在上面我們定義了2個(gè)模塊分別是:com.pack.m1.*,com.pack.m2.* 。模塊中包含了當(dāng)前模塊的配置及接口定義等等。
- 父模塊com.pack.parent.*將作為我們的共享模塊,m1與m2共享該模塊。
- 針對不同的模塊,分別建立不同的配置文件(如上:m1.properties,m2.properties),這里的配置文件與Spring Boot默認(rèn)的application.[properties | yml]對等。
- 其它都于默認(rèn)的Spring Boot一致。
2.1 定義模塊
模塊1
配置類
@Configuration
@ComponentScan(basePackages = {"com.pack.m1"})
@PropertySource({"classpath:m1.properties"})
@EnableAutoConfiguration
public class M1Config {
}
啟動(dòng)了自動(dòng)配置,掃描的基包c(diǎn)om.pack.m1及對應(yīng)的配置文件,這里的配置文件就相當(dāng)于Spring Boot默認(rèn)的application.yml或properties。
定義測試接口
@RestController
@RequestMapping("/m")
public class M1Controller {
private final ParentService parentService ;
public M1Controller(ParentService parentService) {
this.parentService = parentService ;
}
@GetMapping("/index")
public Object m1() {
return "module 01 -" + this.parentService.query() ;
}
}
定義配置文件
# m1.properties
server.port=8075
server.servlet.context-path=/m1
這里的配置完全就是Spring Boot提供的那些配置,所以該配置文件你就當(dāng)是默認(rèn)的配置文件配置即可。
模塊2
模塊2與模塊1除了內(nèi)容不一樣外,其它都一致。
@Configuration
@ComponentScan(basePackages = {"com.pack.m2"})
@PropertySource({"classpath:m2.properties"})
@EnableAutoConfiguration
public class M2Config {
}
// 測試接口
@RestController
@RequestMapping("/m")
public class M1Controller {
private final ParentService parentService ;
public M2Controller(ParentService parentService) {
this.parentService = parentService ;
}
@GetMapping("/index")
public Object m2() {
return "module 02 - " + this.parentService.query() ;
}
}
// 配置文件m2.properties內(nèi)容
server.port=8076
server.servlet.context-path=/m2
這里為了區(qū)分所以為不同的模塊配置了不同的上下文。
按照如果的結(jié)構(gòu),不同的模塊內(nèi)容定義在對應(yīng)的m1,m2包下即可。
2.2 父模塊定義
由于父模塊是其它子模塊共享的,沒有什么特別的配置,我們就定義一個(gè)配置及測試接口即可。
@Service
public class ParentService {
public String query() {
return "我是父模塊內(nèi)容" ;
}
}
配置類
@Configuration
@ComponentScan(basePackages = {"com.pack.parent"})
public class ParentConfig {
}
這里我們并沒有配置啟用Web MVC所以,我們上面的ParentController類并不會生效。(注意:并不僅僅是啟用Web MVC那么簡單,還需要Spring適合的容器支持,這與下面我們將要看到的配置有關(guān))。
2.3 全局配置文件
不管你如何配置(除非你通過spring.config.name進(jìn)行了修改)默認(rèn)的application.yml或properties還是會生效了。默認(rèn)的文件內(nèi)容如下:
spring:
application:
name: springboot-parent-child
---
logging:
include-application-name: false
pattern:
dateformat: HH:mm:ss
如果你希望你的子模塊都能繼承某些配置,那么你可以將配置信息都寫入該公共的文件中。
2.4 啟動(dòng)類
最后一步就是啟動(dòng)類了,這時(shí)候就與之前的Spring Boot默認(rèn)的啟動(dòng)類完全不同了,內(nèi)容如下:
public class SpringbootParentChildApplication {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder() ;
builder
// 配置父容器的配置類(這一步內(nèi)部就將當(dāng)前容器設(shè)置為非web容器)
.parent(ParentConfig.class)
// 配置子容器對應(yīng)的配置及web應(yīng)用
.child(M1Config.class).web(WebApplicationType.SERVLET)
// 配置子容器(與上面的child相當(dāng)于兄弟容器),他們都有共同的父容器
// 在這一步將會創(chuàng)建父容器及上面child子容器
.sibling(M2Config.class).web(WebApplicationType.SERVLET)
// 而這里則會創(chuàng)建sibling設(shè)置子容器
.run(args) ;
}
}
在該啟動(dòng)類上我們沒有添加任何的注解(默認(rèn)你需要添加@SpringBootApplication)。
2.5 測試
啟動(dòng)服務(wù)后,控制臺日志輸入如下:
圖片
啟動(dòng)了2個(gè)Tomcat實(shí)例,分別運(yùn)行在8075和8076端口上。接下來訪問接口測試
圖片
模塊1
模塊2
兩個(gè)模塊都正常訪問接口及父模塊中的組件。