jarslink框架 + springMVC

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

jarslink框架原理介绍


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>


主模块:HelloReaderController
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";
    }


}


子模块:Action (数据的组装、业务的处理都由子模块独立完成) 实际就是打成jar包 供主模块调用  具体指的是项目中的 META-INF/jarslink-module-demo-1.0.0.jar
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>  


注意:不再需要:配置文件中配置bean信息。问题在于每次开发一个Action就要配置文件中加配置文件<beanid=”” name=”” class=”” />
/*
 *
 *  * 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"));
    }

}


对应的WelComeAction:
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);
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值