天天看點

Protobuf(一)[環境搭建]簡介1.安裝

Protobuf-環境搭建

  • 簡介
  • 1.安裝
    • 1.Ubuntu1804LTS
      • 1.安裝依賴
      • 2.解決github clone 速度
        • 1.nslookup方式【不建議】
        • 2.指令方式【推薦】
        • 3.浏覽器方式【推薦】
        • 4.浏覽器搜尋【推薦】
      • 3.安裝
        • 1.克隆
        • 2.執行腳本并安裝
      • 4.測試
      • 5.擷取幫助
    • 2.CentOS-7
      • 1.安裝依賴
      • 2.解決github clone 速度
        • 1.指令方式【推薦】
        • 2.浏覽器方式【推薦】
        • 3.浏覽器搜尋【推薦】
      • 3.安裝
        • 1.克隆
        • 2.執行腳本并安裝
      • 4.測試
      • 5.擷取幫助

簡介

Protocol Buffers - Google’s data interchange format。

Google Protocol Buffers (簡稱 Protobuf)是google旗下的一款輕便高效的結構化資料存儲格式,平台無關、語言無關、可擴充,可用于通訊協定和資料存儲等領域。是以很适合用做資料存儲和作為不同應用,不同語言之間互相通信的資料交換格式,隻要實作相同的協定格式即同一 proto檔案被編譯成不同的語言版本,加入到各自的工程中去。這樣不同語言就可以解析其他語言通過 protobuf序列化的資料。目前官網提供了 C++,Python,JAVA,GO等語言的支援。google在2008年7月7号将其作為開源項目對外公布。【Go于2009年11月正式宣布推出,故而Protobuf原生并不支援Go語言】

Protobuf(一)[環境搭建]簡介1.安裝

Protobuf的github網址:

https://github.com/protocolbuffers/protobuf
           

protobuf的安裝參考:

https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
           

Golang的Protobuf的github位址:

https://github.com/golang/protobuf
           

1.安裝

1.Ubuntu1804LTS

protobuf的安裝參考:

https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
           

1.安裝依賴

sudo apt install -y autoconf automake libtool curl wget git make g++ libffi-dev unzip dnsutils gcc 
           

2.解決github clone 速度

git clone配置的擷取的IP是會變動的,需要及時的擷取更新。

hosts綁定前:

Protobuf(一)[環境搭建]簡介1.安裝

hosts綁定後:

Protobuf(一)[環境搭建]簡介1.安裝

1.nslookup方式【不建議】

1.擷取資訊

dnsutils中的nslookup工具獲得github.com和github.global.ssl.fastly.net域名的ip位址
nslookup github.com
nslookup github.global.ssl.fastly.net
           
Name:   github.com
Address: 192.30.253.112
Name:   github.global.ssl.fastly.net
Address: 151.101.44.249
           
Protobuf(一)[環境搭建]簡介1.安裝

2.追加内容到/etc/hosts

sudo cp /etc/hosts /etc/hosts.backup 
sudo tee -a /etc/hosts <<-'EOF'
192.30.253.112 github.com
151.101.44.249 github.global.ssl.fastly.net
EOF
cat /etc/hosts
sudo /etc/init.d/networking restart
           
Protobuf(一)[環境搭建]簡介1.安裝

2.指令方式【推薦】

1.擷取資訊

github.com

Linux上grep執行不生效,和bsd和GUN協定有關,bsd用grep -oE GUN用grep -oP
Protobuf(一)[環境搭建]簡介1.安裝

github.global.ssl.fastly.net

Linux上grep執行不生效,和bsd和GUN協定有關,bsd用grep -oE GUN用grep -oP
Protobuf(一)[環境搭建]簡介1.安裝

2.追加内容到/etc/hosts

