设为首页 收藏本站
查看: 3649|回复: 1

[经验分享] protoc的protoc-gen-grpc-java插件

[复制链接]

尚未签到

发表于 2017-2-28 08:02:57 | 显示全部楼层 |阅读模式
  编译 protoc-gen-grpc-java插件 的文档在:   
https://github.com/grpc/grpc-java/tree/master/compiler
  编译的步骤:
  Change to the compiler directory:
$ cd $GRPC_JAVA_ROOT/compiler

  To compile the plugin:

$ ../gradlew java_pluginExecutable
  To test the plugin with the compiler:

$ ../gradlew java_pluginExecutable

  编译过程中如果出现下面错误: fatal error: 'google/protobuf/io/zero_copy_stream.h' file not found。
  是因为找不到这些头文件。找不到的原因如下:
  Some versions of Mac OS X (e.g., 10.10) doesn't have /usr/local in the default search paths for header files and libraries.
  It will fail the build of the codegen. To work around this, you will need to set environment variables:

$ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"

  参考: https://github.com/grpc/grpc-java/blob/master/COMPILING.md
  错误的时候提示信息:
  $ ../gradlew java_pluginExecutable
Download https://repo1.maven.org/maven2/org/mortbay/jetty/alpn/jetty-alpn-agent/2.0.2/jetty-alpn-agent-2.0.2.pom
Download https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-parent/25/jetty-parent-25.pom
Download https://repo1.maven.org/maven2/org/mortbay/jetty/alpn/jetty-alpn-agent/2.0.2/jetty-alpn-agent-2.0.2.jar
*** Building codegen requires Protobuf version 3.0.0-beta-2
*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin
:grpc-compiler:compileJava_pluginExecutableJava_pluginCpp
/Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_generator.h:8:10: fatal error: 'google/protobuf/io/zero_copy_stream.h' file not found
#include <google/protobuf/io/zero_copy_stream.h>
         ^
1 error generated.
  In file included from /Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_plugin.cpp:8:
/Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_generator.h:8:10: fatal error: 'google/protobuf/io/zero_copy_stream.h' file not found
#include <google/protobuf/io/zero_copy_stream.h>
         ^
1 error generated.
  In file included from /Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_generator.cpp:1:
/Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_generator.h:8:10: fatal error: 'google/protobuf/io/zero_copy_stream.h' file not found
#include <google/protobuf/io/zero_copy_stream.h>
         ^
1 error generated.
  :grpc-compiler:compileJava_pluginExecutableJava_pluginCpp FAILED
  FAILURE: Build failed with an exception.
  * What went wrong:
Execution failed for task ':grpc-compiler:compileJava_pluginExecutableJava_pluginCpp'.
> Multiple build operations failed.
      C++ compiler failed while compiling java_generator.h.
      C++ compiler failed while compiling java_plugin.cpp.
      C++ compiler failed while compiling java_generator.cpp.
  See the complete log at: file:///Users/ghj1976/project/github/grpc/grpc-java/compiler/build/tmp/compileJava_pluginExecutableJava_pluginCpp/output.txt
  * 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: 1 mins 24.831 secs
  正确的时候的提示信息:
  $ ../gradlew java_pluginExecutable
*** Building codegen requires Protobuf version 3.0.0-beta-2
*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin
:grpc-compiler:compileJava_pluginExecutableJava_pluginCpp
:grpc-compiler:linkJava_pluginExecutable
:grpc-compiler:java_pluginExecutable
  BUILD SUCCESSFUL
  Total time: 14.818 secs
  This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.11/userguide/gradle_daemon.html
localhost:compiler ghj1976$
  $ ../gradlew java_pluginExecutable
