protobuf:IDEA+maven 基础使用教程

1,idea 下载插件Protobuf support

setting->Plugins 搜索;安装完成之后重启IDEA

2,建立好maven项目之后引入Protobuf 和 grpc

<properties>
  <grpc.version>1.17.1</grpc.version>
  <protobuf.version>3.5.0</protobuf.version>
</properties>
<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-stub</artifactId>
  <version>${grpc.version}</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-java</artifactId>
  <version>${protobuf.version}</version>
</dependency>
<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-java-util</artifactId>
  <version>3.5.1</version>
</dependency>

3,引入插件

<build>
<extensions>
  <extension>
    <groupId>kr.motd.maven</groupId>
    <artifactId>os-maven-plugin</artifactId>
    <version>1.5.0.Final</version>
  </extension>
</extensions>
<plugins>
  <plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
    <version>0.5.0</version>
    <configuration>
      <protocArtifact>
        com.google.protobuf:protoc:3.5.0:exe:${os.detected.classifier}
      </protocArtifact>
      <pluginId>grpc-java</pluginId>
      <pluginArtifact>
        io.grpc:protoc-gen-grpc-java:1.17.1:exe:${os.detected.classifier}
      </pluginArtifact>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>compile</goal>
          <goal>compile-custom</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

此处的Plugins 和 PluginManagement 处于同一级,不然在右边的Plugins里面看不见ProtoBuf 插件

4,创建proto文件

proto文件夹,名字不能乱写,*.proto放在文件夹里面

Student.proto 内部数据:

syntax = "proto3";
//生成文件所在包名
option java_package = "com.demo";
//生成的java文件名
option java_outer_classname = "ProtoDemo";
message Student {
    int32 id = 1;
    string name = 2;
    string email = 3;
    //枚举类
    enum Sex {
        MAN = 0;
        WOMAN = 1;
    }
    Sex sex = 4 ;

    enum PhoneType{
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
    }
    //内部类
    message PhoneNumber {
        string number = 1;
        PhoneType type = 2 ;
    }
    //集合
    repeated PhoneNumber phone = 5;
}

5,点击Protobuf插件的compile就可以在target中生成java文件了,拷贝到com目录下,就可以使用了;

6,测试一下

@Test
public void protobufTest() {
    ProtoDemo.Student.Builder builder = ProtoDemo.Student.newBuilder();
    builder.setEmail("280208673@qq.com");
    builder.setName("zhou");
    builder.setId(100);

    ProtoDemo.Student student = builder.build();
    System.out.println(student);
    System.out.println("===============  to byte");

    for (byte b : student.toByteArray()) {
        System.out.println(b);
    }
    byte[] bytes = student.toByteArray();
    try {
        ProtoDemo.Student student1 = ProtoDemo.Student.parseFrom(bytes);
        System.out.println("email:" + student1.getEmail());
        System.out.println("name:" + student1.getName());
        System.out.println("id:" + student1.getId());
    } catch (InvalidProtocolBufferException e) {
    }
}

可以正常输出;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值