使用SOFABoot+SOFAArk进行模块化开发
文章目录
Demo地址
SOFAArk相关
- Ark包 = Ark容器 + Ark插件 + Biz包
- Ark包:可以理解为宿主应用,打包后生成的可执行Jar包,jar包后缀为
-ark-executable.jar - Biz包:每个模块都是一个biz包,biz包的后缀为
-ark-biz.jar,biz包可以直接在已有的Ark容器中安装(现在通过API的方式) API:ArkClient包含的针对Biz包的安装、卸载、切换版本功能,demo中提供了三个接口对其进行操作。
@RequestMapping(value = "/installBiz", method = RequestMethod.POST) public void installBiz(String url) throws Throwable { ArkClient.installBiz(new File(url)); } @RequestMapping(value = "/uninstallBiz", method = RequestMethod.POST) public void uninstallBiz(String bizName, String bizVersion) throws Throwable { ArkClient.uninstallBiz(bizName, bizVersion); } @RequestMapping(value = "/switchBiz", method = RequestMethod.POST) public void switchBiz(String bizName, String bizVersion) { ArkClient.switchBiz(bizName, bizVersion); }如果是Controller层为一个Biz包,那么如果需要发布更新,则需要停止该Biz包的Spring服务后再调用installBiz方法。
SOFAArk配置:
- 在项目的整体目录下需要创建conf/ark文件夹
- conf/ark/bootstrap.properties 是 SOFAArk 容器默认启动配置文件(官方文档)
多模块开发中配置文件中必须指定宿主应用
com.alipay.sofa.ark.master.biz=master-module
模块化开发相关文档:
Maven配置
在工程主
pom.xml中,修改parent为<parent> <groupId>com.alipay.sofa</groupId> <artifactId>sofaboot-dependencies</artifactId> <version>3.2.2</version> <relativePath/> <!-- lookup parent from repository --> </parent>宿主应用
pom.xml中,添加依赖<dependency> <groupId>com.alipay.sofa</groupId> <artifactId>runtime-sofa-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alipay.sofa</groupId> <artifactId>sofa-ark-springboot-starter</artifactId> </dependency> <dependency> <groupId>com.alipay.sofa</groupId> <artifactId>web-ark-plugin</artifactId> </dependency>添加打包插件
<build> <plugins> <!-- 这里配置动态模块打包插件 --> <plugin> <groupId>com.alipay.sofa</groupId> <artifactId>sofa-ark-maven-plugin</artifactId> <executions> <execution> <id>default-cli</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> <configuration> <priority>100</priority> <baseDir>../</baseDir> <bizName>demo</bizName> </configuration> </plugin> </plugins> </build>业务模块
pom.xml中,添加依赖<dependency> <groupId>com.alipay.sofa</groupId> <artifactId>sofa-ark-springboot-starter</artifactId> </dependency>添加打包插件(如果是Controller层的模块,需要指定webContextPath)
<build> <plugins> <!--这里添加ark 打包插件--> <plugin> <groupId>com.alipay.sofa</groupId> <artifactId>sofa-ark-maven-plugin</artifactId> <executions> <execution> <!--goal executed to generate executable-ark-jar --> <goals> <goal>repackage</goal> </goals> <!--ark-biz 包的打包配置 --> <configuration> <!--是否打包、安装和发布 ark biz,详细参考 Ark Biz 文档,默认为false--> <attach>true</attach> <!--ark 包和 ark biz 的打包存放目录,默认为工程 build 目录--> <outputDirectory>target</outputDirectory> <!--default none--> <arkClassifier>executable-ark</arkClassifier> <!-- ark-biz 包的启动优先级,值越小,优先级越高--> <priority>200</priority> <!--设置应用的根目录,用于读取 ${base.dir}/conf/ark/bootstrap.application 配置文件,默认为 ${project.basedir}--> <baseDir>../</baseDir><!--针对 Web 应用,设置 context path,默认为 /--> <webContextPath>/one</webContextPath> </configuration> </execution> </executions> </plugin> </plugins> </build>公共Service实现类的模块,需要配置如下打包插件
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!--这里添加ark 打包插件--> <plugin> <groupId>com.alipay.sofa</groupId> <artifactId>sofa-ark-maven-plugin</artifactId> <executions> <execution> <!--goal executed to generate executable-ark-jar --> <goals> <goal>repackage</goal> </goals> <!--ark-biz 包的打包配置 --> <configuration> <!--是否打包、安装和发布 ark biz,详细参考 Ark Biz 文档,默认为false--> <attach>true</attach> <!--ark 包和 ark biz 的打包存放目录,默认为工程 build 目录--> <outputDirectory>target</outputDirectory> <!--default none--> <arkClassifier>executable-ark</arkClassifier> <!-- ark-biz 包的启动优先级,值越小,优先级越高--> <priority>200</priority> <!--设置应用的根目录,用于读取 ${base.dir}/conf/ark/bootstrap.application 配置文件,默认为 ${project.basedir}--> <baseDir>../</baseDir> </configuration> </execution> </executions> </plugin> </plugins> </build>
如何开发?
通过SOFA容器注入Service
- 接口实现类上必须添加
@SofaService注解 - 在注入时需要使用
@SofaReference注解注入,而不是使用@Autowired或[@Resource](https://my.oschina.net/u/929718) - 业务模块(mng)需要引入Service接口模块,不需要在pom中引入Service实现模块,Service实现类所在模块需要在SOFA容器中启动
- Service实现模块(provider)和业务模块(mng)的包名一致(如果配置spring扫描包的位置可能不需要一致,但我没尝试成功)
- 接口实现类上必须添加
- 业务模块自身的逻辑按照正常的开发即可
在IDE中开发时,需要在业务模块(mng)的
pom.xml中引入Service实现模块(provider)后,即可直接启动mng模块进行开发。<dependency> <groupId>io.sofastack</groupId> <artifactId>dynamic-provider</artifactId> <version>2.0.0</version> </dependency>
如何发版?
宿主应用的
pom.xml中引入公共Service实现类模块,该模块可以在不重启宿主应用的情况下更新代码。例如:<dependency> <groupId>io.sofastack</groupId> <artifactId>dynamic-provider</artifactId> <version>2.0.0</version> <classifier>ark-biz</classifier> </dependency>Service实现类模块发版
pom.xml中修改version版本,例如:1.0.0修改为2.0.0- 使用maven的clean package打包,打包后的
...biz.jar文件即为Biz包 - 调用
installBiz接口安装此2.0.0的biz包 - 安装成功后,调用
switchBiz接口切换至2.0.0版本即可 - 如果旧版本不需要使用,则可以调用
uninstallBiz接口卸载对应版本
Controller模块发版
- 使用maven的clean package打包,打包后的
...biz.jar文件即为Biz包 - 调用停止Spring Boot服务的方法,停止当前模块的服务
- 调用
installBiz接口安装新的biz包
- 使用maven的clean package打包,打包后的

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论