用Java开发gRPC服务的例子分析
本文的代码例子来自:https://github.com/grpc/grpc-java
定义服务
这一步与其他语言完全一样,需要定义gRPC的服务、方法、request和response的类型。
完整的proto定义代码在:grpc-java/examples/src/main/proto/route_guide.proto
在proto中,下面几个定义是为我们生成 Java 类所特有的定义。
option java_multiple_files = true;
option java_package = "io.grpc.examples.routeguide";
option java_outer_classname = "RouteGuideProto";
如果在 .proto 文件中没有显示的 java_package 参数,
那么就会使用缺省的 proto 包(通过 "package" 关键字指定)。但是,因为 proto 包一般不是以域名
翻转的格式命名,所以它不是好的 Java 包。 如果我们用其它语言通过 .proto 文件生成代码,java_package 是不起任何作用的。
定义四种方法(简单RPC、服务器端流式RPC、客户端流式RPC、双向流式RPC)的细节就不在重复,跟其他语言完全一样。
生成客户端和服务器端代码
从 .proto 的服务定义中生成 gRPC 客户端和服务器端的接口。我们通过 protocol buffer 的编译器 protoc 以及一个特殊的 gRPC Java 插件来完成。
这个工具请在 https://github.com/google/protobuf/releases 这里下载。
具体编译的方法,请参考: http://www.cnblogs.com/ghj1976/p/5391205.html
完整的生产内容如下:

编译执行客户端和服务器代码
https://github.com/grpc/grpc-java 例子的编译方法在:
https://github.com/grpc/grpc-java/blob/master/examples/README.md
编译例子,我们需要在 examples 目录运行下面命令:
$ ../gradlew installDist -PskipCodegen=true
执行的结果如下:
pwd
/Users/ghj1976/project/github/grpc/grpc-java/examples
localhost:examples ghj1976$
localhost:examples ghj1976$ ls
README.md android build.gradle src
localhost:examples ghj1976$
localhost:examples ghj1976$ ../gradlew installDist -PskipCodegen=true
Downloading https://services.gradle.org/distributions/gradle-2.11-bin.zip
........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Unzipping /Users/ghj1976/.gradle/wrapper/dists/gradle-2.11-bin/452syho4l32rlk2s8ivdjogs8/gradle-2.11-bin.zip to /Users/ghj1976/.gradle/wrapper/dists/gradle-2.11-bin/452syho4l32rlk2s8ivdjogs8
Set executable permissions for: /Users/ghj1976/.gradle/wrapper/dists/gradle-2.11-bin/452syho4l32rlk2s8ivdjogs8/gradle-2.11/bin/gradle
*** Skipping the build of codegen and compilation of proto files because skipCodegen=true
Download https://repo1.maven.org/maven2/org/apache/maven/maven-plugin-api/3.2.1/maven-plugin-api-3.2.1.jar
Download https://repo1.maven.org/maven2/org/apache/maven/maven-model/3.2.1/maven-model-3.2.1.jar
Download https://repo1.maven.org/maven2/org/apache/maven/maven-artifact/3.2.1/maven-artifact-3.2.1.jar
Download https://repo1.maven.org/maven2/org/codehaus/plexus/plexus-classworlds/2.4/plexus-classworlds-2.4.jar
Download https://repo1.maven.org/maven2/org/apache/httpcomponents/httpmime/4.3/httpmime-4.3.pom
Download https://repo1.maven.org/maven2/org/apache/httpcomponents/project/7/project-7.pom
Download https://repo1.maven.org/maven2/org/apache/httpcomponents/httpclient/4.3/httpclient-4.3.pom
Download https://repo1.maven.org/maven2/net/sf/json-lib/json-lib/2.3/json-lib-2.3.pom
Download https://repo1.maven.org/maven2/net/sourceforge/nekohtml/nekohtml/1.9.16/nekohtml-1.9.16.pom
Download https://repo1.maven.org/maven2/xml-resolver/xml-resolver/1.2/xml-resolver-1.2.pom
Download https://repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.8.0/commons-beanutils-1.8.0.pom
Download https://repo1.maven.org/maven2/net/sf/ezmorph/ezmorph/1.0.6/ezmorph-1.0.6.pom
Download https://repo1.maven.org/maven2/org/apache/httpcomponents/httpcore/4.3/httpcore-4.3.pom
Download https://repo1.maven.org/maven2/org/apache/httpcomponents/httpmime/4.3/httpmime-4.3.jar
Download https://repo1.maven.org/maven2/org/apache/httpcomponents/httpclient/4.3/httpclient-4.3.jar
Download https://repo1.maven.org/maven2/net/sf/json-lib/json-lib/2.3/json-lib-2.3-jdk15.jar
Download https://repo1.maven.org/maven2/net/sourceforge/nekohtml/nekohtml/1.9.16/nekohtml-1.9.16.jar
Download https://repo1.maven.org/maven2/xml-resolver/xml-resolver/1.2/xml-resolver-1.2.jar
Download https://repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.8.0/commons-beanutils-1.8.0.jar
Download https://repo1.maven.org/maven2/net/sf/ezmorph/ezmorph/1.0.6/ezmorph-1.0.6.jar
Download https://repo1.maven.org/maven2/org/apache/httpcomponents/httpcore/4.3/httpcore-4.3.jar
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/ghj1976/project/github/grpc/grpc-java/all/build.gradle' line: 32
* What went wrong:
A problem occurred evaluating project ':grpc-all'.
> A problem occurred configuring project ':grpc-auth'.
> Could not resolve all dependencies for configuration ':grpc-auth:classpath'.
> Could not download animalsniffer-gradle-plugin.jar (be.insaneprogramming.gradle:animalsniffer-gradle-plugin:1.4.0)
> Could not get resource 'https://plugins.gradle.org/m2/be/insaneprogramming/gradle/animalsniffer-gradle-plugin/1.4.0/animalsniffer-gradle-plugin-1.4.0.jar'.
> Could not GET 'https://plugins.gradle.org/m2/be/insaneprogramming/gradle/animalsniffer-gradle-plugin/1.4.0/animalsniffer-gradle-plugin-1.4.0.jar'.
> gradleware-plugins.s3.amazonaws.com:443 failed to respond
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 16 mins 47.903 secs
localhost:examples ghj1976$
这个build错误,是因为下载https://plugins.gradle.org时需要翻墙才能下载。设置翻墙后,设置本机代理:
export http_proxy=http://127.0.0.1:8787
既可以正常下载了。
localhost:examples ghj1976$
localhost:examples ghj1976$ export http_proxy=http://127.0.0.1:8787
localhost:examples ghj1976$
localhost:examples ghj1976$ ../gradlew installDist -PskipCodegen=true
*** Skipping the build of codegen and compilation of proto files because skipCodegen=true
Download https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-parent/23/jetty-parent-23.pom
:grpc-core:compileJava
Download https://repo1.maven.org/maven2/com/google/guava/guava/19.0/guava-19.0.pom
Download https://repo1.maven.org/maven2/com/google/guava/guava-parent/19.0/guava-parent-19.0.pom
Download https://repo1.maven.org/maven2/com/google/guava/guava/19.0/guava-19.0.jar
:grpc-core:animalSniffer
Download https://repo1.maven.org/maven2/org/codehaus/mojo/signature/java16/1.1/java16-1.1.pom
Download https://repo1.maven.org/maven2/org/codehaus/mojo/mojo-parent/23/mojo-parent-23.pom
Download https://repo1.maven.org/maven2/org/codehaus/mojo/signature/java16/1.1/java16-1.1.signature
:grpc-core:processResources UP-TO-DATE
:grpc-core:classes
:grpc-core:jar
:grpc-netty:compileJava
Download https://repo1.maven.org/maven2/io/netty/netty-codec-http2/4.1.0.CR7/netty-codec-http2-4.1.0.CR7.pom
Download https://repo1.maven.org/maven2/io/netty/netty-parent/4.1.0.CR7/netty-parent-4.1.0.CR7.pom
Download https://repo1.maven.org/maven2/io/netty/netty-codec-http/4.1.0.CR7/netty-codec-http-4.1.0.CR7.pom
Download https://repo1.maven.org/maven2/io/netty/netty-handler/4.1.0.CR7/netty-handler-4.1.0.CR7.pom
Download https://repo1.maven.org/maven2/io/netty/netty-codec/4.1.0.CR7/netty-codec-4.1.0.CR7.pom
Download https://repo1.maven.org/maven2/io/netty/netty-buffer/4.1.0.CR7/netty-buffer-4.1.0.CR7.pom
Download https://repo1.maven.org/maven2/io/netty/netty-transport/4.1.0.CR7/netty-transport-4.1.0.CR7.pom
Download https://repo1.maven.org/maven2/io/netty/netty-common/4.1.0.CR7/netty-common-4.1.0.CR7.pom
Download https://repo1.maven.org/maven2/io/netty/netty-resolver/4.1.0.CR7/netty-resolver-4.1.0.CR7.pom
Download https://repo1.maven.org/maven2/io/netty/netty-codec-http2/4.1.0.CR7/netty-codec-http2-4.1.0.CR7.jar
Download https://repo1.maven.org/maven2/io/netty/netty-codec-http/4.1.0.CR7/netty-codec-http-4.1.0.CR7.jar
Download https://repo1.maven.org/maven2/io/netty/netty-handler/4.1.0.CR7/netty-handler-4.1.0.CR7.jar
Download https://repo1.maven.org/maven2/io/netty/netty-codec/4.1.0.CR7/netty-codec-4.1.0.CR7.jar
Download https://repo1.maven.org/maven2/io/netty/netty-buffer/4.1.0.CR7/netty-buffer-4.1.0.CR7.jar
Download https://repo1.maven.org/maven2/io/netty/netty-transport/4.1.0.CR7/netty-transport-4.1.0.CR7.jar
Download https://repo1.maven.org/maven2/io/netty/netty-common/4.1.0.CR7/netty-common-4.1.0.CR7.jar
Download https://repo1.maven.org/maven2/io/netty/netty-resolver/4.1.0.CR7/netty-resolver-4.1.0.CR7.jar
:grpc-netty:processResources
:grpc-netty:classes
:grpc-netty:jar
:grpc-protobuf-lite:compileJava
:grpc-protobuf-lite:animalSniffer
:grpc-protobuf-lite:processResources UP-TO-DATE
:grpc-protobuf-lite:classes
:grpc-protobuf-lite:jar
:grpc-protobuf:compileJava
Download https://repo1.maven.org/maven2/com/google/code/gson/gson/2.3/gson-2.3.pom
Download https://repo1.maven.org/maven2/com/google/code/gson/gson/2.3/gson-2.3.jar
:grpc-protobuf:animalSniffer
:grpc-protobuf:processResources UP-TO-DATE
:grpc-protobuf:classes
:grpc-protobuf:jar
:grpc-stub:compileJava
:grpc-stub:animalSniffer
:grpc-stub:processResources UP-TO-DATE
:grpc-stub:classes
:grpc-stub:jar
:grpc-examples:compileJava
:grpc-examples:processResources
:grpc-examples:classes
:grpc-examples:jar
:grpc-examples:compressingHelloWorldClient
:grpc-examples:helloWorldClient
:grpc-examples:helloWorldServer
:grpc-examples:routeGuideClient
:grpc-examples:routeGuideServer
:grpc-examples:startScripts SKIPPED
:grpc-examples:installDist
BUILD SUCCESSFUL
Total time: 1 mins 44.412 secs
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.11/userguide/gradle_daemon.html
localhost:examples ghj1976$
命令执行完毕后,会在 build/install/grpc-examples/bin/ 目录下产生编译好的文件。

