protobuf + grpc 使用入门 一

所需前提基础 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。会持续分享!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值