用Java開發gRPC服務的例子分析

來源:互聯網
上載者:User

標籤:

本文的代碼例子來自: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/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時需要FQ才能下載。設定FQ後,設定本機代理:

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/ 目錄下產生編譯好的檔案。

運行伺服器端:

$ ./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服務的例子分析

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.