基于grpc从零开始搭建一个准生产分布式应用(系列)
  TEZNKK3IfmPf 2024年03月29日 69 0

花了点时间先把程序代码完全写完了,加了这个章节。因为后续章节是连续的,没有一个总纲同学们难免看的云里雾里的。本章先描述下完整的源码如何运行以及工程的结构。


配套DEMO源码(demo只供学习使用):​​​网盘下载​​ 提取码: s26r

经过优化的源码(可用于开发正式应用,需少量修改):​​github源码​​

配套的项目生成工具:​​github源码​​


必装软件-开发:

  • Intellij IDEA (任意版本,笔者用的是2022.1.2版);
  • JDK 1.8.0_144;
  • Maven 3;
  • Git:建议安装;
  • MySQL:(任意版本,笔者用的是8.0.2);

必装软件-测试:

  • grpcui

二、如何学习

建议读者一步步实操一下,所有章节内容如下:

专题名称

类型

知识点

​​​基于grpc从零开始搭建一个准生产分布式应用(0) - quickStart​​

主课程


​​​基于grpc从零开始搭建一个准生产分布式应用(1) - 开始准备​​​

主课程

架构设计

​​​基于grpc从零开始搭建一个准生产分布式应用(2) - 工程构建​​​

主课程

工程搭建,springboot集成与使用

​​​基于grpc从零开始搭建一个准生产分布式应用(3) - GRPC实现​​​

主课程

grpc框架集成使用

​​基于grpc从零开始搭建一个准生产分布式应用(4) - 01 - proto详解​​​

主课程

proto框架集成与使用

​​​基于grpc从零开始搭建一个准生产分布式应用(4) - 02 - grpc详解基础实现​​​

扩展课程


​​基于grpc从零开始搭建一个准生产分布式应用(4) - 03 - grpc详解流式服务​​

扩展课程


​​基于grpc从零开始搭建一个准生产分布式应用(4) - 04 - grpc框架级应用​​​

扩展课程


​​基于grpc从零开始搭建一个准生产分布式应用(4) - 05 - springGrpc框架​​​

扩展课程


​​基于grpc从零开始搭建一个准生产分布式应用(4) - 06 - springGrpc框架​​​

扩展课程


​​​基于grpc从零开始搭建一个准生产分布式应用(5) - MapStruct传输对象转换​​​

主课程

mapStruct框架集成使用

​​基于grpc从零开始搭建一个准生产分布式应用(6) - 01 - MapStruct基础​​​

扩展课程


​​基于grpc从零开始搭建一个准生产分布式应用(6) - 02 - MapStruct数据转换​​​

扩展课程


​​基于grpc从零开始搭建一个准生产分布式应用(6) - 03 - MapStruct高级映射​​​

扩展课程


​​基于grpc从零开始搭建一个准生产分布式应用(6) - 04 - MapStruct-proto映射​​​

扩展课程


​​基于grpc从零开始搭建一个准生产分布式应用(6) - 05 - MapStruct特殊实现​​​

扩展课程


​​基于grpc从零开始搭建一个准生产分布式应用(6) - 06 - MapStruct常见问题汇总​​​

扩展课程


​​​基于grpc从零开始搭建一个准生产分布式应用(7) - 01 - 附:GRPC拦截器源码​​​​

主课程

源代码

​​​基于grpc从零开始搭建一个准生产分布式应用(8) - 01 - 附:GRPC公共库源码​​​

主课程

源代码

​​基于grpc从零开始搭建一个准生产分布式应用(9) - Service业务逻辑层实现​​

主课程

springService实现

​​​基于grpc从零开始搭建一个准生产分布式应用(10) - Dao逻辑实现​​​

主课程

mybatis框架集成使用

​​​Mybatisplus-【1/3】基础​​​

扩展课程


​​​Mybatisplus-【2/3】基础操作​​​

扩展课程


​​​Mybatisplus-【3/3】高级操作​​​

扩展课程


​​​基于grpc从零开始搭建一个准生产分布式应用(11) - 专题-查询分页​​​

主课程

分页查询

​​​程序异常和日志的设计方法​​​

主课程

日志和异常设计

​​​基于grpc从零开始搭建一个准生产分布式应用(11) - 小结​​​

主课程

待扩展功能点

​​​常用的注解汇总​​​

扩展课程

lombok,json注解使用

​​​基于Springboot的Rpc服务端开发脚手架(base-grpc-framework)​​​

扩展课程

优化后的demo

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

主课程

配置的自定义项目生成工具

三、QuickStart

3.1、创建数据库

笔者源码中用的数据名为【badCase】,同学可以下载完源码后修改【base-grpc-framework-application】模块下的src/main/resources/application-dev.yml配置文件的下面约17行。

url: jdbc:mysql://127.0.0.1:3306/badCase?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai

3.2、启动工程

双击运行【base-grpc-framework-application】模块下的src/main/java/,配置jvm启动参数为dev

