在 SOFABoot 工程中使用类隔离能力,只需两步操作;配置 打包插件以及引入 sofa-ark-springboot-starter 类隔离框架依赖;

官方提供了 Maven 插件 - sofa-ark-maven-plugin ,只需要简单的配置项,即可将 SpringBoot 工程打包成标准格式规范的可执行 Ark 包,插件坐标为:

配置模板如下:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>com.alipay.sofa</groupId>
  5. <artifactId>sofa-ark-maven-plugin</artifactId>
  6. <executions>
  7. <execution>
  8. <id>default-cli</id>
  9. <!--goal executed to generate executable-ark-jar -->
  10. <goals>
  11. <goal>repackage</goal>
  12. </goals>
  13. <configuration>
  14. <!--specify destination where executable-ark-jar will be saved, default saved to ${project.build.directory}-->
  15. <outputDirectory>./target</outputDirectory>
  16. <!--default none-->
  17. <arkClassifier>executable-ark</arkClassifier>
  18. <!-- all class exported by ark plugin would be resolved by ark biz in default, if
  19. configure denyImportClasses, then it would prefer to load them by ark biz itself -->
  20. <denyImportClasses>
  21. <class>com.alipay.sofa.SampleClass1</class>
  22. <class>com.alipay.sofa.SampleClass2</class>
  23. </denyImportClasses>
  24. <!-- Corresponding to denyImportClasses, denyImportPackages is package-level -->
  25. <denyImportPackages>
  26. <package>com.alipay.sofa</package>
  27. <package>org.springframework</package>
  28. <!-- denyImportResources can prevent resource exported by ark plugin with accurate
  29. name to be resolved -->
  30. <denyImportResources>
  31. <resource>META-INF/spring/test1.xml</resource>
  32. <resource>META-INF/spring/test2.xml</resource>
  33. </denyImportResources>
  34. </configuration>
  35. </execution>
  36. </executions>
  37. </plugin>
  38. </build>

插件配置项解释:

  • outputDirectory: 执行 mvn package 命令后,指定打出来的 ark 包存放目录,默认存放至 ${project.build.directory}
  • arkClassifier: 执行 mvn depleoy 命令后,指定发布到仓库的 ark 包的maven坐标的 classifer 值, 默认为空;我们推荐配置此配置项用于和普通的 Fat Jar 加以名字上区别;
  • denyImportClasses: 默认情况下,应用会优先加载 ark plugin 导出的类,使用该配置项,可以禁止应用从 ark plugin 加载其导出类;
  • denyImportPackages: 对应上述的 denyImportClasses, 提供包级别的禁止导入;
  • denyImportResources: 默认情况下,应用会优先加载 ark plugin 导出的资源,使用该配置项,可以禁止应用从 ark plugin 加载其导出资源;

在实际开发中,为了在跑测试用例时使用 SOFABoot 类隔离能力,需要在 SOFABoot 工程中添加如下的依赖:

  1. <dependency>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>sofa-ark-springboot-starter</artifactId>
  4. </dependency>

根据 SpringBoot 依赖即服务的原则,添加该依赖之后,应用启动之前,会优先启动 SOFABoot 类隔离容器;

  1. <dependency>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>rpc-sofa-boot-starter</artifactId>
  4. <classifier>ark-plugin</classifier>
  5. </dependency>

如此,在运行时,SOFABoot 的类隔离容器会自动隔离 SOFARPC 和其他应用依赖,避免可能存在的包冲突;当然开发者也可以独立开发自身所需 ark plugin 包,详情参考SOFAArk

直接通过 main 函数启动即可,可以看到类似如下的启动成功日志:

打包运行

使用 mvn clean install 将工程打包具体类隔离能力的 Fat Jar。打包之后的文件格式类似如下:

  1. .
  2. ├── classes
  3. └── com
  4. └── alipay
  5. └── sofa
  6. └── boot
  7. └── examples
  8. └── demo
  9. └── isolation
  10. └── SofaBootClassIsolationDemoApplication.class
  11. ├── generated-sources
  12. └── annotations
  13. ├── maven-archiver
  14. └── pom.properties
  15. └── maven-compiler-plugin
  16. ├── compile
  17. └── default-compile
  18. ├── createdFiles.lst
  19. └── inputFiles.lst
  20. └── testCompile
  21. └── default-testCompile
  22. └── inputFiles.lst
  23. ├── sofaboot-sample-with-isolation-2.4.0-ark-biz.jar
  24. ├── sofaboot-sample-with-isolation-2.4.0-executable-ark.jar
  25. └── sofaboot-sample-with-isolation-2.4.0.jar

其中 就是一个具备类隔离能力的 Fat Jar,我们可以直接通过 java -jar 的方式将其启动。在服务器或者控制台上执行 java -jar sofaboot-sample-with-isolation-2.4.0-executable-ark 可以看到类似如下的启动成功日志:

  1. 2018-04-07 10:57:48.033 INFO 8488 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
  2. 2018-04-07 10:57:48.042 INFO 8488 --- [ main] .i.SofaBootClassIsolationDemoApplication : Started SofaBootClassIsolationDemoApplication in 7.432 seconds (JVM running for 8.99)
  3. Ark container started in 8797 ms.

为了在跑测试用例时使用 SOFABoot 类隔离能力,需要额外引入 SOFABoot 官方提供的 test-sofa-boot-starter 依赖,使用 SofaBootRunnerSofaJUnit4Runner 编写集成测试和单元测试,这也是 SOFABoot 推荐测试用例的编写方式.

使用 SofaBootRunnerSofaJUnit4Runner 的好处是会自动感知应用是否使用了类隔离能力,开发者只需添加或者删除 SOFABoot 类隔离依赖包:

  1. <dependency>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>sofa-ark-springboot-starter</artifactId>
  4. </dependency>

SofaBootRunner

集成测试示例代码:

SofaBootRunner 会检测应用是否引入

  1. <dependency>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>sofa-ark-springboot-starter</artifactId>
  4. </dependency>

依赖;根据 Spring Boot 依赖即服务的原则,如果检测到 sofa-ark-springboot-starter 依赖,SofaBootRunner 会使用 SOFABoot 类隔离能力,否则和原生的 SpringRunner 无异;

SofaJUnit4Runner

单元测试示例代码:

  1. @RunWith(SofaJUnit4Runner.class)
  2. public class UnitTestCase {
  3. @Test
  4. public void test() {
  5. Assert.assertTrue(true);
  6. }
  7. }

SofaJUnit4Runner 同样会检测应用是否引入 sofa-ark-springboot-starter 依赖;根据 Spring Boot 依赖即服务的原则,如果检测到 sofa-ark-springboot-starter 依赖,SofaJUnit4Runner 会使用 SOFABoot 类隔离能力,否则和原生的 JUnit4 无异;

自定义 Runner

在编写测试用例时,有时需要指定特殊的 Runner,为了统一编码风格,可以借助注解 配合 SofaBootRunnerSofaJUnit4Runner 使用,示例代码:

  1. @RunWith(SofaJUnit4Runner.class)
  2. @DelegateToRunner(BlockJUnit4ClassRunner.class)
  3. public class UnitTestCaseWithoutArk {
  4. @Test
  5. public void test() {
  6. Assert.assertFalse(true);
  7. }