protobuf在idea上的demo实例

先导入依赖


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.menggroupId>
    <artifactId>protobufartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <grpc.version>1.6.1grpc.version>
        <protobuf.version>3.3.0protobuf.version>
    properties>


    <dependencies>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.59version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.16.18version>
        dependency>

        <dependency>
            <groupId>io.grpcgroupId>
            <artifactId>grpc-nettyartifactId>
            <version>${grpc.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>io.grpcgroupId>
            <artifactId>grpc-protobufartifactId>
            <version>${grpc.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>io.grpcgroupId>
            <artifactId>grpc-stubartifactId>
            <version>${grpc.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>com.google.protobufgroupId>
            <artifactId>protobuf-javaartifactId>
            <version>${protobuf.version}version>
        dependency>
    dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.mavengroupId>
                <artifactId>os-maven-pluginartifactId>
                <version>1.5.0.Finalversion>
            extension>
        extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.pluginsgroupId>
                <artifactId>protobuf-maven-pluginartifactId>
                <version>0.5.0version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}protocArtifact>
                    <pluginId>grpc-javapluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}pluginArtifact>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compilegoal>
                            <goal>compile-customgoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>6source>
                    <target>6target>
                configuration>
            plugin>
        plugins>
    build>


project>

然后写teacher.proto,这里文件目录要在main下面,而且文件夹名字是proto不能乱改

syntax = "proto3";
option java_package="protobuf";
option java_outer_classname="TeacherSerializer";

message Teacher{

  int64 id=1;

  int32 age=2;

  string name=3;

  repeated string courses=4;
}

利用插件生成序列化代码,如图
protobuf在idea上的demo实例_第1张图片双击,生成
protobuf在idea上的demo实例_第2张图片
将生成的文件拷贝到domain下即可。
写一个测试类

package domain;

import com.google.protobuf.InvalidProtocolBufferException;

import java.util.Arrays;

/**
 * @author Administrator
 */
public class ProtobufTest {

    public static void main(String[] args) throws InvalidProtocolBufferException {
        byte[] bytes = serialize();
        System.out.println(Arrays.toString(bytes));

        TeacherSerializer.Teacher teacher = deserialize(bytes);
        System.out.println(teacher.getId()+"\t"+teacher.getName()+"\t"+teacher.getAge()+"\t"+teacher.getCoursesList());

    }

    /**序列化
     *
     * @return
     */
    public static byte[] serialize(){
        TeacherSerializer.Teacher.Builder builder = TeacherSerializer.Teacher.newBuilder();
        builder.setId(1001L)
                .setAge(20)
                .setName("liulaoshi")
                .addCourses("java");
        TeacherSerializer.Teacher teacher = builder.build();
        byte[] bytes = teacher.toByteArray();
        return bytes;
    }

    /**反序列化
     *
     * @param bytes
     * @return
     * @throws InvalidProtocolBufferException
     */
    public static TeacherSerializer.Teacher deserialize(byte[] bytes) throws InvalidProtocolBufferException {
        TeacherSerializer.Teacher teacher = TeacherSerializer.Teacher.parseFrom(bytes);
        return teacher;

    }


}

运行结果如下
protobuf在idea上的demo实例_第3张图片

你可能感兴趣的:(protobuf)