Ps:protocol buffers abbreviation PB
PB installation Configuration
- Download PB: On the PB official website, download the latest version (or other version) of Pb, which is used in the PB 2.5.0 version to match the PB Maven dependent version in the Java project.
- To install PB:
- Unzip:
tar zxvf protobuf-2.5.0.tar.gz Use the cd command to enter the protobuf-2.5.0 directory.
- ./configure, it is generally recommended
--prefix to add parameters to the specified directory, for example:
./configure --prefix=/usr/local/Cellar/protobuf/2.5.0
- Make, if the
make: Nothing to be done for all error indicates that PB has been compiled, if you need to recompile, you can first make clean delete the compiled files with the command.
- Make check
- Make install
- Set environment variables: Edit
/etc/profile , add at the end of the file:
Path=$PATH:/usr/local/cellar/protobuf/2.5. Ld_library_path=$LD _library_path:/usr/local/cellar/protobuf/2.5. 0/lib
At this point, PB has been successfully installed, you can use the protoc --version command to verify whether the installation is successful, if the following results, the PB has been installed successfully and effective.
Intellij Idea Install Protobuf plugin
Usually after we write a PB file, we need protoc to use to compile the PB file into corresponding code, in the command line we can use the following command to generate the corresponding Java file:
protoc --proto_path=src --java_out=build/gen src/myfolder
After you write a PB file in the IDE, you can generate the corresponding Java file in the next key of the specified package to facilitate the management of the Java file.
Google Protocol buffers support is an idea plugin with appeal features.
-
Install the PB plugin. Go to Idea Settings Center, open Plugins , click Browse repositories , search protocol buffer install plugin, as shown in. After installing the PB plugin, you need to restart idea.
Installing PB Plugins
-
Configure the path. After the PB plugin is installed, you need to configure the PB compilation path for the plugin, which is what we installed in the previous step protoc .
Now that the idea PB plugin is installed, here's how to use the PB plugin in your project to quickly generate Java files.
Java PB Demo
-
Create a new Maven project, configure the directory of PB-generated Java files: Enter Project Settings , fill in Output source directory , here the directory is the project Java source directory.
-
Generate a Java file based on a PB profile. The corresponding Java file can be generated by right-clicking in the PB file and selecting from the menu Compile ‘person.proto’ .
"Personmodel"; Message person { 1; 2; 3; }
- Join Maven dependencies. Include the following dependencies in the Pom.xml file:
<dependency> <groupid>com.google.protobuf</groupid> < Artifactid>protobuf-java</artifactid> <version>2.5.0</version > </dependency>
Serialization of
PersonModel.Person.Builder Personbuilder = PersonModel.Person.newBuilder ();
Personbuilder.setid (1);
Personbuilder.setname ("* * *");
Personbuilder.setemail ("[email protected]");
Personmodel.person Person1 = Personbuilder.build ();
Writes data to the output stream, such as a network output stream, where Bytearrayoutputstream is used instead
Bytearrayoutputstream output = new Bytearrayoutputstream ();
Person1. WriteTo (output);
deserialization
byte[] ByteArray = Output.tobytearray ();
Receive a stream and read it, such as a network input stream, where Bytearrayinputstream is used instead
Bytearrayinputstream input = new Bytearrayinputstream (ByteArray);
Personmodel.person person2= PersonModel.Person.parseFrom (input);
System.out.println ("ID:" + Person2.getid ());
System.out.println ("Name:" + person2.getname ());
System.out.println ("Email:" + person2.getemail ());
Java&protocol buffers