protobuf + grpc 使用入门 一

所需前提基础 maven,以及maven在eclipse中的使用,proto基础语法,grpc概念。
本文主要介绍作者重点想要使用eclispse 自动编译固定文件夹下的proto文件的方法。

首先在eclipse 新建maven 项目
1.加入grpc依赖
    <dependency>
            <groupId>io.grpcgroupId>
            <artifactId>grpc-allartifactId>
            <version>1.0.1version>
    dependency>
2.加入编译conf
<build>
        <extensions>
            <extension>
                <groupId>kr.motd.mavengroupId>
                <artifactId>os-maven-pluginartifactId>
                <version>1.4.1.Finalversion>
            extension>
        extensions>
        <plugins> 
            <plugin>
                <groupId>org.xolstice.maven.pluginsgroupId>
                <artifactId>protobuf-maven-pluginartifactId>
                <version>0.5.0version>
                <configuration>
                    
                    <protocArtifact>com.google.protobuf:protoc:3.0.0:exe:${os.detected.classifier}protocArtifact>
                    <pluginId>grpc-javapluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}pluginArtifact>
                   <protocExecutable>C:\Windows\System32\protocprotocExecutable>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compilegoal>
                            <goal>compile-customgoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
注意事项
1.在grpc-all和protoc 版本 之间存在相互依赖,如若不匹配会导致 生成的java 文件报错。基本理念是高版本配高版本。懒虫直接参照我的即可。
2. 中指定的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。会持续分享!

你可能感兴趣的:(proto+grpc)