基于Springboot的服务端开发脚手架-自动生成工具

继之前的

  • 专题系列课程: ​​从零开始搭建grpc分布式应用​​
  • 完整DEMO:​​基于Springboot的Rpc服务端开发脚手架(base-grpc-framework)​​

后带来一款项目自动手成工具(由于包路径等原因,完整demo想应用在实际开发中需要改很多代码),可以设置成自己公司的一些规定包路径等。

  • 自动生成工具源码地址:​​源码下载​​

此为一工具,默认生成java服务工程,但可经过配置生成其它类型的工程或目录结构,如python、recat等。此工具中自带了之前课程中提到的dao操作、http拦截、tracklog日志跟踪、exception切面处理等所有功能。可节省大量的搭建工程和基础工作的开发时间。而且生成的项目文件源文件,可进行二次修改。

一、如何使用

建议下载ReleasesV1.0,解压后先运行包中自带的test.sh脚本,会在解压目录的/output下生成三个预置好的三个工程,restful、grpc、fixed(restful+grpc)。

sh test.sh

截图如下:

基于Springboot的服务端开发脚手架-自动生成工具_第1张图片

二、生成自定义的工程

这一步建议选择一个预置的.setting文件进行修改,配置文件说明如下:

2.1、修改project.setting文件

# project config
project.name = base-grpc-framework   #工程名称
project.groupId = com.zd   #对应pom文件的groupId,全工程所有模块统一
project.version = 1.0-SNAPSHOT #对应pom文件的groupId,全工程所有模块统一
project.packaging = jar  #工程打包类型

project.basePackage = com.zd.baseframework  #源码根目录

project.app.tokenKey = token  #token的key,用于http拦截器访问端验证用
project.app.apiPath = /api/** #用于http拦截器使用

#module config
project.modules = api, proto, app, common, core, dao #所有模块名称

#模块类型,可选的有 proto, api, application, common, restful, grpc, fixed,persistence
#PS:一个完整工程最好的实践是由API, SERVER, COMMON, APP, DAO 5个大类型的模块组成,所以在同一个配置文件中会有如下互斥约束
#API模块:(proto, api)二选一,
#SERVER模块:restful, grpc, fixed 三选一,
#必要模块:application,启动
#可选模块:persistence, common 持久化和工具类
api.module.type = api 
api.module.artifactId = base-grpc-framework-api

proto.module.type = proto
proto.module.artifactId = base-grpc-framework-proto

common.module.type = common
common.module.artifactId = base-grpc-framework-common

app.module.type = application
app.module.artifactId = base-grpc-framework-app

core.module.type = fixed
core.module.artifactId = base-grpc-framework-core
core.moudle.port = 18080 #http服务端口
core.moudle.grpc.port = 9090 #grpc服务端口

dao.module.type = persistence
dao.module.artifactId = base-grpc-framework-dao
dao.module.mysql.dbIp = 127.0.0.1
dao.module.mysql.dbPort = 3306
dao.moudle.mysql.dbName = badCase
dao.module.mysql.dbUsername = root
dao.module.mysql.dbPassword = 12345678
dao.module.mysql.mapper = mybatis #mapper文件存放位置

2.2、生成自定义项目

java -jar grpc-framework-project-generator-1.0-SNAPSHOT-jar-with-dependencies.jar [setting=project_fixed.setting] [slt=/slt/source/]
setting默认值为:project.setting
slt默认值为:/slt/source/ 注意最后的/不要丢掉

2.3、打包运行

需要本机装有maven,执行以下命令:

cd /output/xxxxProject
mvn clean install -Dmaven.test.skip=true
mvn clean package -Dmaven.test.skip=true
java -jar -Dspring.profiles.active=dev xxxxProject.jar

# Step3: Test
open http://localhost:18080/swagger-ui.html

比如上述文件中

  1. project.name = base-grpc-framework
  2. project.version = 1.0-SNAPSHOT
  3. app.module.artifactId = base-grpc-framework-app

则执行以下命令:

cd /output/base-grpc-framework
mvn clean install -Dmaven.test.skip=true
mvn clean package -Dmaven.test.skip=true
java -jar -Dspring.profiles.active=dev 1. base-grpc-framework-app-1. 1.0-SNAPSHOT.jar

# Step3: Test
open http://localhost:18080/swagger-ui.html

三、配置符合公司个性的默认工程

上述工具中笔者内置了一些类和文件,这些文件是可以被编辑和替换的,也可基于笔者内置的类来扩展。

3.1、扩展基础文件

扩展配置文件 slt/source/sltext.json,因工具本身内置了一些java实现,基于规范的述求一般公司对这些文件是不允许修改的,如果使用部门有个性化需求的话,比如增加一个基础类,就可以通过这个功能来实现个性化和扩展但又不影响基础版本。

{
  "proto": [
    {"name": "GrpcAccessInterceptor.java", "packagePath": "/grpc/interceptor", "fileType": "source", "fileOperatorType": "create"}
  ]
}
[options]
moduleType: proto, application, persistence, common, restful, grpc, fixed
name: fileName
packagePath: file path 
fileType:  source, yml, xml or config
fileOperatorType: create, copy or append

3.2、重新制定基础文件

基础文件存储路径:slt/source,笔者现在的版本有一个限制就是内置的文件不允许缺失只能改写,否则会报错。如果想配置成公司内部的文件或对基础文件有修改,可以下载笔者的源码后进行修改,或是把源码中所有的配置全部删除,全部用sltext.json的方式来实现

  1. 源码修改位置:

com.zd.tools.project.generator.model.module 包中的所有文件

下面类用于生成src/main和src/resources目录,如果不需要的话可以注释掉其中的代码
com.zd.tools.project.generator.analysis.process.SettingFileConvert.configOwnAttr()

2、源码修改说明,可根据文件名对应各个类型模块的配置

ModuleApi.java          :对应api类型的模块
ModuleApplication.java  :对应application类型的模块
ModuleCommon.java        :对应common类型的模块
ModuleFixed.java        :对应fixed类型的模块
ModuleGrpc.java          :对应grpc类型的模块
ModulePersistence.java  :对应persistence类型的模块
ModuleProto.java        :对应proto类型的模块
ModuleRestful.java      :对应restful类型的模块

源码如下:

public class ModuleRestful extends AbstractModule {

    private String port;
   
    //预生成目录结构
    @Override
    public void configOwnDir() {
        super.configOwnDir();
        getDirs().add(getPackagePath() + File.separator + "server");
        getDirs().add(getPackagePath() + File.separator + "restful");
        getDirs().add(getPackagePath() + File.separator + StrFormatterUtil.replaceSlash("restful/config"));
        getDirs().add(getPackagePath() + File.separator + StrFormatterUtil.replaceSlash("restful/advice"));
        getDirs().add(getPackagePath() + File.separator + StrFormatterUtil.replaceSlash("restful/interceptor"));
        getDirs().add(getPackagePath() + File.separator + StrFormatterUtil.replaceSlash("restful/model"));
    }

    //预生成文件
    @Override
    public void configOwnSourceFile(){
        super.configOwnSourceFile();
        getSourceFiles().add(new SourceFile("log4j2.xml",                      getResourcesPath(), "", GenEnum.fileType.config, GenEnum.fileOperatorType.copy));

        getSourceFiles().add(new SourceFile("RestfulHttpExceptionAdvice.java", getPackagePath(), StrFormatterUtil.replaceSlash("/restful/advice"),      GenEnum.fileType.source, GenEnum.fileOperatorType.create));
        getSourceFiles().add(new SourceFile("RestfulAccessInterceptor.java",   getPackagePath(), StrFormatterUtil.replaceSlash("/restful/interceptor"), GenEnum.fileType.source, GenEnum.fileOperatorType.create));
        getSourceFiles().add(new SourceFile("RestfulInterceptorRegister.java", getPackagePath(), StrFormatterUtil.replaceSlash("/restful/interceptor"), GenEnum.fileType.source, GenEnum.fileOperatorType.create));
        getSourceFiles().add(new SourceFile("RestfulTokenInterceptor.java",    getPackagePath(), StrFormatterUtil.replaceSlash("/restful/interceptor"), GenEnum.fileType.source, GenEnum.fileOperatorType.create));
        getSourceFiles().add(new SourceFile("SwaggerConfig.java",              getPackagePath(), StrFormatterUtil.replaceSlash("/restful/config"),      GenEnum.fileType.source, GenEnum.fileOperatorType.copy));

        getSourceFiles().add(new SourceFile("application-restful.properties",getResourcesPath(), "", GenEnum.fileType.yml, GenEnum.fileOperatorType.append));

        getSourceFiles().add(new SourceFile("pom_restful.xml", getBasePath() ,"", GenEnum.fileType.xml, GenEnum.fileOperatorType.copy));
    }
}

3.3、编写基础文件

基础文件可参考笔者预置的文件进行修改,主要是用占位符替换.java文件中的package, import和className。示例如下:

package ${package};  #固定写法

#common为模块类型(proto, api, application, common, restful, grpc, fixed,persistence)中选一个, 
# "Module"为固定后缀,例子中${commonModule}表示引入common模块的一个类
import ${commonModule}.spring.ApperProperties;  
import ${commonModule}.spring.grpc.AbstractGrpcAccessInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Title: ${package}.${className}
 * @Description
 * @author liudong
 * @date 2022/1/13 4:40 PM
 */
@Slf4j
public class ${className} extends AbstractGrpcAccessInterceptor { #固定写法

    @Autowired
    private ApperProperties apperProperties;

    @Override
    protected String tokenKey() {
        return apperProperties.getTokenKey();
    }

}

你可能感兴趣的:(python,开发语言,java,spring,spring,boot,junit,log4j)