

基于Springboot的Rpc服务端开发脚手架(base-grpc-framework)
source link: https://blog.51cto.com/arch/5688378
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

基于Springboot的Rpc服务端开发脚手架(base-grpc-framework)
推荐 原创经过一段时间的线上环境打磨和验证,重新优化了grpc专题中的框架,优化和扩展了很多功能。如果读者之前未关注过,建议先了解此系列中的: 快速开始 和 开始准备 这两小章。
- 专题系列课程: 从零开始搭建grpc分布式应用
- 框架完整源码: github源码,git访问慢可配置本机host添加 140.82.112.3 github.com和151.101.1.194 github.global.ssl.fastly.net
base-grpc-framework是笔者自行开发和封装的一个server脚手架,可直接用于单机和分布式服务端开发。主要集成了Springboot、Google-grpc、Mapstrut、Mybatis、hutool(开源工具包)等开源框架,并基于上述基础封装了最最基本的服务能力,并对每种能力都提供了抽象实现和配置。使用者可通过修改相关配置就可直接开发业务代码,主要能力包括如下:
- Tracklog:跟踪日志,为每请求生成唯一trackid,并贯穿请求到返回全链路;
- Interceptor:拦截器,提供了三个抽象的拦截器grpc-client、grpc-server、mvc,同时支持mvcExceptionAdvice;
- ModelBeanConvent:对象复制,只需一个接口声明就可实现bean对象的相互转换,包括proto与java对象间的转换;
- DataTool:数据操作,基于mybatis plug,主要扩展了插入和分页查询功能;
项目基于maven构建,脚手架中提供了一种默认的项目模块组织以及maven组织模式,更方便打包部署和多人协同开发。注:针对单机和分布式系统需自行删减相关模块。
二、模块介绍
- pom.xml:项目主maven文件,主要定义公共的配置以及版本控制;
- base-grpc-framework-common:项目工具包;
- base-grpc-framework-api:项目接口定义;
- base-grpc-framework-core:接口业务实现,提供了一些示例demo;
- base-grpc-framework-dao:数据库存储实现;
- base-grpc-framework-application:项目启动包装应用;
三、Quick Start
建议开发环境:JDK 1.8.0_144、Maven 3、Git;代码可从 github源码下载。
3.1、运行示例代码
2、初始化mysql表,示例中的初始化.sql文件位于 base-grpc-framework-dao/src/main/java/resources/ddl/init.sql中;
3、运行 base-grpc-framework-application 模块中的BaseFrameworkApplication.java类,需配置启动参数为active profiles=dev;
最后在浏览器中输入: http://localhost:18080/swagger-ui.html 就可以看到示例了。
3.2、开发自己的业务代码
3.2.1、修改配置
下列配置可配置到公司专用的配置数据库里。如果公司没有专用的配置库,建议用nacos来实现。下面文件是完整的配置,多数比较简单,读者可根据实际情况自行修改;
server:
port: 18080
compression:
enabled: true
mime-types: application/json,application/octet-stream
grpc:
server:
port: 9898 #发布远程访问地址
in-process-name: native #发布本地访问地址
client:
inProcess: #自定义的客户端名称
address: in-process:native #配置内部访问服务名称
enableKeepAlive: true
keepAliveWithoutCalls: true
logging:
config: classpath:log4j2.xml
level:
root: INFO
org.springframework.web: ERROR
app-properties:
token-key: token
http-api-path: /api/**
# spring配置
spring:
application:
name: GrpcFramework-Server-APP
aop:
auto: true
proxy-target-class: true
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/badCase?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 12345678
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
validation-query: SELECT 1 FROM DUAL
max-pool-prepared-statement-per-connection-size: 5
test-while-idle: true
test-on-borrow: false
test-on-return: false
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 100000
filters: stat
# mybatis-plug 配置
mybatis-plus:
mapper-locations: classpath:/mybatis/*Mapper.xml
configuration:
map-underscore-to-camel-case: true #开启驼峰命名规则映射
default-statement-timeout: 20000 #超时查询
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql日志
# swagger 自定义配置,为了扩展base-package功能
swagger-config:
enabled: true #正式环境需要设置为false
title: GrpcFramework-API
description: GrpcFramework-Server-Restful-API
version: V1.0
base-package: com.zd.baseframework.core.restful #多个base包可用逗号分隔
authorization-key-name: token
3.2.2、配置启动类
启动类源码如下,主要是把业务代码的配置添加到@SpringBootApplication、@MapperScan这两个注解中的值中(示例中SpringBootApplication中的值不要删除)
@SpringBootApplication(scanBasePackages = {"com.zd.baseframework", "cn.hutool.extra.spring"})
@MapperScan({"com.zd.baseframework"})
@EnableConfigurationProperties
@EnableScheduling
@EnableAsync
public class BaseFrameworkApplication {
public static void main(String []args){
TimeZone.setDefault(TimeZone.getTimeZone("CTT"));
SpringApplication.run(BaseFrameworkApplication.class, args);
}
}
3.2.3、修改拦截器(可选)
下面的拦截器不一定都需要,可按需扩展,也可直接采用笔者示例中的默认实现。
http-token拦截器,拦截器注册类com.zd.baseframework.core.grpc.interceptor.GloablInterceptorRegister
http-exception切面
grpc-token拦截器,拦截器注册类com.zd.baseframework.core.restful.interceptor.InterceptorRegister
- 赞
- 收藏
- 评论
- 分享
- 举报
</div
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK