当前位置:网站首页>alibaba jarslink
alibaba jarslink
2022-06-27 11:57:00 【Floating across the sea】
alibaba jarslink frame course
demo Of github Address :https://github.com/superRabbitMan/jarslink-demo
What is? jarslink
JarsLink ( Original name Titan) It's based on JAVA Modular development framework for , It provides dynamic loading of modules at run time ( One JAR package )、 Uninstall modules and calls between modules API.
Why use jarslink
Isolation,
- Class isolation : The framework is for each module Class Use a separate ClassLoader To load the , Each module can rely on different versions of the same framework .
- Instance isolation : The framework creates a separate for each module Spring Context , To load... In the module BEAN, Instantiation failure will not affect other modules .
- Resource isolation : Resource isolation between modules will be supported later , Each module uses a separate CPU And memory resources .
dynamic
- Dynamic release : Modules can be dynamically loaded into the system at run time , Realize the new functions without restarting and publishing the system . Support breaking through the parental delegation mechanism , Load the class already loaded by the parent loader at runtime , The implementation of the module upgrade dependency package does not require system release .
- Dynamic unloading : Modules can be dynamically unloaded at runtime , No function is required to realize fast offline .
Ease of use
- Provides a versatile and flexible API Let the system interact with the module .
download
\1. Official website :https://github.com/alibaba/jarslink
\2. Other ways :https://oss.sonatype.org/#nexus-search;quick~jarslink
Introduce projects
As for how to create a Maven The project is not introduced here , Just Baidu yourself
# The main POM, Here please use 1.6 Version above , Because the following introduction has a function 1.6 It is only supported in versions above
<dependency>
<groupId>com.alipay.jarslink</groupId>
<artifactId>jarslink-api</artifactId>
<version>1.6.1.20180301</version>
</dependency>
# rely on POM
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</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>2.6</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>3.2.1</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>
</dependencies>
Introduce modules jar package
modular jar File import is unnecessary buildpath Import project's , You can put it in CDEF Any position of the disc is OK , Because this module jar Files are not launched with the project , We use jarslink Framework to reference it .
For ease of invocation , I put it in the project resources Under the document :

