proto生成descriptor文件动态解析

1. 新建Maven项目

在这里插入图片描述

2. 引入pom

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>protobuff-parser</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <protobuf-maven-plugin.version>0.5.1</protobuf-maven-plugin.version>
        <protoc.version>3.5.1-1</protoc.version>
        <grpc.version>1.13.1</grpc.version>
        <protobuf.version>3.6.0</protobuf.version>
    </properties>

    <dependencies>
        <!--protobuf相关-->
        <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>${protobuf.version}</version>
        </dependency>
        <!--protobuf相关end-->
    </dependencies>


    <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>${protobuf-maven-plugin.version}</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

编写proto

addressbook.proto

syntax = "proto3";

package tutorial;

message Person {
  string username = 1;
  string phone = 2;
  string email = 3;
  enum Gender {
    MALE = 0;
    FEMALE = 1;
    OTHER = 2;
  }
  Gender gender = 4;
}

message AddressBook {
  repeated Person people = 1;
}

通过生成descriptor 文件动态反序列化

package org.example;

import com.google.protobuf.DescriptorProtos;
import com.google.protobuf.Descriptors;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.Message;
import tutorial.Addressbook;

import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

/**
 * 通过生成descriptor 文件, 进行动态解析prootobuf流
 *
 * @author 公众号: Java编程与思想
 */
public class AddressBookDynamicDeserializer {
    public static void main(String[] args) throws Exception {

        // 生成 descriptor 文件的 protoc 命令
        String protocCMD = "protoc --descriptor_set_out=./src/main/proto/cinema1.description ./src/main/proto/addressbook.proto --proto_path=./";
        // 执行 protoc 命令生成 descriptor 文件
        Process process = Runtime.getRuntime().exec(protocCMD);
        // 等待命令执行完成
        process.waitFor();
        int exitValue = process.exitValue();
        if (exitValue != 0) {
            // 如果命令执行失败,打印错误信息并返回
            System.out.println("protoc execute failed");
            return;
        }

        Descriptors.Descriptor pbDescritpor = null;

        // 从文件 "./src/main/proto/cinema1.description" 中解析 FileDescriptorSet 对象
        DescriptorProtos.FileDescriptorSet descriptorSet = DescriptorProtos.FileDescriptorSet
                .parseFrom(new FileInputStream("./src/main/proto/cinema1.description"));

        // 遍历每一个 FileDescriptorProto 对象
        for (DescriptorProtos.FileDescriptorProto fdp : descriptorSet.getFileList()) {
            // 构建 FileDescriptor 对象
            Descriptors.FileDescriptor fileDescriptor = Descriptors.FileDescriptor.buildFrom(fdp, new Descriptors.FileDescriptor[] {});
            // 遍历每一个消息类型(Descriptor 对象)
            for (Descriptors.Descriptor descriptor : fileDescriptor.getMessageTypes()) {
                // 查找名称为 "AddressBook" 的消息类型
                if (descriptor.getName().equals("AddressBook")) {
                    pbDescritpor = descriptor;
                    break;
                }
            }
        }

        // protobuf序列化后的byte[]
        byte[] bytes = initMsg();

        // 创建一个新的 DynamicMessage.Builder,用于构建动态消息
        DynamicMessage.Builder pbBuilder = DynamicMessage.newBuilder(pbDescritpor);
        // 从序列化数据中合并消息
        Message pbMessage = pbBuilder.mergeFrom(bytes).build();
        // 打印消息内容
        printMessage(pbMessage);
    }

    private static void printMessage(Message message) {
        // 遍历消息中的所有字段
        for (Map.Entry<Descriptors.FieldDescriptor, Object> entry : message.getAllFields().entrySet()) {
            Descriptors.FieldDescriptor field = entry.getKey();
            Object value = entry.getValue();

            if (field.isRepeated()) {
                // 处理重复字段
                List<?> values = (List<?>) value;
                for (Object item : values) {
                    if (item instanceof Message) {
                        // 如果值是消息类型,递归调用 printMessage 方法
                        printMessage((Message) item);
                    }
                }
            } else {
                if (value instanceof Message) {
                    // 如果值是消息类型,递归调用 printMessage 方法
                    printMessage((Message) value);
                } else {
                    // 打印字段的 JSON 名称和字段值
                    System.out.println(field.getJsonName() + ": "  + value.toString());
                }
            }
        }
    }


    private static byte[] initMsg() {
        // 创建 AddressBook 的构建器
        Addressbook.AddressBook.Builder addressBook = Addressbook.AddressBook.newBuilder();

        // 添加一个人员信息(硬编码数据)
        Addressbook.Person person1 = Addressbook.Person.newBuilder()
                .setUsername("Alice")
                .setPhone("1234567890")
                .setEmail("alice@example.com")
                .setGender(Addressbook.Person.Gender.FEMALE)
                .build();

        // 将人员信息添加到地址簿
        addressBook.addPeople(person1);

        // 将地址簿构建成字节数组(序列化)
        byte[] serializedData = addressBook.build().toByteArray();

        return serializedData;
    }

}

项目结构

在这里插入图片描述
项目地址(dynamic分支):

https://gitee.com/javadage/protobuff-parser

https://github.com/JavaSqh/protobuff-parser.git

有任何问题请在下方留言。

要将.proto文件生成Java文件,可以使用gRPC提供的protobuf编译器插件。下面是在Spring Boot中使用protobuf编译器插件将.proto文件生成Java文件的步骤: 1. 在pom.xml文件中添加protobuf编译器插件: ```xml <build> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.17.2:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.38.1:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2. 在src/main/proto目录下创建.proto文件,如下所示: ```protobuf syntax = "proto3"; option java_multiple_files = true; option java_package = "com.example.grpc"; option java_outer_classname = "HelloWorldProto"; package helloworld; message HelloRequest { string name = 1; } message HelloReply { string message = 1; } service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); } ``` 3. 在项目根目录下运行以下命令: ``` mvn clean compile ``` 这将使用protobuf-maven-plugin插件自动从src/main/proto目录中的.proto文件生成Java类。生成Java类将保存在target/generated-sources/protobuf/java目录中。 以上是在Spring Boot中使用protobuf编译器插件将.proto文件生成Java文件的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值