赞
踩
公司微服务细分太多,最近跟我提说需要将几个微服务合为单体,经过几天的查阅,决定用二次打包的方式进行合并,然后部署的时候在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的合并。
<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> |
<!-- 将指定执行包解包到指定目录下 --> <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> |
<!-- 将解开的执行包与本工程代码合并打包 --> <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> |
<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包。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。