Jarslink Demo Alibaba(教程 源码)

简介: Jarslink Demo Alibaba(教程 源码)

Jarslink Demo源码


https://github.com/baidaguo/jarslink


Jarslink 简介


JarsLink是一个基于JAVA的模块化开发框架,它提供在运行时动态加载模块(JAR包)、卸载模块和模块间调用的API。


优点


隔离性


类隔离:框架为每个模块的Class使用单独的ClassLoader来加载,每个模块可以依赖同一种框架的不同的版本。

实例隔离:框架为每个模块创建了一个独立的Spring上下文,来加载模块中的BEAN,实例化失败不会影响其他模块。

资源隔离:后续会支持模块之间的资源隔离,每个模块使用独立的CPU和内存资源。


动态性


动态发布:模块能在运行时动态加载到系统中,实现不需要重启和发布系统新增功能。支持突破双亲委派机制,在运行时加载父加载器已经加载过的类,实现模块升级依赖包不需要系统发布。

动态卸载:模块能在运行时被动态卸载干净,实现快速下线不需要功能。


应用背景


微服务集成测试, 目前一个微服务是一个FAT


JAR,如果有几十个微服务,则需要启动很多进程,DEBUG端口会很多,使用JarsLink框架合并FAT


JAR,再路由请求到其他JAR,就可以只启动一个进程进行DEBUG测试。


数据管理中心,数据采集的数据源多,而且每种数据源都需要对接和开发,通过模块化开发,实现一个数据源使用一个模块进行对接。


指标计算系统,每个TOPIC一个模块,把消息转发到模块中进行消息处理。


后台管理系统,几乎每个系统都有后台开发的需求,新建应用则应用数多,维护成本高,


引入模块化开发,一个二级域一个模块来开发后台功能。


目前蚂蚁金服微贷事业部几个系统和几十个模块已经使用JarsLink框架。


下载


官网:https://github.com/alibaba/jarslink


其它途径:https://oss.sonatype.org/#nexus-search;quick~jarslink


项目总体构造


20210708161656359.png


引入依赖


此处jarslink-api选择引入1.6版本以上 由于在每次开发一个Action就要配置文件中加配置文件<beanid=”” name=”” class=”” />,一旦类多了,这样管理比较麻烦,还好在1.6版本中提供了扫描包的功能。


