JAVA多服务整合为单体多模块项目

张开发
2026/4/15 8:41:10 15 分钟阅读

分享文章

JAVA多服务整合为单体多模块项目
1.项目背景本篇背景为在写APP项目的时候因为多数情况下模块功能不相互依赖的原因导致催生出了多个后台服务 如登录服务以及各个前台模块的对应的后台服务在表面看有一些微服务的概念但是因为用户量并不多的原因反而导致了后台维护起来麻烦的结果。在此状况下考虑将之前的多服务进行收敛整合为一个后台多模块的服务。借助多模块的优势减少维护程度节省服务器资源。2.后台 Maven 目录结构具体目录结构如下图所示只能方便展示一些公共模块具体 Maven 目录可以了解出来。3.父模块 pom 相关配置在父模块中并没有进行相关的实际依赖的引入而是更多的去进行子模块的项目依赖版本的管理以及 Maven 插件的相关配置主要的配置部分代码如下。!-- 父模块基础配置-- modelVersion4.0.0/modelVersion groupIdcom.**/groupId artifactId**/artifactId version0.0.1-SNAPSHOT/version name**/name description**/description packagingpom/packaging!-- 子模块相关引用-- modules module**-appserver/module module**-common/module module**-globalmoduleconfig/module /modules!-- 部分相关jar包版本控制-- properties java.version1.8/java.version project.build.sourceEncodingUTF-8/project.build.sourceEncoding project.reporting.outputEncodingUTF-8/project.reporting.outputEncoding /properties!-- 部分jar 包管理-- dependencyManagement dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-dependencies/artifactId version${spring-boot.version}/version typepom/type scopeimport/scope /dependency dependency groupIdorg.mapstruct/groupId artifactIdmapstruct/artifactId version${mapstruct-version}/version /dependency dependency groupIdcommons-beanutils/groupId artifactIdcommons-beanutils/artifactId version${beanutils-version}/version /dependency /dependencyManagement!-- Maven 插件相关配置-- build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId version3.8.1/version !-- 添加版本号 -- configuration source${java.version}/source target${java.version}/target parameterstrue/parameters annotationProcessorPaths path groupIdorg.mapstruct/groupId artifactIdmapstruct-processor/artifactId version${mapstruct-version}/version /path path groupIdorg.projectlombok/groupId artifactIdlombok/artifactId version${lombok-version}/version /path path groupIdorg.projectlombok/groupId artifactIdlombok-mapstruct-binding/artifactId version0.2.0/version /path /annotationProcessorPaths /configuration /plugin plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId version${spring-boot.version}/version configuration skipfalse/skip /configuration executions execution goals goalrepackage/goal /goals /execution /executions /plugin /plugins /build4.入口模块主模块 pom 相关配置在实际的项目中通常会对不同的环境进行不同的参数配置如开发环境测试环境生产环境等在这里 Profies 的选择是使用了 Maven 的 Profies 工具没有去选择使用 Spring 的 Profies。部分配置如下。!-- 主模块基础配置-- modelVersion4.0.0/modelVersion groupIdcom.**/groupId artifactId**-inputApp/artifactId version0.0.1-SNAPSHOT/version name**/name description**/description !-- 因为是主模块打包方式选择jar包-- packagingjar/packaging!-- 父模块引用-- parent groupIdcom.**/groupId artifactId**/artifactId version0.0.1-SNAPSHOT/version /parent!-- 部分 jar包引用-- dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdcom.**/groupId artifactId**-globalmoduleconfig/artifactId version0.0.1-SNAPSHOT/version /dependency /dependencies !-- Maven profiles 相关配置-- profiles profile idtest/id properties propssrc/main/filters/test.properties/props /properties /profile profile iddev/id activation activeByDefaulttrue/activeByDefault /activation properties propssrc/main/filters/dev.properties/props /properties /profile profile idprod/id properties propssrc/main/filters/prod.properties/props /properties /profile /profiles build filters filter${props}/filter /filters resources resource directorysrc/main/resources/directory filteringtrue/filtering /resource /resources /build5.全局配置模块子模块配置在全局配置模块中通常对整个项目进行一些全局的配置如 全局异常配置全局参数校验器全局异常日志记录全局权限过滤器等。此处仅展示全局模块的 pom 文件配置针对全局的项目配置后续会有。!-- 全局模块基础配置-- modelVersion4.0.0/modelVersion groupIdcom.*/groupId artifactId*-globalmoduleconfig/artifactId version0.0.1-SNAPSHOT/version nameglobalModuleConfig/name descriptionglobalModuleConfig/description!-- 父模块依赖-- parent groupIdcom.*/groupId artifactId*/artifactId version0.0.1-SNAPSHOT/version /parent!--全局模块构建插件-- build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId configuration skiptrue/skip /configuration /plugin /plugins /build在子模块中因为没有主类的缘故所以要在子模块构建的时候要使用 Maven 插件进行子模块中主类程序的跳过这也是为什么能够把多服务整合成多模块项目的主要原因后续的子模块都需要相关的配置才能达到多服务整合为单体项目多模块的效果这里就不再展示其他子模块的配置因为除了模块对应所需依赖其余重要的配置都是一样的。6.结尾Java 的多模块项目大致为这些重要的就是各个模块的相关 Maven 插件配置后续会展示一些在一个项目中常用且好用的技术 如 profiles 项目的全局管理日志等。

更多文章