多微服务合并为一个服务

公司微服务细分太多,最近跟我提说需要将几个微服务合为单体,经过几天的查阅,决定用二次打包的方式进行合并,然后部署的时候在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包。

相关推荐
yours_Gabriel13 小时前
【SpringBoot问题】IDEA中用Service窗口展示所有服务及端口的办法
java·微服务·springboot
wclass-zhengge15 小时前
02微服务系统与设计(D1_走出微服务误区:避免从单体到分布式单体)
分布式·微服务·架构
黄俊懿19 小时前
【深入理解SpringCloud微服务】Sentinel功能详解
后端·spring·spring cloud·微服务·中间件·架构·sentinel
运维&陈同学19 小时前
【zookeeper04】消息队列与微服务之zookeeper客户端访问
linux·后端·微服务·zookeeper·云原生·消息队列·云计算
petaexpress20 小时前
k8s微服务架构就是云原生吗?两者是什么关系
微服务·云原生·架构·kubernetes·k8s
HuaLuLemon20 小时前
03-微服务搭建
java·微服务·gulimall
喵叔哟20 小时前
18. 【.NET 8 实战--孢子记账--从单体到微服务】--记账模块--账本
微服务·架构·.net
BestandW1shEs20 小时前
谈谈微服务的常用组件
微服务·架构
猫猫不是喵喵.1 天前
分布式搜索引擎Elasticsearch(一)
分布式·搜索引擎·微服务
.Ayang1 天前
微服务介绍
网络·安全·网络安全·微服务·云原生·架构·安全架构