<dependency>
            <groupId>com.alipay.jarslink</groupId>
            <artifactId>jarslink-api</artifactId>
            <version>1.6.1.20180301</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.5.RELEASE</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>${apache.commons.lang.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>${apache.commons.collections.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>17.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>

主模块引入子模块jar包

20210708161743987.png

jarslink.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
       <!--模块加载引擎-->
       <bean id="moduleLoader" name="moduleLoader" class="com.alipay.jarslink.api.impl.ModuleLoaderImpl" />
       <!--模块管理器-->
       <bean id="moduleManager" name="moduleManager" class="com.alipay.jarslink.api.impl.ModuleManagerImpl" />
       <!--可以通过该对象获取所有模块信息-->
       <bean id="moduleRefreshScheduler" name="moduleRefreshScheduler" class="com.alibaba.service.ModuleRefreshSchedulerImpl">
              <property name="moduleLoader" ref="moduleLoader" />
              <property name="moduleManager" ref="moduleManager" />
       </bean>
</beans>

子模块一配置及Action

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:webflow="http://www.springframework.org/schema/webflow-config"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
         http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd"
       default-autowire="byName">
    <context:annotation-config/>
    <!--创建Action-->
    <bean id="helloWorldAction" class="com.alibaba.action.HelloWorldAction"/>
    <bean id="welComeAction" class="com.alibaba.action.WelComeAction" />
</beans>
public class HelloWorldAction implements Action<String, String> {
    @Override
    public String execute(String s) {
        return s + ":hello world222";
    }
    @Override
    public String getActionName() {
        return "hello-world-action";
    }
}
public class WelComeAction implements Action<String, String> {
    @Override
    public String execute(String s) {
        return "welcome " + s;
    }
    @Override
    public String getActionName() {
        return "welcome-action";
    }
}


子模块二配置及Action

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:webflow="http://www.springframework.org/schema/webflow-config"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
         http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd"
       default-autowire="byName">
    <context:annotation-config/>
</beans>
@Configuration
public class StringToLongAction implements Action<String, Long> {
    @Override
    public Long execute(String s) {
        return Long.valueOf(s);
    }
    @Override
    public String getActionName() {
        return "string-to-long";
    }
}

编译文件然后导出Jar包 这样一个模块就开发完成(一个模块的Action开发就是这么简单,1.创建自己的Action;2.配置文件中配置bean信息) 对应模块下有自己对应的Action


测试


此处我们选择子模块一HelloAction进行调用测试

20210709095905615.png

运行结果如下

20210708163253790.png

可以看到HelloAction已调用 而后修改完成子模块重新打包jar,并且拷贝到主模块下


更新HelloAction后会实现热插拔动态部署 再次看结果如下:

20210708163406606.png

目录
相关文章
|
前端开发 算法 数据可视化
怎么在echarts图上左右滑动切换数据区间
怎么在echarts图上左右滑动切换数据区间
611 0
太惨痛: Redis 分布式锁 5个大坑,又大又深, 如何才能 避开 ?
Redis分布式锁在高并发场景下是重要的技术手段,但其实现过程中常遇到五大深坑:**原子性问题**、**连接耗尽问题**、**锁过期问题**、**锁失效问题**以及**锁分段问题**。这些问题不仅影响系统的稳定性和性能,还可能导致数据不一致。尼恩在实际项目中总结了这些坑,并提供了详细的解决方案,包括使用Lua脚本保证原子性、设置合理的锁过期时间和使用看门狗机制、以及通过锁分段提升性能。这些经验和技巧对面试和实际开发都有很大帮助,值得深入学习和实践。
太惨痛: Redis 分布式锁 5个大坑,又大又深, 如何才能 避开 ?
Vue2表格(Table)
这是一个基于 Vue3 的表格组件,提供了灵活的数据展示与分页功能。主要接收表格列配置 `columns`、数据源 `dataSource`、分页器配置 `pagination` 等参数,并支持加载状态显示及单页隐藏分页器等特性。组件内置了加载中组件 `Spin` 和分页组件 `Pagination`,样式参考 ant-design。使用时需在目标页面引入组件并设置相关属性即可实现丰富的表格展示效果。
765 1
Vue2表格(Table)
|
Java API Apache
阿里巴巴开源框架JarsLink
JarsLink是一个基于JAVA的模块化开发框架,它提供在运行时动态加载模块(JAR包)、卸载模块和模块间调用的API,它能够帮助你进行模块化开发,也能帮助你的系统在运行时动态添加新功能,减少编译、打包和部署带来的发布耗时,同时它也是阿里巴巴的开源项目之一 https://github.com/alibaba/jarslink,目前在微贷事业群各团队广泛使用。
14727 0
|
缓存 前端开发 JavaScript
为什么用Vite框架?来看它的核心组件案例详解
Vite 是一款前沿的前端构建工具,以其闪电般的开发服务器和高效的生产构建而著称。它利用现代浏览器对 ES 模块的支持,在开发环境中提供快速启动及按需加载,显著提升了开发体验。Vite 的核心组件包括开发服务器、按需编译、依赖预构建、热模块替换(HMR)、缓存机制、模块路径重写、构建优化和插件系统。通过这些功能,Vite 实现了快速的模块加载、高效的模块更新、减少网络请求、以及生产环境下的代码压缩和优化。Vite 还支持多种前端框架和技术栈,内置 TypeScript 支持,并能处理 CSS 和静态资源,极大地方便了开发者的日常开发工作。
563 9
|
网络安全 网络架构
Nmap扫描六种端口状态介绍
Nmap扫描六种端口状态介绍
829 2
|
XML 安全 IDE
springboot @RequiredArgsConstructor的概念与使用
【4月更文挑战第25天】在Spring Boot中,@RequiredArgsConstructor注解是Lombok库提供的功能,用于自动生成包含必需参数的构造函数。"必需参数"指的是那些被声明为final或者有@NonNull注解的成员变量。这种注解极大地简化了Java类的编写,尤其是在需要注入依赖或常量值时
681 3
|
Kubernetes Cloud Native API
云原生技术专题 | 深入浅出分析云原生微服务的技术结构和架构设计
云原生技术专题 | 深入浅出分析云原生微服务的技术结构和架构设计
833 0
|
SQL DataWorks 应用服务中间件
【教程】利用宜撘实现自动生成业务SQL功能
模板地址: https://yida.alibaba-inc.com/newApp.html?#/template/TPL_LPS8OWKVUIEHEVM80XFN?_k=z3r1e4 背景: 之前在内网上有讨论SQL算不算是编程语言,一石激起千层浪,大家参与度非常高,有很多运营同学也表示会用SQL,我觉得这个是很好的现象。编程语言只是解决问题的工具,黑猫白猫,能抓到老鼠就是好猫。 这个问
1561 0