多微服务合并为一个服务

公司微服务细分太多,最近跟我提说需要将几个微服务合为单体,经过几天的查阅,决定用二次打包的方式进行合并,然后部署的时候在nginx改下合并的微服务转发路劲即可,不需要前端修改路劲了。

方案

采用二次打包的方式进行合并,利用maven-dependency-plugin解压插件先将各微服务的jar包解压再用maven-assembly-plugin进行合并打包为一个jar包。

合并前问题处理

1、由于包合并时,相同的类会进行覆盖,会导致找不到类等,所以存在相同包下的相同类名作用不一致的话,需要调整下各服务的包名、类

2、类注册的bean的name相同时,会导致注册bean失败,所以需要调整各服务有相同bean的name,特别是@FeignClient的contextId,因为是以contextId作为bean的name。

3、重复扫描导致重复注册等,如@EnableJpaAuditing,重复扫描,导致重复注册jpaAuditingHandler,所以在有用到@EnableJpaAuditing的类上也加上@ConditionalOnMissingBean(name="jpaAuditingHandler")条件,已经注册过的,就不需要再注册了

4、因为每个微服务都有yml配置文件,所以需要写个合并适配器,将每个微服务的yml合并一起,后续会另外开篇文件细说yml的合并。

合并步骤和代码

1、pom文件添加需要合并的微服务依赖

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <module1.version>6.1.0</module1.version> <module2.version>6.1.0</module2.version> <module3.version>6.1.0</module3.version> </properties> <dependencies> //需要整合的微服务包,type、scope需填写正确,其中groupId、artifactId、version按需填写即可 <dependency> <groupId>com.lfq.module1</groupId> <artifactId>module1</artifactId> <version>${module1.version}</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>com.lf1.module2</groupId> <artifactId>module2</artifactId> <version>${module2.version}</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>com.lf1.module3</groupId> <artifactId>module3</artifactId> <version>${module3.version}</version> <type>jar</type> <scope>provided</scope> </dependency> </dependencies> |

2、pom文件添加解压插件,将微服务解压到指定目录下

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <!-- 将指定执行包解包到指定目录下 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack-classes</id> <phase>package</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>com.lfq.module1</groupId> <artifactId>module1</artifactId> <outputDirectory>${project.build.directory}/work/addpack/module1</outputDirectory> </artifactItem> <artifactItem> <groupId>com.lfq.module2</groupId> <artifactId>module2</artifactId> <outputDirectory>${project.build.directory}/work/addpack/module2</outputDirectory> </artifactItem> <artifactItem> <groupId>com.lfq.module3</groupId> <artifactId>module3</artifactId> <outputDirectory>${project.build.directory}/work/addpack/module3</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> |

3、pom文件添加合并打包插件,将解压目录下的文件和本工程代码合并打包

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <!-- 将解开的执行包与本工程代码合并打包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <recompressZippedFiles>false</recompressZippedFiles> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <archive> <!-- 标红部分是合并后的执行包的启动类MANIFEST.MF文件,我这里选module1解压下的文件,按需配启动类 --> <manifestFile> ${project.build.directory}/work/addpack/module1/META-INF/MANIFEST.MF </manifestFile> </archive> <descriptors> <descriptor>assembly.xml</descriptor> <!-- 加载指定的assembly配置文件 --> </descriptors> </configuration> </execution> </executions> <!-- 标红部分是合并yml处理的包,如果需要合并yml文件,需自定义适配器对yml进行合并处理,再将依赖包添加进来 --> <!-- <dependencies> <dependency> <groupId>com.fql.merge</groupId> <artifactId>mergeHandle</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> --> </plugin> |

4、添加assembly.xml文件描述合并打包

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <assembly> <!-- id自定义 --> <id>lfq</id> <formats> <!-- 打为jar包 --> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <!-- 先将本工程内容输出 --> <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>BOOT-INF/classes</outputDirectory> </fileSet> <!-- 输出解压目录下的内容 --> <fileSet> <directory>${project.build.directory}/work/addpack/module1</directory> <outputDirectory>.</outputDirectory> <!-- 这里是module1下的DbDataController类不需要输出参与合并打包,即打包后的jar包没有DbDataController类,按需配置 --> <excludes> <exclude>**/DbDataController.class</exclude> </excludes> </fileSet> <fileSet> <directory>${project.build.directory}/work/addpack/module2</directory> <outputDirectory>.</outputDirectory> <!-- 这里是module2下的application.yml不需要输出参与合并打包,按需配置 --> <excludes> <exclude>**/application.yml</exclude> </excludes> </fileSet> <fileSet> <directory>${project.build.directory}/work/addpack/module3</directory> <outputDirectory>.</outputDirectory> </fileSet> </fileSets> <!--标红部分是对yml文件合并处理,如果没有实现,可去掉 --> <containerDescriptorHandlers> <containerDescriptorHandler> <handlerName>yml-merge</handlerName> <configuration> <filePattern>.*/application.yml</filePattern> <outputPath>BOOT-INF/classes/application.yml</outputPath> </configuration> </containerDescriptorHandler> </containerDescriptorHandlers> <!-- 本工程依赖 --> <dependencySets> <dependencySet> <unpack>false</unpack> <useProjectArtifact>false</useProjectArtifact> <outputDirectory>BOOT-INF/lib</outputDirectory> </dependencySet> </dependencySets> </assembly> |

后续执行clean install 即可得到一个jar包。

相关推荐
ChaITSimpleLove6 天前
云原生性能对决:JDK21虚拟线程+GraalVM vs .NET10/11 技术栈深度对比
微服务·云原生·serverless·graalvm native·native aot·runtime async·.net11/java21
EatFan7 天前
gRPC 为什么比 HTTP+JSON 快?详细讲解gRPC
后端·微服务·grpc
辛迪聊物业数字化8 天前
智慧物业管理平台架构实践:从SaaS物业云到商管系统的全模块落地
大数据·微服务·php
叶总没有会8 天前
Nacos—注册中心
spring cloud·微服务
天天喝旺仔8 天前
分布式服务容错实战:用 Sentinel 实现限流、熔断与降级
分布式·微服务·云原生·sentinel
周先生FullStack9 天前
Mac + 容器本地 MySQL/Redis 环境搭建与网络原理实战
java·spring boot·微服务·容器·架构·个人开发
叶总没有会9 天前
微服务--分布式基础
java·spring·spring cloud·微服务
weixin_435247069 天前
微服务开发规范模版
微服务·云原生
智慧物业老杨9 天前
物业数字化落地思考:真正的转型,是底层数据秩序的重构
java·大数据·人工智能·微服务·系统架构
VortMall9 天前
VortMall微服务商城系统 v1.3.20 正式发布:分销客户管理与后台安全双升级
微服务·商城系统·开源商城系统