SOFABoot using class isolation
SOFABoot provides the class isolation framework SOFAArk, which makes up for the lack of class isolation capability of Spring Boot to solve common class conflicts and package conflicts in actual development.
For more information, see SOFAArk overview.
Using class isolation capabilities in SOFABoot projects requires only two steps:
Configure the
sofa-ark-maven-pluginpackaging plug-in.Introduce
sofa-ark-springboot-starterclass isolation framework dependency.
Configure the Maven packaging plug-in
SOFAArk provides the Maven plug-in sofa-ark-maven-plugin by default. You can package a Spring Boot project into an executable Ark package in the standard format by simple configuration. The coordinates of the plug-in are as follows:
<plugin>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-maven-plugin</artifactId>
</plugin>The configuration template is as follows:
<build>
<plugins>
<plugin>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-maven-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<!--goal executed to generate executable-ark-jar -->
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!--specify destination where executable-ark-jar will be saved, default saved to ${project.build.directory}-->
<outputDirectory>./target</outputDirectory>
<!--default none-->
<arkClassifier>executable-ark</arkClassifier>
<!-- all class exported by ark plugin would be resolved by ark biz in default, if
configure denyImportClasses, then it would prefer to load them by ark biz itself -->
<denyImportClasses>
<class>com.alipay.sofa.SampleClass1</class>
<class>com.alipay.sofa.SampleClass2</class>
</denyImportClasses>
<!-- Corresponding to denyImportClasses, denyImportPackages is package-level -->
<denyImportPackages>
<package>com.alipay.sofa</package>
<package>org.springframework</package>
</denyImportPackages>
<!-- denyImportResources can prevent resource exported by ark plugin with accurate
name to be resolved -->
<denyImportResources>
<resource>META-INF/spring/test1.xml</resource>
<resource>META-INF/spring/test2.xml</resource>
</denyImportResources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>Plug-in configuration items description:
outputDirectory: After you run themvn packagecommand, specify the directory where the generated Ark package is stored. Default to${project.build.directory}.arkClassifier: Theclassifervalue of the Maven coordinates of the Ark package published to the repository after themvn depleoycommand is executed. This parameter is empty by default.You recommend configure this item to distinguish the name from the common Fat Jar.
denyImportClasses: By default, the application loads classes exported by the Ark plugin first. Use this configuration items to disable an application from loading its exported classes from an Ark plugin.denyImportPackages: provides package-level import prohibition corresponding to the precedingdenyImportClasses.denyImportResources: By default, the application loads resources exported by the Ark plugin first. Use this configuration items to disable an application from loading its exported resources from an Ark plugin.
Add class isolation framework dependencies
In actual development, to use the SOFABoot class isolation capability when running test cases, you need to add the following dependencies to the SOFABoot project:
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-springboot-starter</artifactId>
</dependency>Based on the SpringBoot dependency-as-a-service principle, after the dependency is added, the SOFABoot isolation container is preferentially started before the application is started.
The class isolation framework of SOFABoot automatically detects whether an Ark plug-in (that is, a JAR package that needs to be isolated) is introduced into the application, and isolates the load. For more information, see SOFAArk overview. For example, to avoid dependency conflicts between SOFARPC components provided by SOFABoot and applications, SOFABoot provides the Ark Plugin version corresponding to SOFARPC components. To isolate SOFARPC, add the following components:
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-plugin</artifactId>
</dependency>After the application is added, the SOFABoot class isolation container will automatically isolate SOFARPC and other application dependencies to avoid possible package conflicts when the application is run time. Of course, developers can also independently develop their own required Ark Plugin packages. For more information, see SOFAArk.
Run SOFABoot project
Running in the IDE
Run the main function to start the service. You can see a startup success log similar to the following:
2018-04-0710:51:59,646 INFO main -Begin to start ArkServiceContainer
2018-04-0710:52:00,550 INFO main -InitService: com.alipay.sofa.ark.container.service.plugin.PluginDeployServiceImpl
2018-04-0710:52:00,551 INFO main -InitService: com.alipay.sofa.ark.container.service.classloader.ClassloaderServiceImpl
2018-04-0710:52:00,557 INFO main -Finish to start ArkServiceContainer
2018-04-0710:52:00,571 INFO main -Start to process pipeline stage: com.alipay.sofa.ark.container.pipeline.HandleArchiveStage
2018-04-0710:52:00,624 INFO main -Finish to process pipeline stage: com.alipay.sofa.ark.container.pipeline.HandleArchiveStage
2018-04-0710:52:00,624 INFO main -Start to process pipeline stage: com.alipay.sofa.ark.container.pipeline.DeployPluginStage
2018-04-0710:52:00,626 INFO main -Start to deploy plugin: sample-ark-plugin
starting in sample ark plugin activator
2018-04-0710:52:00,645 INFO main -Service: com.alipay.sofa.ark.sample.facade.SamplePluginService publish by:PluginServiceProvider{pluginName='sample-ark-plugin', priority=1000} succeed
2018-04-0710:52:00,647 INFO main -Finish to deploy plugin: sample-ark-plugin
2018-04-0710:52:00,647 INFO main -Finish to process pipeline stage: com.alipay.sofa.ark.container.pipeline.DeployPluginStage
2018-04-0710:52:00,647 INFO main -Start to process pipeline stage: com.alipay.sofa.ark.container.pipeline.DeployBizStage
2018-04-0710:52:00,650 INFO main -Begin to start biz:StartupIn IDEPackage and run
Use the Fat Jar that mvn clean install the ability to isolate specific classes for project packaging. The format of the packaged file is similar to the following:
.
├── classes
│ └── com
│ └── alipay
│ └── sofa
│ └── boot
│ └── examples
│ └── demo
│ └── isolation
│ └──SofaBootClassIsolationDemoApplication.class
├── generated-sources
│ └── annotations
├── maven-archiver
│ └── pom.properties
├── maven-status
│ └ ── maven-compiler-plugin
│ ├── compile
│ │ └──default-compile
│ │ ├── createdFiles.lst
│ │ └── inputFiles.lst
│ └── testCompile
│ └ ── default-testCompile
│ └── inputFiles.lst
├── sofaboot-sample-with-isolation-2.4.0-ark-biz.jar
├── sofaboot-sample-with-isolation-2.4.0-executable-ark.jar
└── sofaboot-sample-with-isolation-2.4.0.jar sofaboot-sample-with-isolation-2.4.0-executable-ark of these is a Fat JAR with class isolation capability, which can be started directly by java -jar. Run a java -jar sofaboot-sample-with-isolation-2.4.0-executable-ark on the server or in the console. You can see a startup success log similar to the following.
2018-04-0710:57:48.033 INFO 8488---[ main] s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat started on port(s):8080(http)
2018-04-0710:57:48.042 INFO 8488---[ main].i.SofaBootClassIsolationDemoApplication:StartedSofaBootClassIsolationDemoApplicationin7.432 seconds (JVM running for8.99)
Ark container started in8797 ms.Run test cases
In order to use SOFABoot class isolation capabilities when running test cases, it is necessary to additionally introduce SOFABoot official test-sofa-boot-starter dependencies and use SofaBootRunner and SofaJUnit4Runner to write integration tests and unit tests, which is also the way SOFABoot recommend test cases are written.
The advantage of using SofaBootRunner and SofaJUnit4Runner is that it will automatically detect whether the application uses the class isolation capability. Developers only need to add or delete the SOFABoot class isolation dependency package to complete the integration and stripping with the SOFABoot class isolation capability.
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-springboot-starter</artifactId>
</dependency>The usage details are as follows:
SofaBootRunner
Sample code for integration testing:
@RunWith(SofaBootRunner.class) @SpringBootTest(classes =SofaBootClassIsolationDemoApplication.class) public class IntegrationTestCase{ @Autowired private SampleService sampleService; @Test public void test(){ Assert.assertTrue("service".equals(sampleService.service())); } }The
SofaBootRunnerchecks whether the application introduces the following dependencies:<dependency> <groupId>com.alipay.sofa</groupId> <artifactId>sofa-ark-springboot-starter</artifactId> </dependency>According to the Spring Boot dependency-as-a-service principle, if a
sofa-ark-springboot-starterdependency is detected, theSofaBootRunneruses the SOFABoot class isolation capability, otherwise it is the same as the nativeSpringRunner.SofaJUnit4Runner
Sample code for unit tests:
@RunWith(SofaJUnit4Runner.class) public class UnitTestCase{ @Test public void test(){ Assert.assertTrue(true); } }The
SofaJUnit4Runneralso detects whether the application introducessofa-ark-springboot-starterdependencies. According to the Spring Boot dependency-as-a-service principle, if asofa-ark-springboot-starterdependency is detected, theSofaJUnit4Runneruses the SOFABoot class isolation capability, otherwise it is the same as the nativeJUnit4.Custom Runner
When writing test cases, it is sometimes necessary to specify a special Runner. In order to unify the coding style, you can use annotation
@DelegateToRunnerwithSofaBootRunnerandSofaJUnit4Runner. The sample code is as follows:@RunWith(SofaJUnit4Runner.class) @DelegateToRunner(BlockJUnit4ClassRunner.class) public class UnitTestCaseWithoutArk{ @Test public void test(){ Assert.assertFalse(true); } }Equivalent to the following usage:
@RunWith(BlockJUnit4ClassRunner.class) public class UnitTestCaseWithoutArk{ @Test public void test(){ Assert.assertFalse(true); } }