运行grpc示例

2019-02-20  本文已影响0人  nextliving

下载源码

执行git clone -b v1.18.0 https://github.com/grpc/grpc-java下载相关源码,然后执行cd grpc-java/examples进入要执行后续操作的文件夹。

编译

首先执行./gradlew installDist进行编译。

出现错误:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/chenxin/Workspaces/grpc/grpc-java/examples/build.gradle' line: 14

* What went wrong:
A problem occurred evaluating root project 'examples'.
> Cannot resolve placeholder 'HOME' in value '${HOME}/.m2/repository'

打开build.gradle文件,发现第14行的内容是mavenLocal(),也就是使用了maven的本地仓库,本地仓库的定义在~/.m2/setting.xml中:<localRepository>${HOME}/.m2/repository</localRepository>
mac上修改为<localRepository>$HOME/.m2/repository</localRepository>问题即可解决。

继续编译,又出现问题:

> Task :authClient FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':authClient'.
> Could not resolve all files for configuration ':runtimeClasspath'.
   > Could not download grpc-netty-shaded.jar (io.grpc:grpc-netty-shaded:1.18.0
      > Could not get resource 'https://maven-central.storage-download.googleapis.com/repos/central/data/io/grpc/grpc-netty-shaded/1.18.0/grpc-netty-shaded-1.18.0.jar'.
         > SSL peer shut down incorrectly
   > Could not download protobuf-java.jar (com.google.protobuf:protobuf-java:3.5.1)
      > Could not get resource 'https://maven-central.storage-download.googleapis.com/repos/central/data/com/google/protobuf/protobuf-java/3.5.1/protobuf-java-3.5.1.jar'.
         > SSL peer shut down incorrectly
   > Could not download guava.jar (com.google.guava:guava:25.1-android)
      > Could not get resource 'https://maven-central.storage-download.googleapis.com/repos/central/data/com/google/guava/guava/25.1-android/guava-25.1-android.jar'.
         > SSL peer shut down incorrectly
   > Could not download proto-google-common-protos.jar (com.google.api.grpc:proto-google-common-protos:1.12.0)
      > Could not get resource 'https://maven-central.storage-download.googleapis.com/repos/central/data/com/google/api/grpc/proto-google-common-protos/1.12.0/proto-google-common-protos-1.12.0.jar'.
         > SSL peer shut down incorrectly
   > Could not download opencensus-api.jar (io.opencensus:opencensus-api:0.18.0)
      > Could not get resource 'https://maven-central.storage-download.googleapis.com/repos/central/data/io/opencensus/opencensus-api/0.18.0/opencensus-api-0.18.0.jar'.
         > SSL peer shut down incorrectly

这是因为google被域名被和谐,需要使用vpn连接到google的服务器下载依赖的jar包。
解决上面的问题后,最后会出现编译成功的标志:

> Task :compileJava
warning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning

BUILD SUCCESSFUL in 1m 3s
14 actionable tasks: 7 executed, 7 up-to-date

运行

执行./build/install/examples/bin/hello-world-server启动grpc服务端:

Feb 20, 2019 10:40:34 AM io.grpc.examples.helloworld.HelloWorldServer start
INFO: Server started, listening on 50051

打开一个新的terminal,执行./build/install/examples/bin/hello-world-client启动grpc客户端:

Feb 20, 2019 10:47:43 AM io.grpc.examples.helloworld.HelloWorldClient greet
INFO: Will try to greet world ...
Feb 20, 2019 10:47:44 AM io.grpc.examples.helloworld.HelloWorldClient greet
INFO: Greeting: Hello world

参考

上一篇下一篇

猜你喜欢

热点阅读