美文网首页
第一章 SOFAArk 类隔离技术使用姿势

第一章 SOFAArk 类隔离技术使用姿势

作者: 原水寒 | 来源:发表于2019-12-30 08:54 被阅读0次

    SOFAArk 是一个轻量级的类隔离框架,由蚂蚁金服开源。GitHub 地址: https://github.com/sofastack/sofa-ark

    本文使用 SOFAArk 实现一个基本的类隔离例子,工程结构如下:


    image.png
    • my-ark-service:基本的服务,会打两个不同版本的包:1.0和2.0。
    • my-ark-plugin1:依赖 my-ark-service:1.0,打成一个 ark-plugin。
    • my-ark-plugin2:依赖 my-ark-service:2.0,打成一个 ark-plugin。
    • my-ark-biz:依赖 my-ark-plugin1 和 my-ark-plugin2,分别调用其两个 ark-plugin,由于使用了类隔离机制,可以使得 my-ark-service:1.0 和 my-ark-service:2.0 两个版本的包共存并且同时使用。

    my-ark-service

    v1 版本

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>io.study</groupId>
        <artifactId>my-ark-service</artifactId>
        <version>1.0</version>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
    package io.study.service;
    public class HelloService {
        public void sayHello(){
            System.out.println("hi, v1"); // v1版本
        }
    }
    

    v2 版本

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>io.study</groupId>
        <artifactId>my-ark-service</artifactId>
        <version>2.0</version>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
    package io.study.service;
    public class HelloService {
        public void sayHello(){
            System.out.println("hi, v2"); // v2版本
        }
    }
    

    my-ark-plugin1

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>io.study</groupId>
        <artifactId>my-ark-plugin1</artifactId>
        <version>1.0</version>
    
        <dependencies>
            <!-- 引入版本1.0依赖 -->
            <dependency>
                <groupId>io.study</groupId>
                <artifactId>my-ark-service</artifactId>
                <version>1.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.alipay.sofa</groupId>
                    <artifactId>sofa-ark-plugin-maven-plugin</artifactId>
                    <version>1.1.0</version>
                    <executions>
                        <execution>
                            <id>default-cli</id>
                            <goals>
                                <goal>ark-plugin</goal>
                            </goals>
                            <configuration>
                                <classifier>ark-plugin</classifier>
                                <exported>
                                    <classes>
                                        <class>io.study.plugin.ArkPlugin1Service</class>
                                    </classes>
                                </exported>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    
    package io.study.plugin;
    import io.study.service.HelloService;
    public class ArkPlugin1Service {
        public void test(){
            new HelloService().sayHello();
        }
    }
    

    my-ark-plugin2

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>io.study</groupId>
        <artifactId>my-ark-plugin2</artifactId>
        <version>1.0</version>
    
        <dependencies>
            <dependency>
                <groupId>io.study</groupId>
                <artifactId>my-ark-service</artifactId>
                <version>2.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.alipay.sofa</groupId>
                    <artifactId>sofa-ark-plugin-maven-plugin</artifactId>
                    <version>1.1.0</version>
                    <executions>
                        <execution>
                            <id>default-cli</id>
                            <goals>
                                <goal>ark-plugin</goal>
                            </goals>
                            <configuration>
                                <classifier>ark-plugin</classifier>
                                <exported>
                                    <classes>
                                        <class>io.study.plugin.ArkPlugin2Service</class>
                                    </classes>
                                </exported>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    
    package io.study.plugin;
    import io.study.service.HelloService;
    public class ArkPlugin2Service {
        public void test(){
            new HelloService().sayHello();
        }
    }
    

    my-ark-biz

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>io.study</groupId>
        <artifactId>my-ark-biz</artifactId>
        <version>1.0</version>
    
        <dependencies>
            <dependency>
                <groupId>com.alipay.sofa</groupId>
                <artifactId>sofa-ark-support-starter</artifactId>
                <version>1.1.0</version>
            </dependency>
            <dependency>
                <groupId>io.study</groupId>
                <artifactId>my-ark-plugin1</artifactId>
                <version>1.0</version>
                <!-- ark-plugin -->
                <classifier>ark-plugin</classifier>
            </dependency>
            <dependency>
                <groupId>io.study</groupId>
                <artifactId>my-ark-plugin2</artifactId>
                <version>1.0</version>
                <!-- ark-plugin -->
                <classifier>ark-plugin</classifier>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.alipay.sofa</groupId>
                    <artifactId>sofa-ark-maven-plugin</artifactId>
                    <version>1.1.0</version>
                    <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>./</outputDirectory>
                                <!--default none, 可执行fat jar-->
                                <arkClassifier>executable-ark</arkClassifier>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    
    package io.study.biz;
    import com.alipay.sofa.ark.support.startup.SofaArkBootstrap;
    import io.study.plugin.ArkPlugin1Service;
    import io.study.plugin.ArkPlugin2Service;
    public class Main {
        public static void main(String[] args) {
            SofaArkBootstrap.launch(args);
            new ArkPlugin1Service().test(); // hi,v1
            new ArkPlugin2Service().test(); // hi,v2
        }
    }
    

    本文代码地址

    参考

    相关文章

      网友评论

          本文标题:第一章 SOFAArk 类隔离技术使用姿势

          本文链接:https://www.haomeiwen.com/subject/ootuoctx.html