关于java:使用SOFABootSOFAArk进行模块化开发

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
      
  • 模块化开发相干文档:

    • JVM 服务公布与援用:模块隔离后如何相干调用
    • 蚂蚁金服的业务零碎模块化之模块化隔离计划
    • 基于 SOFABoot 进行模块化开发

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包

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理