sudo cp /etc/hosts /etc/hosts.backup 
sudo tee -a /etc/hosts <<-'EOF'
140.82.112.3 github.com
199.232.69.194 github.global.ssl.fastly.net
EOF
cat /etc/hosts
sudo /etc/init.d/networking restart 
           
Protobuf(一)[環境搭建]簡介1.安裝

3.浏覽器方式【推薦】

1.擷取資訊

浏覽器打開:

https://github.com.ipaddress.com/
           
Protobuf(一)[環境搭建]簡介1.安裝

浏覽器打開:

https://fastly.net.ipaddress.com/github.global.ssl.fastly.net
           
Protobuf(一)[環境搭建]簡介1.安裝

2.追加内容到/etc/hosts

sudo cp /etc/hosts /etc/hosts.backup 
sudo tee -a /etc/hosts <<-'EOF'
140.82.112.3 github.com
199.232.69.194 github.global.ssl.fastly.net
EOF
cat /etc/hosts
sudo /etc/init.d/networking restart
           
Protobuf(一)[環境搭建]簡介1.安裝

4.浏覽器搜尋【推薦】

浏覽器通路:

https://www.ipaddress.com/
           
Protobuf(一)[環境搭建]簡介1.安裝

3.安裝

protobuf的安裝參考:

https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
           

1.克隆

git clone https://github.com/protocolbuffers/protobuf.git && ls | grep protobuf
           
Protobuf(一)[環境搭建]簡介1.安裝
Protobuf(一)[環境搭建]簡介1.安裝

2.執行腳本并安裝

安裝的耗時會很長……

cd ./protobuf 
git submodule update --init --recursive
./autogen.sh
./configure
make # 可使用加速: make -j 8 # 開多線程8個執行
make check
sudo make install
sudo ldconfig # [很重要]重新整理共享庫
           

4.測試

protoc --version
           
Protobuf(一)[環境搭建]簡介1.安裝

5.擷取幫助

protoc --help
           
Usage: protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
                              If not found in any of the these directories,
                              the --descriptor_set_in descriptors will be
                              checked for required proto file.
  --version                   Show version info and exit.
  -h, --help                  Show this text and exit.
  --encode=MESSAGE_TYPE       Read a text-format message of the given type
                              from standard input and write it in binary
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode=MESSAGE_TYPE       Read a binary message of the given type from
                              standard input and write it in text format
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode_raw                Read an arbitrary protocol message from
                              standard input and write the raw tag/value
                              pairs in text format to standard output.  No
                              PROTO_FILES should be given when using this
                              flag.
  --descriptor_set_in=FILES   Specifies a delimited list of FILES
                              each containing a FileDescriptorSet (a
                              protocol buffer defined in descriptor.proto).
                              The FileDescriptor for each of the PROTO_FILES
                              provided will be loaded from these
                              FileDescriptorSets. If a FileDescriptor
                              appears multiple times, the first occurrence
                              will be used.
  -oFILE,                     Writes a FileDescriptorSet (a protocol buffer,
    --descriptor_set_out=FILE defined in descriptor.proto) containing all of
                              the input files to FILE.
  --include_imports           When using --descriptor_set_out, also include
                              all dependencies of the input files in the
                              set, so that the set is self-contained.
  --include_source_info       When using --descriptor_set_out, do not strip
                              SourceCodeInfo from the FileDescriptorProto.
                              This results in vastly larger descriptors that
                              include information about the original
                              location of each decl in the source file as
                              well as surrounding comments.
  --dependency_out=FILE       Write a dependency output file in the format
                              expected by make. This writes the transitive
                              set of input file paths to FILE
  --error_format=FORMAT       Set the format in which to print errors.
                              FORMAT may be 'gcc' (the default) or 'msvs'
                              (Microsoft Visual Studio format).
  --print_free_field_numbers  Print the free field numbers of the messages
                              defined in the given proto files. Groups share
                              the same field number space with the parent
                              message. Extension ranges are counted as
                              occupied fields numbers.

  --plugin=EXECUTABLE         Specifies a plugin executable to use.
                              Normally, protoc searches the PATH for
                              plugins, but you may specify additional
                              executables not in the path using this flag.
                              Additionally, EXECUTABLE may be of the form
                              NAME=PATH, in which case the given plugin name
                              is mapped to the given executable even if
                              the executable's own name differs.
  --cpp_out=OUT_DIR           Generate C++ header and source.
  --csharp_out=OUT_DIR        Generate C# source file.
  --java_out=OUT_DIR          Generate Java source file.
  --js_out=OUT_DIR            Generate JavaScript source.
  --objc_out=OUT_DIR          Generate Objective C header and source.
  --php_out=OUT_DIR           Generate PHP source file.
  --python_out=OUT_DIR        Generate Python source file.
  --ruby_out=OUT_DIR          Generate Ruby source file.
  @<filename>                 Read options and filenames from file. If a
                              relative file path is specified, the file
                              will be searched in the working directory.
                              The --proto_path option will not affect how
                              this argument file is searched. Content of
                              the file will be expanded in the position of
                              @<filename> as in the argument list. Note
                              that shell expansion is not applied to the
                              content of the file (i.e., you cannot use
                              quotes, wildcards, escapes, commands, etc.).
                              Each line corresponds to a single argument,
                              even if it contains spaces.
           

