基础环境准备
JDK | 11 |
SpringBoot | 2.3.7.RELEASE |
Maven |
二次开发的扩展包可以以正常的springboot项目进行启动,也可以放在lubase的extend目录,通过lubase-core 动态加载扩展包,详情见以下介绍。
一、快速开始
- 新建SpringBoot项目,版本2.3.7.RELEASE,设置JDK为11
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
- 添加maven依赖:lubase-core 需提前编译。建议 除 lubase-core 之外其他jar不指定版本,采用dependencyManagement中的版本声明
<dependencies>
<dependency>
<groupId>com.lubase</groupId>
<artifactId>lubase-core</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
- 调整dependencyManagement,指定版本
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 在项目添加application.yml,可从lubase项目进行复制
- 修改启动类,添加如下标记 @EnableCaching 和
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class }, scanBasePackages = "com.lubase.*")
- 修改打包配置,建议去除相配置文件,然后打包为瘦jar包
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<excludes>
<exclude>*.yml</exclude>
<exclude>*.yaml</exclude>
<exclude>templates/</exclude>
</excludes>
<archive>
<manifest>
<mainClass>com.lubase.extenddemo.ExtendDemoApplication</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 编写hello lubase
lubase 提供了很多个可供扩展的接口,用于不同的场景,详细见:lubase后端扩展接口
以下代码提供了一个最基本扩展方法
package com.lubase.extenddemo;
import com.lubase.core.extend.IInvokeMethod;
import org.springframework.stereotype.Component;
import java.util.HashMap;
@Component
public class HelloFromExtend implements IInvokeMethod {
@Override
public boolean checkRight() {
return false;
}
@Override
public Object exe(HashMap<String, String> hashMap) throws Exception {
return "hello lubase";
}
@Override
public String getDescription() {
return "测试扩展方法:hello lubase";
}
@Override
public String getId() {
// 设置唯一的方法id
return "1181294501122019328";
}
}
- 打包后启动程序即可进行验证
java -jar extend-demo.jar
POST /invoke/method HTTP/1.1
Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1Y28iOiJhZG1pbjEiLCJhdWQiOiI2ODgxNjQwNzA2ODcyNDgzODQiLCJ1bmEiOiJhZG1pbjEiLCJ1b3IiOiIxIiwiZXhwIjoxNzE4NjU1MjgzfQ.ZBWf7xDjouugGe9FCo40Riw6hKbEetFFJSvB4DvkKF8
User-Agent: Apipost/8 (https://www.apipost.cn)
Content-Type: application/json
Host: localhost:8084
Content-Length: 116
{
"pageId": "697491263502422016",
"appId": "671085014334574592",
"methodId": "1181294501122019328",
"data": {}
}
{
"success": 1,
"code": "1000",
"message": "处理成功",
"data": "hello lubase",
"timestamp": "1718629442660"
}
二、动态加载扩展包
场景:通常有这样一种情况,有多个应用都进行了二次开发,但是二开内容不多,同时不希望为每个应用部署一个后端服务,可以通过启动一个lubase-core 服务,动态加载这些应用的扩展jar包即可。
在yaml 文件中 custom.extend-path 属性指定了扩展目录,在启动lubase-core时(或者引用了lubase-core)会扫描扩展目录的jar并进行加载,参考如下: java -jar lubase-core.jar
完整的demo 详见:extend-demo: 这次后端二次开发的一个demo
LuBase低代码系列文章:
【开发工具自荐】LuBase 低代码开发框架介绍-可私有化部署-CSDN博客
【LuBase低代码框架】表单引擎字段类型设计-CSDN博客
【LuBase低代码框架】私有化部署后如何扩展文件存储?-CSDN博客
【LuBase低代码框架】5分钟快速构建一个应用?-CSDN博客
入群交流:
如果群二维码过期,请点击获取最新群二维码:LuBase私有化部署步骤&扫码入微信群 · Issue #I9U4LD · lubase/LuBase - Gitee.com