maven 使用插件方式編譯protobuf3 協議


<?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">
    <parent>
        <artifactId>im3</artifactId>
        <groupId>com.im</groupId>
        <version>3.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>im3-proto</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.11.4</version>
        </dependency>
    </dependencies>


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

protobuf3 協議文件例子

ProtoMsg.proto

syntax = "proto3";
option java_package = "com.im.common.proto";
option java_outer_classname = "MessageProto";

enum HeadType {
    LOGIN_REQUEST = 0; //登陸請求
    LOGIN_RESPONSE = 1; //登錄響應
    LOGOUT_REQUEST = 2; //退出請求
    LOGOUT_RESPONSE = 3;
    KEEPALIVE_REQUEST = 4; //心跳請求PING;
    KEEPALIVE_RESPONSE = 5;
    MESSAGE_REQUEST = 6; //消息請求;
    MESSAGE_RESPONSE = 7; //消息回執;
    MESSAGE_NOTIFICATION = 8; //通知消息
}

/*通知消息*/
message MessageNotification {
    uint32 msg_type = 1; //通知類型 1 上線 2 下線 ...
    bytes sender = 2;
    string json = 3;
    string timestamp = 4;
}

/*登錄信息*/
// LoginRequest對應的HeadType為Login_Request
// 消息名稱去掉下划線,更加符合Java 的類名規范
message LoginRequest {
    string uid = 1; // 用戶唯一id
    string deviceId = 2; // 設備ID
    string token = 3; // 用戶token
    uint32 platform = 4; //客戶端平台 windows、mac、android、ios、web
    string app_version = 5; // APP版本號
}

//token說明: 賬號服務器登錄時生成的Token
/*登錄響應*/
message LoginResponse {
    bool result = 1; //true 表示成功,false表示失敗
    uint32 code = 2; //錯誤碼
    string info = 3; //錯誤描述
    uint32 expose = 4; //錯誤描述是否提示給用戶:1 提示;0 不提示
    string session_id = 5; //sessionId
}

/*聊天消息*/
message MessageRequest {
    uint64 msg_id = 1; //消息id
    string from = 2; //發送方uId
    string to = 3; //接收方uId
    uint64 time = 4; //時間戳(單位:毫秒)
    uint32 msg_type = 5; //消息類型  1:純文本  2:音頻 3:視頻 4:地理位置 5:其他
    string session_id = 6; //sessionId
    string content = 7; //消息內容
    string url = 8; //多媒體地址
    string property = 9; //附加屬性
    string from_nick = 10; //發送者昵稱
    string json = 11; //附加的json串
}

message MessageResponse {
    bool result = 1; //true表示發送成功,false表示發送失敗
    uint32 code = 2; //錯誤碼
    string info = 3; //錯誤描述
    uint32 expose = 4; //錯誤描述是否提示給用戶:1 提示;0 不提示
    bool last_block = 5;
    fixed32 block_index = 6;
}

/*頂層消息*/
//頂層消息是一種嵌套消息,嵌套了各種類型消息
//內部的消息類型,全部使用optional字段
//根據消息類型 type的值,最多只有一個有效
message Message {
    HeadType type = 1; //消息類型
    uint64 sequence = 2; //消息系列號,主要用於Request和Response,Response的值必須和Request相同,使得發送端可以進行事務匹配處理
    fixed32 session_id = 3;
    LoginRequest loginRequest = 4;
    LoginResponse loginResponse = 5;
    MessageRequest messageRequest = 6;
    MessageResponse messageResponse = 7;
    MessageNotification notification = 8;
}

工程結構

 

 注意:

proto 文件放在 src/main/proto 文件夾下

 

執行mvn命令

 

 

在編譯路徑下會生成 Java文件

 

 把Java代碼和包路徑復制到 src/main/java 下

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM