用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 ...
随机推荐
- linux包之e2fsprogs之chattr命令
概述 [root@localhost ~]# rpm -qf /usr/bin/chattre2fsprogs-1.41.12-18.el6.x86_64 chattr命令的作用很大,其中一些功能是由 ...
- asp.net中Cookie的用法【转】
比如建立一个名为aspcn,值为灌水小鱼的cookie HttpCookie cookie = new HttpCookie["aspcn"];cookie.Value = &qu ...
- docker nodejs 基本应用
1. 安装docker 环境 2. nodejs 应用布局 package.json { "name": "docker-centos-hello", &qu ...
- python之os模块
#!/usr/bin/env python3# _*_ coding:utf-8 _*_ import os os.getcwd()#os.chdir('path')print(os.curdir)p ...
- selenium+python自动化之元素定位
自动化按步骤拆分的话,可以分为四步操作:定位元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告.本篇接下来讲基本的八种元素定位方法.说的通俗一点,就是教大家找对象. ...
- eclipse color themes 让eclipse编码好看点
http://eclipsecolorthemes.org/ 就是这个,很好用的! 安装后需要重启Eclipse.重启后打开Window->Preferences->General-> ...
- 01TCP/IP基础
ISO/OSI参考模型: OSI(open system interconnection)开放系统互联模型是由ISO(International Organization for Standardiz ...
- Neutron分析(3)—— neutron-l3-agent
一.Layer-3 Networking Extension neutron l3作为一种API扩展,向租户提供了路由和NAT功能. l3扩展包含两种资源: router:在不同内部子网中转发数据包: ...
- bootstrap的datetimepicker只选择月份
本文转载自:http://blog.csdn.net/feng1603/article/details/41869523 直接上代码: //选择年月日的 startView: 2, minView: ...
- 【转】java 自动装箱与拆箱
java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...