花了点时间先把程序代码完全写完了,加了这个章节。因为后续章节是连续的,没有一个总纲同学们难免看的云里雾里的。本章先描述下完整的源码如何运行以及工程的结构。此专题大概由30章组成,真正的从0开始,框架是在原生产环境中抽取的,无坑无bug。
配套源码(可用于开发正式应用,需少量修改):github源码
配套的项目生成工具:github源码
一、准备工作
必装软件-开发:
- Intellij IDEA (任意版本,笔者用的是2022.1.2版);
- JDK 1.8.0_144;
- Maven 3;
- Git:建议安装;
- MySQL:(任意版本,笔者用的是8.0.2);
必装软件-测试:
- grpcui
二、如何学习
建议读者一步步实操一下,所有章节内容如下:
三、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,进行测试即可:
因截图太大了,笔者把测试用例文字描述一下:
测试增加功能:
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,会自动弹出浏览器窗口
测试增加功能:
测试查询功能:
五、完整工程结构
5.1、工程划分
- 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、所有源码文件
这些源码在后续章节会依次展开。