用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 ...
随机推荐
- wikioi 1973 Fibonacci数列【输出第N项的值】
/*===================================== 1978 Fibonacci数列 3 题目描述 Description 斐波纳契数列是这样的数列: f1 = 1 f2 ...
- 服务器安全狗,支持Linux/windows
安全狗: www.safedog.cn 防DDOS sql注入检测
- CGI相关概念
common gateway interface 通用网关接口 可以让客户端从浏览器向执行在服务器上的程序请求数据.CGI描述了客户端和服务器程序之间传输数据的一种标准. 编程语言perl是一种被广泛 ...
- Eclipse的中文字体设置
打开eclipse中文字体很小,简直难以辨认.在网上搜索发现这是由于Eclipse 用的字体是 Consolas,显示中文的时候默认太小了.解决方式有两种:一.把字体设置为Courier New 操 ...
- wcf 获取客户端 IP
http://stackoverflow.com/questions/3937773/wcf-security-using-client-ip-address var context = Operat ...
- 基于.net开发chrome核心浏览器
本文转载自:http://www.cnblogs.com/liulun/archive/2013/04/20/3031502.html 一: 上一篇的链接: 基于.net开发chrome核心浏览器[一 ...
- Node.js 相关资料网站汇总
地址:https://cnodejs.org/ nodejs中文网:http://nodejs.cn/ nodejs中文网:http://www.nodejs.net/ 相关API地址:http:// ...
- 事件日志ID 2511:服务器服务无法重新创建 <sharename> 共享关系,因为 <address> 目录已不再存在
服务器服务无法重新创建 QQMusicDownload 共享关系,因为 D:\QQMusic\QQMusicDownload 目录已不再存在.请运行 "net share QQMusicDo ...
- Spring进阶之路(1)-Spring核心机制:依赖注入/控制反转
原文地址:http://blog.csdn.net/wangyang1354/article/details/50757098 我们经常会遇到这样一种情景,就是在我们开发项目的时候经常会在一个类中调用 ...
- BIP_开发案例02_BI Publisher中复杂案例实现代码(案例)
2014-12-27 Created By BaoXinjian