多微服务合并为一个服务

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

相关推荐
茶馆大橘7 小时前
微服务系列五:避免雪崩问题的限流、隔离、熔断措施
java·jmeter·spring cloud·微服务·云原生·架构·sentinel
coding侠客8 小时前
揭秘!微服务架构下,Apollo 配置中心凭啥扮演关键角色?
微服务·云原生·架构
lexusv8ls600h9 小时前
微服务设计模式 - 网关路由模式(Gateway Routing Pattern)
spring boot·微服务·设计模式
码农爱java13 小时前
Kafka 之消息并发消费
spring boot·微服务·kafka·mq·消息中间件·并发消费
Flamesky13 小时前
dotnet core微服务框架Jimu ~ 会员注册微服务
微服务·services·micro
为美好的生活献上中指15 小时前
Java学习Day60:微服务总结!(有经处无火,无火处无经)
java·spring boot·spring cloud·微服务·sentinel·jetty
猫猫不是喵喵.15 小时前
【微服务】Docker 容器化
java·docker·微服务
诗这样的18 小时前
【需求变更】使用 Redis 和 Lua 脚本实现变更后方案编号的生成
java·redis·缓存·微服务·lua·需求分析
白总Server1 天前
OpenHarmony
后端·spring cloud·华为·微服务·ribbon·架构·系统架构