运行服务器端:
$ ./build/install/grpc-examples/bin/hello-world-server
在另外一个终端窗口运行客户端:
$ ./build/install/grpc-examples/bin/hello-world-client
参考资料:
http://doc.oschina.net/grpc?t=60134
http://www.grpc.io/docs/tutorials/basic/java.html
用Java开发gRPC服务的例子分析的更多相关文章
- Java 开发 gRPC 服务和客户端
新建一个普通的Maven项目: 配置pom文件,导入gRPC的依赖和插件 pom 中增加外部依赖 <dependency> <groupId>io.grpc</g ...
- grpc(3):使用 golang 开发 grpc 服务端和client
1,关于grpc-go golang 能够能够做grpc的服务端和client. 官网的文档: http://www.grpc.io/docs/quickstart/go.html https://g ...
- 十分钟学会Golang开发gRPC服务
gRPC是Google发起的一个开源RPC框架,使用HTTP/2传输协议,使用Protocol Buffers编码协议,相比RESTful框架的程序性能提高不少,而且当前流行的编程语言基本都已经支持. ...
- java做成windows服务,电子秤例子,开机自动启动
使用Java Service Wrapper工具制作 1.windows32位下载地址 https://sourceforge.net/projects/wrapper/files/ 2.window ...
- Java开发实践 集合框架 全面分析
http://www.open-open.com/lib/view/open1474167415464.html
- java版gRPC实战之二:服务发布和调用
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 【.NET6】gRPC服务端和客户端开发案例,以及minimal API服务、gRPC服务和传统webapi服务的访问效率大对决
前言:随着.Net6的发布,Minimal API成了当下受人追捧的角儿.而这之前,程序之间通信效率的王者也许可以算得上是gRPC了.那么以下咱们先通过开发一个gRPC服务的教程,然后顺势而为,再接着 ...
- 基于知名微服务框架go-micro开发gRPC应用程序
go-micro是golang的一个微服务框架. go-micro各个版本之间的兼容性问题一直被诟病,前几年go-micro更是分化出了两个分支: 一个延续了go-micro,只不过转到了其公司CEO ...
- java版gRPC实战之七:基于eureka的注册发现
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
随机推荐
- gnu-software
gnu coreutils https://www.gnu.org/software/coreutils/manual/coreutils.html#toc_Printing-text 目录列表 ht ...
- 虚拟化之vmware DirectPath I/O
首先Intel这边,必须CPU支持VT-D,基本就是i5/i7的型号,而且部分i5中低端型号还不支持,带K的不支持.同时主板要开启VT-D支持,这些从775时代开始就有了,所以型号搭配复杂.AMD那边 ...
- vs2010编译lua-5.3.2
lua-5.3.2.tar.gz 解开在 D:\lua 1. 启动VS命令行 2. c:\program files\microsoft visual studio 10.0\vc\bin>cd ...
- (五)Linux引导流程解析
目录 Linux引导流程 Linux运行级别 Linux启动服务管理 GRUB配置与应用 启动故障分析与解决 Linux引导流程 Linux系统引导流程如下图: 固件(Firmware)就是写入ERO ...
- 在数据库查询时解决大量in 关键字的方法
有时候在前台界面需要批量处理一些数据时,经常要用到update table set fields=value where keyid in ('1','2',....) 但如果这个数据量如果超过100 ...
- mysql中一对一,一对多,多对多关系
打个比方吧一个班级有很多学生,他们分别有不同的学号.一个学生对应一个学号,一个学号对应一个学生:这里学生和学号的关系就是一对一.一个班级有很多学生,这个班级只有一个班主任.一个班主任对应多个学生,多个 ...
- 基于spring-boot的web应用,ckeditor上传文件图片文件
说来惭愧,这个应用调试,折腾了我一整天,google了很多帖子,才算整明白,今天在这里做个记录和分享吧,也作为自己后续的参考! 第一步,ckeditor(本博文论及的ckeditor版本4.5.6)的 ...
- 本地同时启动两个tomcat
本地同时启动两个tomcat 这几天开发用到了Ext JS4,所以着手学习Ext JS4,由于官方很多demo都是需要与服务器端进行数据交互,因此需要在tomcat里部署上官方的demo.而本地mye ...
- 【linux】yum本地源制作
在/etc/yum.repos.d/ 目录下,有两个文件 CentOS-Base.repo和 CentOS-Media.repo 其中CentOS-Base.repo 记录着网络上的 yum 源的地 ...
- Hadoop:使用原生python编写MapReduce
功能实现 功能:统计文本文件中所有单词出现的频率功能. 下面是要统计的文本文件 [/root/hadooptest/input.txt] foo foo quux labs foo bar quux ...