com.zd.baseframework.BaseFrameworkApplication.java

启动后控制台会输出下面的日志

已连接到目标 VM, 地址: ''127.0.0.1:52801',传输: '套接字''

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)

2022-06-15 21:20:48 CST [1] INFO com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils buildNacosConfigProperties 47 - nacosConfigProperties : NacosConfigProperties{serverAddr='127.0.0.1:8848', contextPath='null', encode='null', endpoint='null', namespace='null', accessKey='null', secretKey='null', ramRoleName='null', autoRefresh=false, dataId='null', dataIds='null', group='DEFAULT_GROUP', type=null, maxRetry='null', configLongPollTimeout='null', configRetryTime='null', enableRemoteSyncConfig=false, extConfig=[], bootstrap=Bootstrap{enable=false, logEnable=false}}
2022-06-15 21:20:48 CST [1] INFO com.alibaba.boot.nacos.config.autoconfigure.NacosConfigApplicationContextInitializer initialize 75 - [Nacos Config Boot] : The preload configuration is not enabled
2022-06-15 21:20:48 CST [1] INFO org.springframework.boot.StartupInfoLogger logStarting 55 - Starting BaseFrameworkApplication on MacBook with PID 28944 (/Users/liudong/personCode/java/base-grpc-framework/base-grpc-framework-application/target/classes started by liudong in /Users/liudong/personCode/java/base-grpc-framework)
2022-06-15 21:20:48 CST [1] INFO org.springframework.boot.SpringApplication logStartupProfileInfo 655 - The following profiles are active: dev
2022-06-15 21:20:49 CST [1] INFO org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker postProcessAfterInitialization 330 - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-06-15 21:20:49 CST [1] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer initialize 92 - Tomcat initialized with port(s): 8080 (http)
2022-06-15 21:20:49 CST [1] INFO org.apache.juli.logging.DirectJDKLog log 173 - Initializing ProtocolHandler ["http-nio-8080"]
2022-06-15 21:20:49 CST [1] INFO org.apache.juli.logging.DirectJDKLog log 173 - Starting service [Tomcat]
2022-06-15 21:20:49 CST [1] INFO org.apache.juli.logging.DirectJDKLog log 173 - Starting Servlet engine: [Apache Tomcat/9.0.29]
2022-06-15 21:20:50 CST [1] INFO org.apache.juli.logging.DirectJDKLog log 173 - Initializing Spring embedded WebApplicationContext
2022-06-15 21:20:50 CST [1] INFO net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration shadedNettyGrpcChannelFactory 152 - Detected grpc-netty-shaded: Creating ShadedNettyChannelFactory + InProcessChannelFactory
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
2022-06-15 21:20:50 CST [1] INFO com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure dataSource 56 - Init DruidDataSource
2022-06-15 21:20:50 CST [1] INFO com.alibaba.druid.pool.DruidDataSource init 994 - {dataSource-1} inited
Parsed mapper file: 'file [/Users/liudong/personCode/java/base-grpc-framework/base-grpc-framework-core/target/classes/mybatis/SystemLogMapper.xml]'
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.4.1
2022-06-15 21:20:51 CST [1] INFO springfox.documentation.spring.web.PropertySourcedRequestMappingHandlerMapping initHandlerMethods 69 - Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation(String, HttpServletRequest)]
2022-06-15 21:20:51 CST [1] INFO org.springframework.scheduling.concurrent.ExecutorConfigurationSupport initialize 171 - Initializing ExecutorService 'applicationTaskExecutor'
2022-06-15 21:20:51 CST [1] INFO net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration shadedNettyGrpcServerFactory 74 - Detected grpc-netty-shaded: Creating ShadedNettyGrpcServerFactory
2022-06-15 21:20:51 CST [1] INFO net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration inProcessGrpcServerFactory 157 - 'grpc.server.in-process-name' is set: Creating InProcessGrpcServerFactory
2022-06-15 21:20:51 CST [1] INFO springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper start 160 - Context refreshed
2022-06-15 21:20:51 CST [1] INFO springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper start 163 - Found 1 custom documentation plugin(s)
2022-06-15 21:20:51 CST [1] INFO springfox.documentation.spring.web.scanners.ApiListingReferenceScanner scan 41 - Scanning for api listing references
2022-06-15 21:20:52 CST [1] INFO net.devh.boot.grpc.server.serverfactory.AbstractGrpcServerFactory configureServices 108 - Registered gRPC service: com.zd.baseframework.core.api.sysrecord.ISystemLogService, bean: systemLogServiceApiImpl, class: com.zd.baseframework.core.core.systemlog.api.impl.SystemLogServiceApiImpl
2022-06-15 21:20:52 CST [1] INFO net.devh.boot.grpc.server.serverfactory.AbstractGrpcServerFactory configureServices 108 - Registered gRPC service: grpc.health.v1.Health, bean: grpcHealthService, class: io.grpc.protobuf.services.HealthServiceImpl
2022-06-15 21:20:52 CST [1] INFO net.devh.boot.grpc.server.serverfactory.AbstractGrpcServerFactory configureServices 108 - Registered gRPC service: grpc.reflection.v1alpha.ServerReflection, bean: protoReflectionService, class: io.grpc.protobuf.services.ProtoReflectionService
2022-06-15 21:20:52 CST [1] INFO net.devh.boot.grpc.server.serverfactory.GrpcServerLifecycle createAndStartGrpcServer 116 - gRPC Server started, listening on address: *, port: 9898
2022-06-15 21:20:52 CST [1] INFO net.devh.boot.grpc.server.serverfactory.AbstractGrpcServerFactory configureServices 108 - Registered gRPC service: com.zd.baseframework.core.api.sysrecord.ISystemLogService, bean: systemLogServiceApiImpl, class: com.zd.baseframework.core.core.systemlog.api.impl.SystemLogServiceApiImpl
2022-06-15 21:20:52 CST [1] INFO net.devh.boot.grpc.server.serverfactory.AbstractGrpcServerFactory configureServices 108 - Registered gRPC service: grpc.health.v1.Health, bean: grpcHealthService, class: io.grpc.protobuf.services.HealthServiceImpl
2022-06-15 21:20:52 CST [1] INFO net.devh.boot.grpc.server.serverfactory.AbstractGrpcServerFactory configureServices 108 - Registered gRPC service: grpc.reflection.v1alpha.ServerReflection, bean: protoReflectionService, class: io.grpc.protobuf.services.ProtoReflectionService
2022-06-15 21:20:52 CST [1] INFO net.devh.boot.grpc.server.serverfactory.GrpcServerLifecycle createAndStartGrpcServer 116 - gRPC Server started, listening on address: in-process:native, port: -1
2022-06-15 21:20:52 CST [1] INFO org.apache.juli.logging.DirectJDKLog log 173 - Starting ProtocolHandler ["http-nio-8080"]
2022-06-15 21:20:52 CST [1] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer start 204 - Tomcat started on port(s): 8080 (http) with context path ''
2022-06-15 21:20:52 CST [1] INFO org.springframework.boot.StartupInfoLogger logStarted 61 - Started BaseFrameworkApplication in 3.871 seconds (JVM running for 4.743)

