java版gRPC实战之一:用proto生成代码
Posted 程序员欣宸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java版gRPC实战之一:用proto生成代码相关的知识,希望对你有一定的参考价值。
欢迎访问我的GitHub
明确依赖库和开发环境
整个系列文章涉及的依赖库和开发环境的情况如下:
- JDK:1.8.0_281
- gradle:6.7.1
- springboot:2.3.8.RELEASE
- grpc:1.35.0
- protobuf:3.14.0
- grpc-server-spring-boot-starter:2.11.0.RELEASE
- grpc-client-spring-boot-starter:2.11.0.RELEASE
- 操作系统:win10专业版
- IDEA:2021.1 (Ultimate Edition)
源码下载
- 本篇实战中的完整源码可在GitHub下载到,地址和链接信息如下表所示(https://github.com/zq2599/blog_demos):
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
- 这个git项目中有多个文件夹,《java版gRPC实战》系列的源码在==grpc-tutorials==文件夹下,如下图红框所示:
创建《java版gRPC实战》系列的父工程
- 新建名为==grpc-tutorials==的gradle工程,前面提到的库及其版本都在此工程中处理好,build.gradle内容如下:
import java.time.OffsetDateTime import java.time.format.DateTimeFormatter
buildscript
repositories
maven
url https://plugins.gradle.org/m2/
// 如果有私服就在此配置,如果没有请注释掉
maven
url http://192.168.50.43:8081/repository/aliyun-proxy/
// 阿里云
maven
url http://maven.aliyun.com/nexus/content/groups/public/
mavenCentral()
ext
// 项目版本
projectVersion = 1.0-SNAPSHOT
// 依赖库的版本
grpcSpringBootStarterVersion = 2.11.0.RELEASE
// grpc版本 https://github.com/grpc/grpc-java/releases
grpcVersion = 1.35.0
// protobuf版本 https://github.com/protocolbuffers/protobuf/releases
protobufVersion = 3.14.0
// protobuf的gradle插件版本
protobufGradlePluginVersion = 0.8.12
// sprignboot版本 https://github.com/spring-projects/spring-boot/releases
springBootVersion = 2.3.8.RELEASE
// springcloud版本 https://github.com/spring-cloud/spring-cloud-release/releases
springCloudVersion = Hoxton.SR9
// nacos版本 https://github.com/alibaba/spring-cloud-alibaba/releases
springCloudAlibabaNacosVersion = 2.2.3.RELEASE
// security版本 https://github.com/spring-projects/spring-security-oauth/releases
springSecurityOAuthVersion = 2.5.0.RELEASE
plugins
id java
id java-library
id org.springframework.boot version "$springBootVersion" apply false
id io.spring.dependency-management version 1.0.11.RELEASE
id net.nemerosa.versioning version 2.14.0
id com.google.protobuf version 0.8.14
id io.franzbecker.gradle-lombok version 4.0.0 apply false
id com.github.ben-manes.versions version 0.36.0 // gradle dependencyUpdates
// If you attempt to build without the --scan
parameter in gradle 6.0+
it will cause a build error that it cant find
// a buildScan property to change. This avoids that problem.
if (hasProperty(buildScan))
buildScan
termsOfServiceUrl = https://gradle.com/terms-of-service
termsOfServiceAgree = yes
wrapper
gradleVersion = 6.7.1
def buildTimeAndDate = OffsetDateTime.now()
ext
// 构建时取得当前日期和时间
buildDate = DateTimeFormatter.ISO_LOCAL_DATE.format(buildTimeAndDate)
buildTime = DateTimeFormatter.ofPattern(HH:mm:ss.SSSZ).format(buildTimeAndDate)
buildRevision = versioning.info.commit
allprojects
apply plugin: java
apply plugin: idea
apply plugin: eclipse
apply plugin: io.spring.dependency-management
apply plugin: io.franzbecker.gradle-lombok
compileJava
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
options.encoding = UTF-8
compileJava.options*.compilerArgs = [
-Xlint:all, -Xlint:-processing
]
// Copy LICENSE
tasks.withType(Jar)
from(project.rootDir)
include LICENSE
into META-INF
// 写入到MANIFEST.MF中的内容
jar
manifest
attributes(
Created-By: "$System.properties[java.version] ($System.properties[java.vendor] $System.properties[java.vm.version])".toString(),
Built-By: travis,
Build-Date: buildDate,
Build-Time: buildTime,
Built-OS: "$System.properties[os.name]",
Build-Revision: buildRevision,
Specification-Title: project.name,
Specification-Version: projectVersion,
Specification-Vendor: Will Zhao,
Implementation-Title: project.name,
Implementation-Version: projectVersion,
Implementation-Vendor: Will Zhao
)
repositories
mavenCentral()
// 如果有私服就在此配置,如果没有请注释掉
maven
url http://192.168.50.43:8081/repository/aliyun-proxy/
// 阿里云
maven
url http://maven.aliyun.com/nexus/content/groups/public/
jcenter()
buildscript
repositories
maven url https://plugins.gradle.org/m2/
allprojects project ->
buildscript
dependencyManagement
imports
mavenBom "org.springframework.boot:spring-boot-starter-parent:$springBootVersion"
mavenBom "org.springframework.cloud:spring-cloud-dependencies:$springCloudVersion"
mavenBom "com.google.protobuf:protobuf-bom:$protobufVersion"
mavenBom "io.grpc:grpc-bom:$grpcVersion"
mavenBom "org.junit:junit-bom:5.7.0"
dependencies
dependency org.projectlombok:lombok:1.16.16
dependency org.apache.commons:commons-lang3:3.11
dependency commons-collections:commons-collections:3.2.2
dependency "net.devh:grpc-server-spring-boot-starter:$grpcSpringBootStarterVersion"
dependency "net.devh:grpc-client-spring-boot-starter:$grpcSpringBootStarterVersion"
ext
micrometerVersion = dependencyManagement.importedProperties[micrometer.version]
springFrameworkVersion = dependencyManagement.importedProperties[spring-framework.version]
springSecurityVersion = dependencyManagement.importedProperties[spring-security.version]
springCloudCommonsVersion = dependencyManagement.importedProperties[spring-cloud-commons.version]
group = com.bolingcavalry
version = projectVersion
- 整个系列用到的父工程已经完成,接下来可以开始编码了;
### 实战用proto文件自动生成java代码
- gRPC服务能够用不同的语言编写,其中的关键是定义服务的proto文件可以被生成各种语言的代码,java也不例外,接下来一起体验;
- 在父工程==grpc-tutorials==下新建模块,名为==grpc-lib==,其build.gradle内容如下,可见主要是配置了protobuf插件,以及生成的java代码如何才能被IDE工具加入到source path中:
```groovy
// 根据proto生成java代码的gradle插件
plugins
id com.google.protobuf
dependencies
implementation io.grpc:grpc-netty-shaded
implementation io.grpc:grpc-protobuf
implementation io.grpc:grpc-stub
if (JavaVersion.current().isJava9Compatible())
// Workaround for @javax.annotation.Generated
// see: https://github.com/grpc/grpc-java/issues/3633
implementation jakarta.annotation:jakarta.annotation-api
protobuf
protoc
artifact = "com.google.protobuf:protoc:$protobufVersion"
// 自动生成的代码放在这里
generatedFilesBaseDir = "$projectDir/src/generated"
clean
delete generatedFilesBaseDir
// 生成java代码的插件
plugins
grpc
artifact = io.grpc:protoc-gen-grpc-java
generateProtoTasks
all()*.plugins
grpc
// 对于eclipse,通过以下脚本可以将生成的代码加入source路径中,编译时会被用到
eclipse
classpath
file.beforeMerged cp ->
def generatedGrpcFolder = new org.gradle.plugins.ide.eclipse.model.SourceFolder(src/generated/main/grpc, null);
generatedGrpcFolder.entryAttributes[ignore_optional_problems] = true;
cp.entries.add( generatedGrpcFolder );
def generatedJavaFolder = new org.gradle.plugins.ide.eclipse.model.SourceFolder(src/generated/main/java, null);
generatedJavaFolder.entryAttributes[ignore_optional_problems] = true;
cp.entries.add( generatedJavaFolder );
// 对于idea,通过以下脚本可以将生成的代码加入source路径中,编译时会被用到
idea
module
sourceDirs += file(src/generated/main/java)
sourceDirs += file(src/generated/main/grpc)
generatedSourceDirs += file(src/generated/main/java)
generatedSourceDirs += file(src/generated/main/grpc)
- 在grpc-lib模块的==src/main/proto==目录下新增名为==helloworld.proto==的文件,这里面定义了一个gRPC服务,里面含有一个接口,并且还有这个接口的入参和返回结果的定义:
syntax = "proto3";
option java_multiple_files = true;
// 生成java代码的package
option java_package = "com.bolingcavalry.grpctutorials.lib";
option java_outer_classname = "HelloWorldProto";
// gRPC服务
service Simple
// 接口定义
rpc SayHello (HelloRequest) returns (HelloReply)
// 入参的数据结构
message HelloRequest
string name = 1;
// 返回结果的数据结构
message HelloReply
string message = 1;
- proto文件已经做好,接下来要根据这个文件来生成java代码,在==grpc-tutorials==目录下执行命令==gradle grpc-lib:generateProto==,即可根据helloworld.proto文件生成java代码,执行成功后会生出下图红框中的内容,这些就是java代码:
![在这里插入图片描述](https://s4.51cto.com/images/blog/202112/23081042_61c3be826207538379.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- 本篇只聊如何生成上述代码,至于这些代码的用途就留到下一篇再说吧,这里只简单提一下,SimpleGrpc里面有抽象类SimpleImplBase,制作gRPC服务的时候需要继承该类,另外,如果您要远程调用gRPC的sayHello接口,就会用到SimpleGrpc类中的SimpleStub类,其余的HelloReply、HelloRequest这些则是入参和返回的数据结构定义;
- 至此,java版gRPC实战的准备工作就完成了,根据proto文件生成java代码的方法也掌握了,接下来的章节咱们一起尝试服务的发布和调用;
### 欢迎关注公众号:程序员欣宸
> 微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
以上是关于java版gRPC实战之一:用proto生成代码的主要内容,如果未能解决你的问题,请参考以下文章