protobuf + grpc 使用入门 一
2016-12-16 02:27
525 查看
所需前提基础 maven,以及maven在eclipse中的使用,proto基础语法,grpc概念。 本文主要介绍作者重点想要使用eclispse 自动编译固定文件夹下的proto文件的方法。 首先在eclipse 新建maven 项目 1.加入grpc依赖
<dependency> <groupId>io.grpc</groupId> <artifactId>grpc-all</artifactId> <version>1.0.1</version> </dependency>
2.加入编译conf
<build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.4.1.Final</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.0</version> <configuration> <!-- The version of protoc must match protobuf-java. If you don't depend on protobuf-java directly, you will be transitively depending on the protobuf-java version that grpc depends on. --> <protocArtifact>com.google.protobuf:protoc:3.0.0:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}</pluginArtifact> <protocExecutable>C:\Windows\System32\protoc</protocExecutable> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
注意事项 1.在grpc-all和protoc 版本 之间存在相互依赖,如若不匹配会导致 生成的java 文件报错。基本理念是高版本配高版本。懒虫直接参照我的即可。 2.<protocExecutable> 中指定的protoc.exe 是我自行下载 作用是protobuf 用来编译.proto文件用的。在这里指定的好处是你的eclipse 会在编译项目执行 是帮你自动生成java类。 3. 因为楼主安装eclipse 上的插件失败(据说可以指定扫描文件夹和输出文件夹) 所以就让它自己的输出位置,默认扫描文件夹是 src/main/proto. 后面还会贴图展示整个项目路径,
proto文件如下
syntax = "proto3"; option java_generic_services = true; option java_multiple_files = true; option java_package = "com.kuaipi.demo.common.grpc"; option java_outer_classname = "HelloWorldProto"; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; }
配置好一切之后直接对project右键maven install 即可,附上成功截图 ![成功编译](https://img-blog.csdn.net/20161216022406331?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFiYW9nZV8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) ![项目路径](https://img-blog.csdn.net/20161216022453190?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFiYW9nZV8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) 夜深了我先睡了,之后工作中会实际使用grpc和protobuf。会持续分享!
相关文章推荐
- protobuf + grpc 使用入门 二
- 基于protobuf service使用rpc入门教程
- Google Protobuf入门与使用
- 简单介绍google protobuf rpc框架使用方法
- Java使用protobuf入门四步骤
- 使用netty+zookeeper+protobuf实现一个RPC过程
- Go实战--golang中使用gRPC和Protobuf实现高性能api(golang/protobuf、google.golang.org/grpc)
- 使用google protobuf RPC实现echo service
- aio系列文档(3)----protobuf入门与使用
- 以小见大——那些基于 protobuf 的五花八门的 RPC(2)
- 以小见大——那些基于 protobuf 的五花八门的 RPC(2)
- 以小见大——那些基于 protobuf 的五花八门的 RPC(5 完)
- [转]Android上GTalk以及Push机制的XMPP数据选择使用protobuf格式而非XML格式
- google protobuf java 简单使用
- 以小见大——那些基于 protobuf 的五花八门的 RPC(5 完)
- protobuf使用初步
- 以小见大——那些基于 protobuf 的五花八门的 RPC(4)
- 以小见大——那些基于 protobuf 的五花八门的 RPC(4)
- java中使用protobuf序列化(反序列化)
- 以小见大——那些基于 protobuf 的五花八门的 RPC(3)