2.CentOS-7

protobuf的安裝參考:

https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
           

1.安裝依賴

sudo yum install -y autoconf automake libtool curl wget git make g++ libffi-dev unzip dnsutils gcc 
           
Protobuf(一)[環境搭建]簡介1.安裝

2.解決github clone 速度

git clone配置的擷取的IP是會變動的,需要及時的擷取更新。

hosts綁定前:

Protobuf(一)[環境搭建]簡介1.安裝

hosts綁定後:

Protobuf(一)[環境搭建]簡介1.安裝

1.指令方式【推薦】

1.擷取資訊

github.com

Linux上grep執行不生效,和bsd和GUN協定有關,bsd用grep -oE GUN用grep -oP
Protobuf(一)[環境搭建]簡介1.安裝

github.global.ssl.fastly.net

Linux上grep執行不生效,和bsd和GUN協定有關,bsd用grep -oE GUN用grep -oP
Protobuf(一)[環境搭建]簡介1.安裝

2.追加内容到/etc/hosts

sudo cp /etc/hosts /etc/hosts.backup 
sudo tee -a /etc/hosts <<-'EOF'
140.82.112.3 github.com
199.232.69.194 github.global.ssl.fastly.net
EOF
cat /etc/hosts
sudo systemctl start network.service
           
Protobuf(一)[環境搭建]簡介1.安裝

2.浏覽器方式【推薦】

1.擷取資訊

浏覽器打開:

https://github.com.ipaddress.com/
           
Protobuf(一)[環境搭建]簡介1.安裝

浏覽器打開:

https://fastly.net.ipaddress.com/github.global.ssl.fastly.net
           
Protobuf(一)[環境搭建]簡介1.安裝

2.追加内容到/etc/hosts

sudo cp /etc/hosts /etc/hosts.backup 
sudo tee -a /etc/hosts <<-'EOF'
140.82.112.3 github.com
199.232.69.194 github.global.ssl.fastly.net
EOF
cat /etc/hosts
sudo systemctl start network.service
           
Protobuf(一)[環境搭建]簡介1.安裝

3.浏覽器搜尋【推薦】

浏覽器通路:

https://www.ipaddress.com/
           
Protobuf(一)[環境搭建]簡介1.安裝

3.安裝

protobuf的安裝參考:

https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
           

1.克隆

git clone https://github.com/protocolbuffers/protobuf.git && ls | grep protobuf
           
Protobuf(一)[環境搭建]簡介1.安裝
Protobuf(一)[環境搭建]簡介1.安裝

2.執行腳本并安裝

