用Java开发gRPC服务的例子分析

时间:2023-11-22 16:02:14

本文的代码例子来自: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 

完整的生产内容如下:

用Java开发gRPC服务的例子分析

 

 

 

编译执行客户端和服务器代码

 

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/com/google/gradle/osdetector-gradle-plugin/1.4.0/osdetector-gradle-plugin-1.4.0.pom

Download https://repo1.maven.org/maven2/kr/motd/maven/os-maven-plugin/1.4.0.Final/os-maven-plugin-1.4.0.Final.pom

Download https://repo1.maven.org/maven2/com/google/gradle/osdetector-gradle-plugin/1.4.0/osdetector-gradle-plugin-1.4.0.jar

Download https://repo1.maven.org/maven2/kr/motd/maven/os-maven-plugin/1.4.0.Final/os-maven-plugin-1.4.0.Final.jar

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/kt3k/gradle/plugin/coveralls-gradle-plugin/2.0.1/coveralls-gradle-plugin-2.0.1.pom

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/httpcomponents-client/4.3/httpcomponents-client-4.3.pom

Download https://repo1.maven.org/maven2/org/apache/httpcomponents/project/7/project-7.pom

Download https://repo1.maven.org/maven2/org/codehaus/groovy/modules/http-builder/http-builder/0.7.1/http-builder-0.7.1.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/httpcomponents-core/4.3/httpcomponents-core-4.3.pom

Download https://repo1.maven.org/maven2/org/kt3k/gradle/plugin/coveralls-gradle-plugin/2.0.1/coveralls-gradle-plugin-2.0.1.jar

Download https://repo1.maven.org/maven2/org/apache/httpcomponents/httpmime/4.3/httpmime-4.3.jar

Download https://repo1.maven.org/maven2/org/codehaus/groovy/modules/http-builder/http-builder/0.7.1/http-builder-0.7.1.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

Download https://plugins.gradle.org/m2/be/insaneprogramming/gradle/animalsniffer-gradle-plugin/1.4.0/animalsniffer-gradle-plugin-1.4.0.pom

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://plugins.gradle.org/m2/gradle/plugin/me/champeau/gradle/jmh-gradle-plugin/0.3.0/jmh-gradle-plugin-0.3.0.jar

Download https://repo1.maven.org/maven2/com/google/gradle/osdetector-gradle-plugin/1.2.1/osdetector-gradle-plugin-1.2.1.jar

Download https://repo1.maven.org/maven2/kr/motd/maven/os-maven-plugin/1.2.3.Final/os-maven-plugin-1.2.3.Final.jar

Download https://repo1.maven.org/maven2/org/mortbay/jetty/alpn/alpn-boot/8.1.2.v20141202/alpn-boot-8.1.2.v20141202.pom

Download https://repo1.maven.org/maven2/org/mortbay/jetty/alpn/alpn-project/8.1.2.v20141202/alpn-project-8.1.2.v20141202.pom

Download https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-parent/23/jetty-parent-23.pom

Download https://repo1.maven.org/maven2/org/mortbay/jetty/alpn/alpn-boot/8.1.2.v20141202/alpn-boot-8.1.2.v20141202.jar

: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/signature/signatures-parent/1.1/signatures-parent-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/protobuf/protobuf-java-util/3.0.0-beta-2/protobuf-java-util-3.0.0-beta-2.pom

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/protobuf/protobuf-java-util/3.0.0-beta-2/protobuf-java-util-3.0.0-beta-2.jar

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/ 目录下产生编译好的文件。

用Java开发gRPC服务的例子分析

运行服务器端:

$ ./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