The most important object
<!-- Module loading engine , Responsible for loading modules --><bean name="moduleLoader" class="com.alipay.jarslink.api.impl.ModuleLoaderImpl"></bean>
<!-- Module manager , Responsible for registration , uninstall , Find modules and execute Action --><bean name="moduleManager" class="com.alipay.jarslink.api.impl.ModuleManagerImpl"></bean>
To configure jarslink.xml file
<?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">
<!-- Module loading engine -->
<bean id="moduleLoader" name="moduleLoader" class="com.alipay.jarslink.api.impl.ModuleLoaderImpl" />
<!-- Module manager -->
<bean id="moduleManager" name="moduleManager" class="com.alipay.jarslink.api.impl.ModuleManagerImpl" />
</beans>
Configure module information
public static ModuleConfig buildModuleConfig() {
URL demoModule = Thread.currentThread().getContextClassLoader().getResource("jarslink-module-demo-1.0.0.jar");// Load module jar package , Can be in any path
ModuleConfig moduleConfig = new ModuleConfig();
moduleConfig.setName("hello-world");// Set the module name
moduleConfig.setEnabled(true);
moduleConfig.setVersion("1.0.0");// Set version
moduleConfig.setProperties(ImmutableMap.of("svnPath", new Object()));
moduleConfig.setModuleUrl(ImmutableList.of(demoModule));
return moduleConfig;
}
The first call demo
package com.alibaba.test;
import com.alipay.jarslink.api.Action;
import com.alipay.jarslink.api.Module;
import com.alipay.jarslink.api.ModuleLoader;
import com.alipay.jarslink.api.ModuleManager;
import com.alipay.jarslink.api.impl.AbstractModuleRefreshScheduler;
import com.alipay.jarslink.api.impl.ModuleManagerImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Map;
import java.util.Set;
/** * Created by HASEE on 2018/4/22. */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath*:jarslink.xml"})
public class JarsLinkTest {
@Autowired
private ModuleLoader moduleLoader;
@Autowired
private ModuleManager moduleManager;
@Test
public void test1() {
// System.out.println("moduleLoader = " + moduleLoader);
// System.out.println("moduleManager = " + moduleManager);
Module module = moduleLoader.load(ModuleRefreshSchedulerImpl.buildModuleConfig());// Load module , Load a module
moduleManager.register(module);// Register module information
/* Get the specified Action, Execution mode I */
Module mod = moduleManager.find("hello-world");
Map<String, Action> actions = mod.getActions();
// Set<String> keys = actions.keySet();
// for (String key : keys) {
// System.out.println(key + ", " + actions.get(key));
// }
Action xmlaction = actions.get("XMLACTION");
System.out.println(xmlaction.execute("hello world"));
/* Get the specified Action, Mode II */
//doAction Parameters : Module action The name of ,action in execute Method parameters
String result = module.doAction("XMLACTION", "hello world");
System.out.println(result);
}
}
summary
Jarslink The use of is so simple ,1) Load module jar.2) Registration module .3) Call the Action
Development module ( Create a new one maven project )
Module and Action The relationship between , One jar A package is a module , There is one or more... In the module Action class , What we call is the module Action Acquisition function . All development modules are actually development Action.
Action The development of is also relatively simple , As long as the implementation com.alipay.jarslink.api.Action<R,T> The class can ,Action Yes 2 Generic classes ,R Represents the incoming parameter ,T Indicates the return parameter .
Action There are two methods to implement ,1)T execute(Rvar1);.2)String getActionName();.execute The method is Action Function method provided .getActionName Method is to get the name of the , The name must be unique , If it is not unique, then it will not know which module to execute Action.
package com.alibaba.action;
import com.alipay.jarslink.api.Action;
/** * Created by HASEE on 2018/4/22. */
public class HelloWorldAction implements Action<String, String> {
@Override
public String execute(String s) {
return s + ":hello world";
}
@Override
public String getActionName() {
return "hello-world-action";
}
}
To configure xml file
Please note the directory where the configuration files are stored ,resources/META-INF/spring/*.xml, When loading the module, you will read the configuration information in this directory .

The configuration information is as follows
<?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/>
<!-- establish Action-->
<bean id="helloWorldAction" class="com.alibaba.action.HelloWorldAction"/>
<bean id="welComeAction" class="com.alibaba.action.WelComeAction" />
</beans>
Compile the file and go everywhere Jar package , Such a module is developed , This module contains a HelloworldAction Class .
Will export jar The package is introduced into the first case resources Under the folder , You can call .
The second call demo
Configure self-developed modules
public static ModuleConfig buildModuleConfig_Demo() {
URL demoModule = Thread.currentThread().getContextClassLoader().getResource("jarslink-demo-action-1.0-SNAPSHOT.jar");
ModuleConfig moduleConfig = new ModuleConfig();
moduleConfig.setName("demo-action");
moduleConfig.setEnabled(true);
moduleConfig.setVersion("1.0.0");
// Configure custom properties Information
moduleConfig.setProperties(ImmutableMap.of("svnPath", new Object()));
moduleConfig.setModuleUrl(ImmutableList.of(demoModule));
return moduleConfig;
}
package com.alibaba.test;
import com.alipay.jarslink.api.Action;
import com.alipay.jarslink.api.Module;
import com.alipay.jarslink.api.ModuleLoader;
import com.alipay.jarslink.api.ModuleManager;
import com.alipay.jarslink.api.impl.AbstractModuleRefreshScheduler;
import com.alipay.jarslink.api.impl.ModuleManagerImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Map;
import java.util.Set;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath*:jarslink.xml"})
public class JarsLinkTest {
@Autowired
private ModuleLoader moduleLoader;
@Autowired
private ModuleManager moduleManager;
@Test
public void test2() {
// Load and register modules
Module module = moduleLoader.load(ModuleRefreshSchedulerImpl.buildModuleConfig_Demo());// Load module , Load a module
moduleManager.register(module);
String result = module.doAction("hello-world-action", "rabbit");
System.out.println("result = " + result);
}
}
summary
A modular Action Development is that simple ,1) Create your own Action.2) Configuration in profile bean Information . The problem is developing one at a time Action You need to add a configuration file to the configuration file <beanid=”” name=”” class=”” />, Once there are more classes , Such management is more troublesome , Fortunately 1.6 The function of scanning package is provided in version .
Scan package function
Action The development process is the same , But in xml No configuration information is required in the file , Instead, you can scan the specified package when calling , Here's the key code .
package com.alibaba.test;
import com.alipay.jarslink.api.Module;
import com.alipay.jarslink.api.ModuleConfig;
import com.alipay.jarslink.api.ModuleLoader;
import com.alipay.jarslink.api.ModuleManager;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.net.URL;
/** * Created by HASEE on 2018/4/23. */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath*:jarslink.xml"})
public class AnnoJarsLinkTest {
@Autowired
private ModuleLoader moduleLoader;
@Autowired
private ModuleManager moduleManager;
@Test
public void test1() {
URL demoModule = Thread.currentThread().getContextClassLoader().getResource("jarslink-demo-anno-action-1.0-SNAPSHOT.jar");
ModuleConfig moduleConfig = new ModuleConfig();
moduleConfig.setName("anno-action");
moduleConfig.setEnabled(true);
moduleConfig.setVersion("1.0.0");
moduleConfig.setProperties(ImmutableMap.of("svnPath", new Object()));
moduleConfig.setModuleUrl(ImmutableList.of(demoModule));
// Scan the... Under the module Action
moduleConfig.addScanPackage("com.alibaba.action");
Module module = moduleLoader.load(moduleConfig);
moduleManager.register(module);
System.out.println("string to long " + module.doAction("string-to-long", "500"));
}
}
Best practices
HTTP Request forwarding
Determine the requested module and... Through the request path action
private ModuleManager moduleManager;
@RequestMapping(value = "module/{moduleName}/{actionName}/process.json", method = {
RequestMethod.GET,RequestMethod.POST })
public Object process(HttpServletRequest request, HttpServletResponse response) {
Map<String, String> pathVariables = resolvePathVariables(request);
String moduleName = pathVariables.get("moduleName").toUpperCase()
String actionName = pathVariables.get("actionName").toUpperCase()
String actionRequest = XXX;
return moduleManager.doAction(moduleName,
actionName, actionRequest);
}
private Map<String, String> resolvePathVariables(HttpServletRequest request) {
return (Map<String, String>) request
.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
}
How to publish modules
There are two options :
programme 1 Pull mode
- 1: Compile the code locally and package it into JAR package .
- 2: hold JAR Upload to a remote file server , For example, Alibaba cloud's OSS.
- 3: The application is downloaded from a remote server JAR, The configuration information can be stored in JAR in , Such as version number ,JAR name .
programme 2 Push mode
- 1: Compile the code locally and package it into JAR package .
- 2: hold JAR direct SCP To a directory on the server .
- 3: The application checks the directory specified by the service JAR Is there an update , If there is an update, load it .
边栏推荐
- [tcapulusdb knowledge base] Introduction to tmonitor system upgrade
- Interview shock 60: what will cause MySQL index invalidation?
- Shell script learning notes
- FileOutputStream
- Xuri 3sdb, installing the original ROS
- Jerry's DAC output mode setting [chapter]
- 千万不要错过,新媒体运营15个宝藏公众号分享
- [tcapulusdb knowledge base] Introduction to tcapulusdb table data caching
- Nvme2.0 protocol - new features
- Jwas: a Bayesian based GWAS and GS software developed by Julia
猜你喜欢

C語言0長度數組的妙用

2022CISCN华中 Web

【TcaplusDB知识库】TcaplusDB常规单据介绍
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction](/img/a4/c3255ce17516348f703f7f21511555.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction

干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了

Mqtt protocol stack principle and interaction flow chart

【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍

L'utilisation de C language 0 length Array

从零开始搭建物联网系统

Redis distributed lock 15 ask, what have you mastered?
随机推荐
数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段
Daily leetcode force deduction (21~25)
Challenges of machine learning system in production
【TcaplusDB知识库】TcaplusDB系统管理介绍
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
千万不要错过,新媒体运营15个宝藏公众号分享
[tcapulusdb knowledge base] Introduction to tcapulusdb data import
杰理之睡眠以后定时唤醒系统继续跑不复位【篇】
Unity Shader学习(二)第一个Shader
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
This privatized deployed enterprise knowledge base makes telecommuting a zero distance
The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the AIC function is used to compare the AIC values of the two models (simple
Nvme2.0 protocol - new features
Qstype implementation of self drawing interface project practice (I)
57. The core principle of flutter - layout process
alibaba jarslink
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
进程间通信详解
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、解读分析模型