jarslink框架 + springMVC
1.jarslink框架的原理介绍:
https://mp.weixin.qq.com/s/AhNIvlKNba8ls9loM1CZ-w?client=tim&ADUIN=252714046&ADSESSION=1521678260&ADTAG=CLIENT.QQ.5531_.0&ADPUBNO=26745
2.jarslink使用三步骤:(1):加载模块jar;(2):注册模块; (3):调用模块的Action
项目的结构:maven + tomcat 8.5.30 +jdk 1.8
pom.xml 文件:
<?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>com.caox</groupId>
<artifactId>nazi</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alipay.jarslink</groupId>
<artifactId>jarslink-api</artifactId>
<version>1.5.0.20180217</version>
<exclusions>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package com.caox.controller;
/**
* Created by BF100 on 2018/5/8.
*/
import com.alipay.jarslink.api.*;
import com.caox.schedule.ModuleRefreshSchedulerImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@Controller
@RequestMapping(value="/hello", method = GET)
public class HelloReaderController {
@Resource(name = "moduleManager")
private ModuleManager moduleManager;
@Resource(name = "moduleService")
private ModuleService moduleService;
@RequestMapping(value = "/hello", method = GET)
public String printHello(ModelMap model) {
// 查找模块-组装发票
Module findModule = moduleManager.find("invoice-jars");
// 执行Action,数据交互
// 方式一
// Action<String, String> action = findModule.getAction("hello-world-action");
// String result = action.execute("rabbit");
// 方式二
moduleManager.register(findModule);
String result = findModule.doAction("hello-world-action", "rabbit");
model.addAttribute("message",result);
return "helloReader";
}
@RequestMapping(value = "/reload", method = RequestMethod.GET)
@ResponseBody
public Object reloadModule() {
ModuleConfig moduleConfig = ModuleRefreshSchedulerImpl.buildModuleConfig();
moduleConfig.setEnabled(true);
moduleService.loadAndRegister(moduleConfig);
return "success";
}
}
package com.alipay.jarslink.demo;
import com.alipay.jarslink.api.Action;
import com.alipay.jarslink.api.ModuleConfig;
/**
* 一个简单的Action实现 开发模块(新建一个maven项目)
*
*/
public class WelComeAction implements Action<String, String> {
@Override
public String execute(String var) {
return var + ":hello world";
}
@Override
public String getActionName() {
return "hello-world-action";
}
}
重要:子模块的修改完成之后,需要打包并且拷贝到resources/META-INF内,手动或等待定时器自动加载最新的jar之后完成热插拔(每次更新先运行)
子模块打包Action参考项目:
3.jarslink目前最新版本:支持扫描包功能
参考项目
<dependency>
<groupId>com.alipay.jarslink</groupId>
<artifactId>jarslink-api</artifactId>
<version>1.6.1.20180301</version>
</dependency>
/*
*
* * Copyright 1999-2018 Alibaba Group Holding Ltd.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package com.alipay.jarslink.api.impl;
import com.alipay.jarslink.api.*;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.junit.Assert;
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.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;
import static com.alipay.jarslink.api.impl.ModuleLoaderImplTest.buildModuleConfig;
/**
* 模块加载和执行测试
*
* @author tengfei.fangtf
* @version $Id: ModuleManagerTest.java
* <p>
* v 0.1 2017年06月20日 3:24 PM tengfei.fangtf Exp $
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:META-INF/spring/jarslink.xml"})
public class ModuleManagerTest {
@Autowired
private ModuleManager moduleManager;
@Autowired
private ModuleLoader moduleLoader;
@Test
public void shouldRegisterModule() throws MalformedURLException {
//注册一个模块
Module module = loadModule("demo", "1.0");
Module removedModule = moduleManager.register(module);
Assert.assertNull(removedModule);
再注册同一个模块不同版本,会卸载旧的模块
Module newModule = loadModule("demo", "2.0");
removedModule = moduleManager.register(newModule);
Assert.assertEquals(1, moduleManager.getModules().size());
//查找模块
Module findModule = moduleManager.find(module.getName());
Assert.assertNotNull(findModule);
Assert.assertNotNull(moduleManager.getErrorModuleContext());
Assert.assertEquals(1, moduleManager.getModules().size());
Module remove = moduleManager.remove(module.getName());
Assert.assertNull(moduleManager.find(remove.getName()));
Assert.assertEquals(0, moduleManager.getModules().size());
removeModule(module);
}
@Test
public void shouldNotRegisterSameVersionModule() {
//注册一个模块
Module module = loadModule();
Module removedModule = moduleManager.register(module);
Assert.assertNull(removedModule);
Assert.assertEquals(1, moduleManager.getModules().size());
//再注册同一个模块,不会注册成功,返回空
Module register = moduleManager.register(module);
Assert.assertNull(register);
Assert.assertEquals(1, moduleManager.getModules().size());
}
private Module loadModule() {
return moduleLoader.load(buildModuleConfig(true));
}
private Module loadModule(String name) {
return moduleLoader.load(buildModuleConfig(name, "1.0.0.20170621", true));
}
private void removeModule(Module module) {
moduleManager.remove(module.getName());
}
private Module loadModule(String name, String version) {
return moduleLoader.load(buildModuleConfig(name, version, true));
}
@Test
public void test1() {
URL demoModule = Thread.currentThread().getContextClassLoader().getResource("jarslink-module-demo-1.0.0.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));
//扫描模块下的Action
moduleConfig.addScanPackage("com.alipay.jarslink.demo");
Module module = moduleLoader.load(moduleConfig);
moduleManager.register(module);
System.out.println("string to long " + module.doAction("app", "500"));
}
}
package com.alipay.jarslink.demo;
import com.alipay.jarslink.api.Action;
import org.springframework.stereotype.Component;
/**
* 一个简单的Action实现 开发模块(新建一个maven项目)
*
*/
@Component("welComeAction")
public class WelComeAction implements Action<String, String> {
@Override
public String execute(String var) {
return var + ":hello world";
}
@Override
public String getActionName() {
return "app";
}
}
@Test
public void test2() {
//加载和注册模块
Module module = moduleLoader.load(buildModuleConfig_Demo());//加载模块,加载一个模块
moduleManager.register(module);
String result = module.doAction("hello-world-action", "rabbit");
System.out.println("result = " + result);
}