1.安裝java
brew install protobuf
2.查看安裝版本git
➜ protobuf git:(dev) ✗ protoc --version libprotoc 3.4.0 ➜ protobuf git:(dev) ✗
3.生成Prtobuf文件數組
➜ java git:(dev) ✗ protoc --java_out=./ com/test/frame/zookeeper/protobuf/SubscribeReq.proto ➜ java git:(dev) ✗ pwd /Users/guanguan/study/frame/zookeeper/src/main/java ➜ java git:(dev) ✗
4.若是遇到如下問題測試
➜ protobuf git:(dev) ✗ sudo protoc --java_out=./ SubscribeReq.proto [libprotobuf WARNING google/protobuf/compiler/parser.cc:546] No syntax specified for the proto file: SubscribeReq.proto. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
解決方案,將syntax ="proto2"或者syntax="proto3"放到.proto文件前面便可解決如(SubscribeReq.proto)ui
syntax = "proto2"; package com.test.frame.zookeeper.protobuf; option java_package="com.test.frame.zookeeper.protobuf"; option java_outer_classname="SubscribeReqProto"; message SubscribeReq{ required int32 subReqID=1; required string userName=2; required string productName=3; required string address=4; }
5.pom.xml文件中增長依賴google
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java --> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.4.0</version> </dependency>
6.測試編解碼效果TestSubcribeReq.java編碼
package com.test.frame.zookeeper.protobuf; import com.google.protobuf.InvalidProtocolBufferException; /** * TestSubscribeReqProto class * * @author guanhuifang * @date 2017/10/30 下午4:35 **/ public class TestSubscribeReqProto { /** * 編碼encode * * 將SubscribeReq類型的轉爲字節數組 * @param req * @return */ private static byte[] encode(SubscribeReqProto.SubscribeReq req){ /** * 編碼時經過調用SubscribeReqProto.SubscribeReq實例的toByteArray便可將SubscribeReq編碼爲byte數組 */ return req.toByteArray(); } /** * 解碼 * 將字節數組轉換爲SubscribeReq類型 * @param body * @return * @throws InvalidProtocolBufferException */ private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException { /** * 解碼時經過調用SubscribeReqProto.SubscribeReq實例的靜態方法parseForm將二進制byte數組解碼爲原始的對象 */ return SubscribeReqProto.SubscribeReq.parseFrom(body); } private static SubscribeReqProto.SubscribeReq createSubscribeReq(){ /** * 經過靜態方法newBuilder建立Builder實例,經過Builder構建器對SubscribeReq的屬性進行設置 */ SubscribeReqProto.SubscribeReq.Builder builder= SubscribeReqProto.SubscribeReq.newBuilder(); builder.setSubReqID(1); builder.setUserName("Gholly"); builder.setProductName("hh"); builder.setAddress("Shenzhen"); return builder.build(); } public static void main(String[] args) throws InvalidProtocolBufferException { SubscribeReqProto.SubscribeReq req=createSubscribeReq(); System.out.println("encode before:"+req.toString()); SubscribeReqProto.SubscribeReq req2= decode(encode(req)); System.out.println("after decode:"+req.toString()); System.out.println(req2.equals(req)); } }
運行結果爲:code
encode before:subReqID: 1
userName: "Gholly"
productName: "hh"
address: "Shenzhen"orm
after decode:subReqID: 1
userName: "Gholly"
productName: "hh"
address: "Shenzhen"xml
true
Process finished with exit code 0
說明編解碼先後數據是同樣的