首页 > 其他分享 >基于Springboot的服务端开发脚手架-自动生成工具

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

时间:2022-09-29 20:00:52浏览次数:75  
标签:java Springboot grpc module GenEnum 模块 脚手架 restful 服务端

继之前的

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

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

一、如何使用

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

sh test.sh

截图如下:

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

二、生成自定义的工程

这一步建议选择一个预置的.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();
}

}





标签:java,Springboot,grpc,module,GenEnum,模块,脚手架,restful,服务端
From: https://blog.51cto.com/arch/5724003

相关文章

  • 支付宝沙箱服务 (结合springboot实现,这里对接的是easy版本,工具用的是IDEA,WebStrom)
    一:打开支付宝开发平台,登录,然后点击控制台https://open.alipay.com/   二:滚动到底部,选着沙箱服务   三:获取到对接要用的appId和公钥私钥    四......
  • 脚手架笔记
    笔记脚手架文件结构├──node_modules├──public│├──favicon.ico:页签图标│└──index.html:主页面├──src│├──assets:存放静态......
  • SpringBoot运维实用篇
    ​下面就从运维实用篇开始讲,在运维实用篇中,我给学习者的定位是玩转配置,为开发实用篇中做各种技术的整合做好准备工作。与开发实用篇相比,运维实用篇的内容显得略微单薄,......
  • SpringBoot基础篇
    JC-1.快速上手SpringBoot​ 学习任意一项技术,首先要知道这个技术的作用是什么,不然学完以后,你都不知道什么时候使用这个技术,也就是技术对应的应用场景。SpringBoot技术由Pi......
  • SpringBoot基础篇->运维实用篇-->开发实用篇->原理篇
    点击领取课程使用到的资料(阿里云盘)一、SpringBoot基础篇二、SpringBoot运维实用篇三、SpringBoot开发实用篇四、SpringBoot原理篇......
  • 基于 DolphinDB 搭建微服务的 SpringBoot 项目
    SpringBoot是一个基于Spring的快速开发框架,也是SpringCloud构建微服务分布式系统的基础设施。本文主要介绍如何通过SpringBoot快速搭建DolphinDB微服务,并且基于My......
  • Springboot自定义工具类中调用mapper或者service接口方式
    1、该类使用@Component注解2、添加一个本类类型的静态字段3、创建一个初始化方法,贴上@PostConstruct标签,用于注入bean4、创建方法调用mapper或service接口5、最后直接......
  • SpringBoot中写前端代码JSP、HTML、thymeleaf
    为什么springboot不能很好的支持jsp?:https://www.zhihu.com/question/61385975在springboot中使用jsp:https://blog.csdn.net/cflsup/article/details/123089542>>freemak......
  • MQTTnet3.1.2 服务端
    usingMQTT;usingMQTTnet.Client;usingMQTTnet.Protocol;usingMQTTnet;usingMQTTnet.Server;usingSystem;usingSystem.Collections.Generic;usingSystem.Te......
  • SpringBoot整合其他框架
    SpringBoot整合Junit实现步骤搭建SpringBoot工程引入starter-test起步依赖编写测试类添加测试相关注解@RunWith(SpringRunner.class)@SpringBootTest(classes=启......