Example analysis of developing GRPC services with Java

Source: Internet
Author: User
Tags stack trace

The code examples in this article come from: Https://github.com/grpc/grpc-java

Defining services

This step is exactly the same as in other languages and requires defining the types of GRPC services, methods, request, and response.

The complete proto definition code is in: Grpc-java/examples/src/main/proto/route_guide.proto

In Proto, the following definitions are for us to generate specific definitions of Java classes.

Option Java_multiple_files = true;
Option Java_package = "Io.grpc.examples.routeguide";
Option Java_outer_classname = "Routeguideproto";

If the Java_package parameter is not displayed in the. proto file,
The default proto package (specified through the "packages" keyword) is then used. However, because the proto package is generally not a domain name
The flipped format is named, so it's not a good Java package. If we generate code in other languages through the. Proto file, Java_package is useless.

The details of defining four methods (simple RPC, server-side streaming RPC, client streaming RPC, bidirectional streaming RPC) are not duplicated, exactly the same as in other languages.

Generate client and server-side code

Generate GRPC client and server-side interfaces from the service definition for. Proto. We do this by protocol buffer's compiler protoc and a special grpc Java plugin.

Please download this tool here at https://github.com/google/protobuf/releases.

For specific compiling methods, please refer to: http://www.cnblogs.com/ghj1976/p/5391205.html

The complete production content is as follows:

Compiling execution of client and server code

The Https://github.com/grpc/grpc-java examples are compiled in the following ways:

Https://github.com/grpc/grpc-java/blob/master/examples/README.md

To compile the example, we need to run the following command in the examples directory:

$ ../gradlew installDist -PskipCodegen=true

The results of the implementation are as follows:

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$

This build error is due to the need for FQ to download https://plugins.gradle.org. After setting FQ, set the native agent:

Export http_proxy=http://127.0.0.1:8787

It can be downloaded as normal.

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 is faster, please consider using the Gradle Daemon:https://docs.gradle.org/2.11/userguide/gradle_daemon . html
Localhost:examples ghj1976$

When the command finishes executing, the compiled file is generated in the build/install/grpc-examples/bin/directory.

Run Server side:

$ ./build/install/grpc-examples/bin/hello-world-server

Run the client in a different terminal window:

$ ./build/install/grpc-examples/bin/hello-world-client

Resources:

http://doc.oschina.net/grpc?t=60134

Http://www.grpc.io/docs/tutorials/basic/java.html

Example analysis of developing GRPC services with Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.