四、测试

4.1、测试http接口

在浏览器中输入:http://localhost:8080/swagger-ui.html ,会看到如下页面,后展开controller,进行测试即可:

基于grpc从零开始搭建一个准生产分布式应用(系列)   

因截图太大了,笔者把测试用例文字描述一下:

测试增加功能:

curl -X GET "http://localhost:8080/systemlog/v1/create_systemlog?biz_id=bizTest3&code=user&custom_code=userDel&user_id=10000" -H "accept: */*"
{
"status": 0,
"message": "请求成功",
"data": null
}

测试查询功能:

curl -X POST "http://localhost:8080/systemlog/v1/list_systemlog" -H "accept: */*" -H "Content-Type: application/json" -d "{ \"biz_id\": \"bizTest2\"}"
{
"status": 0,
"message": "请求成功",
"data": [
{
"id": 1537061877920575500,
"biz_id": "bizTest2",
"user_id": 10000,
"track_uid": "10000",
"code": "user",
"custom_code": "userDel",
"state": 0,
"utime": "2022-06-15 13:18"
}
]
}

4.2、测试grpc接口

需先安装grpcui工具,然后在命令行输入:grpcui -plaintext 127.0.0.1:9898,会自动弹出浏览器窗口

测试增加功能:

基于grpc从零开始搭建一个准生产分布式应用(系列)

测试查询功能:

基于grpc从零开始搭建一个准生产分布式应用(系列)

五、完整工程结构

5.1、工程划分

基于grpc从零开始搭建一个准生产分布式应用(系列)

  • pom.xml:项目主maven文件,主要定义公共的配置以及版本控制;
  • .gitignore:用git时忽略的提供文件配置;
  • base-grpc-framework-common:项目工具包;
  • base-grpc-framework-api:项目接口定义;
  • base-grpc-framework-core:接口业务实现;
  • base-grpc-framework-dao:数据库存储实现;
  • base-grpc-framework-application:项目启动包装应用;
  • base-grpc-framework-client:web客户端;

5.2、所有源码文件

这些源码在后续章节会依次展开。

基于grpc从零开始搭建一个准生产分布式应用(系列)

基于grpc从零开始搭建一个准生产分布式应用(系列)

基于grpc从零开始搭建一个准生产分布式应用(系列)

基于grpc从零开始搭建一个准生产分布式应用(系列)

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年03月29日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月17日   46   0   0 JSpspring
  TEZNKK3IfmPf   2024年05月31日   28   0   0 分布式
TEZNKK3IfmPf