安裝的耗時會很長……

cd ./protobuf 
git submodule update --init --recursive
./autogen.sh
./configure
make # 可使用加速: make -j 8 # 開多線程8個執行
make check
sudo make install
sudo ldconfig # [很重要]重新整理共享庫
           

4.測試

protoc --version
           
Protobuf(一)[環境搭建]簡介1.安裝

5.擷取幫助

protoc --help
           
Usage: protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
                              If not found in any of the these directories,
                              the --descriptor_set_in descriptors will be
                              checked for required proto file.
  --version                   Show version info and exit.
  -h, --help                  Show this text and exit.
  --encode=MESSAGE_TYPE       Read a text-format message of the given type
                              from standard input and write it in binary
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode=MESSAGE_TYPE       Read a binary message of the given type from
                              standard input and write it in text format
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode_raw                Read an arbitrary protocol message from
                              standard input and write the raw tag/value
                              pairs in text format to standard output.  No
                              PROTO_FILES should be given when using this
                              flag.
  --descriptor_set_in=FILES   Specifies a delimited list of FILES
                              each containing a FileDescriptorSet (a
                              protocol buffer defined in descriptor.proto).
                              The FileDescriptor for each of the PROTO_FILES
                              provided will be loaded from these
                              FileDescriptorSets. If a FileDescriptor
                              appears multiple times, the first occurrence
                              will be used.
  -oFILE,                     Writes a FileDescriptorSet (a protocol buffer,
    --descriptor_set_out=FILE defined in descriptor.proto) containing all of
                              the input files to FILE.
  --include_imports           When using --descriptor_set_out, also include
                              all dependencies of the input files in the
                              set, so that the set is self-contained.
  --include_source_info       When using --descriptor_set_out, do not strip
                              SourceCodeInfo from the FileDescriptorProto.
                              This results in vastly larger descriptors that
                              include information about the original
                              location of each decl in the source file as
                              well as surrounding comments.
  --dependency_out=FILE       Write a dependency output file in the format
                              expected by make. This writes the transitive
                              set of input file paths to FILE
  --error_format=FORMAT       Set the format in which to print errors.
                              FORMAT may be 'gcc' (the default) or 'msvs'
                              (Microsoft Visual Studio format).
  --print_free_field_numbers  Print the free field numbers of the messages
                              defined in the given proto files. Groups share
                              the same field number space with the parent
                              message. Extension ranges are counted as
                              occupied fields numbers.

  --plugin=EXECUTABLE         Specifies a plugin executable to use.
                              Normally, protoc searches the PATH for
                              plugins, but you may specify additional
                              executables not in the path using this flag.
                              Additionally, EXECUTABLE may be of the form
                              NAME=PATH, in which case the given plugin name
                              is mapped to the given executable even if
                              the executable's own name differs.
  --cpp_out=OUT_DIR           Generate C++ header and source.
  --csharp_out=OUT_DIR        Generate C# source file.
  --java_out=OUT_DIR          Generate Java source file.
  --js_out=OUT_DIR            Generate JavaScript source.
  --objc_out=OUT_DIR          Generate Objective C header and source.
  --php_out=OUT_DIR           Generate PHP source file.
  --python_out=OUT_DIR        Generate Python source file.
  --ruby_out=OUT_DIR          Generate Ruby source file.
  @<filename>                 Read options and filenames from file. If a
                              relative file path is specified, the file
                              will be searched in the working directory.
                              The --proto_path option will not affect how
                              this argument file is searched. Content of
                              the file will be expanded in the position of
                              @<filename> as in the argument list. Note
                              that shell expansion is not applied to the
                              content of the file (i.e., you cannot use
                              quotes, wildcards, escapes, commands, etc.).
                              Each line corresponds to a single argument,
                              even if it contains spaces.
           

修改完成後一定要重新開機網絡服務!

操作完成後建議還原hosts的原配置!