protobuf + grpc 使用入门 一

来源:互联网 发布:吉林大学就业知乎 编辑:程序博客网 时间:2024/05/16 17:50
所需前提基础 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 greetingsmessage HelloReply {  string message = 1;}
配置好一切之后直接对project右键maven install 即可,附上成功截图![成功编译](http://img.blog.csdn.net/20161216022406331?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFiYW9nZV8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)![项目路径](http://img.blog.csdn.net/20161216022453190?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFiYW9nZV8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)夜深了我先睡了,之后工作中会实际使用grpc和protobuf。会持续分享!
0 0
原创粉丝点击