Downloading https://services.gradle.org/distributions/gradle-2.13-bin.zip
.....................................................................................................................................................................................
Unzipping /Users/ghj1976/.gradle/wrapper/dists/gradle-2.13-bin/4xsgxlfjcxvrea7akf941nvc7/gradle-2.13-bin.zip to /Users/ghj1976/.gradle/wrapper/dists/gradle-2.13-bin/4xsgxlfjcxvrea7akf941nvc7
Set executable permissions for: /Users/ghj1976/.gradle/wrapper/dists/gradle-2.13-bin/4xsgxlfjcxvrea7akf941nvc7/gradle-2.13/bin/gradle
Download https://repo1.maven.org/maven2/com/google/protobuf/protobuf-gradle-plugin/0.7.7/protobuf-gradle-plugin-0.7.7.pom
Download https://repo1.maven.org/maven2/com/google/protobuf/protobuf-gradle-plugin/0.7.7/protobuf-gradle-plugin-0.7.7.jar
*** Building codegen requires Protobuf version 3.0.0-beta-2
*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin
:grpc-compiler:compileJava_pluginExecutableJava_pluginCpp
:grpc-compiler:linkJava_pluginExecutable
:grpc-compiler:java_pluginExecutable
  BUILD SUCCESSFUL
  Total time: 4 mins 29.65 secs
  This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.13/userguide/gradle_daemon.html
localhost:compiler ghj1976$

  

  我这里编译后的文件在下面目录的 protoc-gen-grpc-java 文件:
  /Users/ghj1976/project/github/grpc/grpc-java/compiler/build/exe/java_plugin
  对于需要产生Java版本的gRPC自动编译代码的,需要做下面几步:
  1、下载 protoc,对于原来就在用 protoc 的人来说,需要注意替换成 3.0 的版本。下载、使用方法参考: http://www.cnblogs.com/ghj1976/p/5435565.html
  2、同时去官网下载protoc-gen-grpc-java的源码自行在本地编译(参考本文上面部分)。
  3、然后使用 protoc 命令时指定 --plugin编译,不要试图使用 IDE 的protobuf 插件。
  http://evthoriz.com/2015/07/07/gRPC-tutorial/
  具体产生 java 版gRPC代码的命令如下:

$ protoc --plugin=protoc-gen-grpc-java=build/binaries/java_pluginExecutable/protoc-gen-grpc-java \
--grpc-java_out=&quot;$OUTPUT_FILE&quot; --proto_path=&quot;$DIR_OF_PROTO_FILE&quot; &quot;$PROTO_FILE&quot;
  具体到我这里的代码就是:
  $ protoc --plugin=protoc-gen-grpc-java=/Users/ghj1976/project/github/grpc/grpc-java/compiler/build/exe/java_plugin/protoc-gen-grpc-java       --grpc-java_out=./java/       ./proto/helloworld.proto
  产生 android 使用的 java-nano 代码方法如下:

$ protoc --plugin=protoc-gen-grpc-java=build/binaries/java_pluginExecutable/protoc-gen-grpc-java \
--grpc-java_out=nano:&quot;$OUTPUT_FILE&quot; --proto_path=&quot;$DIR_OF_PROTO_FILE&quot; &quot;$PROTO_FILE&quot;
  具体到我这里的代码就是:
  $ protoc --plugin=protoc-gen-grpc-java=/Users/ghj1976/project/github/grpc/grpc-java/compiler/build/exe/java_plugin/protoc-gen-grpc-java --grpc-java_out=nano:./javanano/ ./proto/helloworld.proto
  参考:
  protoc not generating service stub files

  http://stackoverflow.com/questions/31029675/protoc-not-generating-service-stub-files

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348129-1-1.html 上篇帖子: Hadoop 下篇帖子: [转]几种开源Java Web容器线程池的实现方法简介(二)
累计签到:1 天
连续签到:1 天
发表于 2017-3-3 13:46:05 | 显示全部楼层
$ ../gradlew java_pluginExecutable

  编译过程中如果出现下面错误: fatal error: 'google/protobuf/io/zero_copy_stream.h' file not found。
  是因为找不到这些头文件。找不到的原因如下:
  Some versions of Mac OS X (e.g., 10.10) doesn't have /usr/local in the default search paths for header files and libraries.
  It will fail the build of the codegen. To work around this, you will need to set environment variables:

$ export CXXFLAGS=&quot;-I/usr/local/include&quot; LDFLAGS=&quot;-L/usr/local/lib&quot;


windows10环境下怎么解决呢?大神!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表