Protobuf(一)[环境搭建]


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(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

hosts绑定后:
Protobuf(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

2.命令方式【推荐】 1.获取信息
github.com
Linux上grep执行不生效,和bsd和GUN协议有关,bsd用grep -oE GUN用grep -oP
curl -skX GET 'https://github.com.ipaddress.com/' | grep -oP 'IPv4 Addresses
  • .*?
  • '| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'

Protobuf(一)[环境搭建]
文章图片

github.global.ssl.fastly.net
Linux上grep执行不生效,和bsd和GUN协议有关,bsd用grep -oE GUN用grep -oP
curl -skX GET 'https://fastly.net.ipaddress.com/github.global.ssl.fastly.net' | grep -oP 'IPv4 Addresses
  • .*?
  • '| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'

Protobuf(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

3.浏览器方式【推荐】 1.获取信息
浏览器打开:
https://github.com.ipaddress.com/

Protobuf(一)[环境搭建]
文章图片

浏览器打开:
https://fastly.net.ipaddress.com/github.global.ssl.fastly.net

Protobuf(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

4.浏览器搜索【推荐】 浏览器访问:
https://www.ipaddress.com/

Protobuf(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

Protobuf(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

5.获取帮助
protoc --help

Usage: protoc [OPTION] PROTO_FILES Parse PROTO_FILES and generate output based on the options given: -IPATH, --proto_path=PATHSpecify 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. --versionShow version info and exit. -h, --helpShow this text and exit. --encode=MESSAGE_TYPERead 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_TYPERead 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_rawRead 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=FILESSpecifies 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_importsWhen using --descriptor_set_out, also include all dependencies of the input files in the set, so that the set is self-contained. --include_source_infoWhen 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=FILEWrite a dependency output file in the format expected by make. This writes the transitive set of input file paths to FILE --error_format=FORMATSet the format in which to print errors. FORMAT may be 'gcc' (the default) or 'msvs' (Microsoft Visual Studio format). --print_free_field_numbersPrint 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=EXECUTABLESpecifies 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_DIRGenerate C++ header and source. --csharp_out=OUT_DIRGenerate C# source file. --java_out=OUT_DIRGenerate Java source file. --js_out=OUT_DIRGenerate JavaScript source. --objc_out=OUT_DIRGenerate Objective C header and source. --php_out=OUT_DIRGenerate PHP source file. --python_out=OUT_DIRGenerate Python source file. --ruby_out=OUT_DIRGenerate Ruby source file. @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 @ 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(一)[环境搭建]
文章图片

2.解决github clone 速度
git clone配置的获取的IP是会变动的,需要及时的获取更新。
hosts绑定前:
Protobuf(一)[环境搭建]
文章图片

hosts绑定后:
Protobuf(一)[环境搭建]
文章图片

1.命令方式【推荐】 1.获取信息
github.com
Linux上grep执行不生效,和bsd和GUN协议有关,bsd用grep -oE GUN用grep -oP
curl -skX GET 'https://github.com.ipaddress.com/' | grep -oP 'IPv4 Addresses
  • .*?
  • '| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'

Protobuf(一)[环境搭建]
文章图片

github.global.ssl.fastly.net
Linux上grep执行不生效,和bsd和GUN协议有关,bsd用grep -oE GUN用grep -oP
curl -skX GET 'https://fastly.net.ipaddress.com/github.global.ssl.fastly.net' | grep -oP 'IPv4 Addresses
  • .*?
  • '| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'

Protobuf(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

2.浏览器方式【推荐】 1.获取信息
浏览器打开:
https://github.com.ipaddress.com/

【Protobuf(一)[环境搭建]】Protobuf(一)[环境搭建]
文章图片

浏览器打开:
https://fastly.net.ipaddress.com/github.global.ssl.fastly.net

Protobuf(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

3.浏览器搜索【推荐】 浏览器访问:
https://www.ipaddress.com/

Protobuf(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

Protobuf(一)[环境搭建]
文章图片

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(一)[环境搭建]
文章图片

5.获取帮助
protoc --help

Usage: protoc [OPTION] PROTO_FILES Parse PROTO_FILES and generate output based on the options given: -IPATH, --proto_path=PATHSpecify 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. --versionShow version info and exit. -h, --helpShow this text and exit. --encode=MESSAGE_TYPERead 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_TYPERead 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_rawRead 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=FILESSpecifies 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_importsWhen using --descriptor_set_out, also include all dependencies of the input files in the set, so that the set is self-contained. --include_source_infoWhen 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=FILEWrite a dependency output file in the format expected by make. This writes the transitive set of input file paths to FILE --error_format=FORMATSet the format in which to print errors. FORMAT may be 'gcc' (the default) or 'msvs' (Microsoft Visual Studio format). --print_free_field_numbersPrint 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=EXECUTABLESpecifies 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_DIRGenerate C++ header and source. --csharp_out=OUT_DIRGenerate C# source file. --java_out=OUT_DIRGenerate Java source file. --js_out=OUT_DIRGenerate JavaScript source. --objc_out=OUT_DIRGenerate Objective C header and source. --php_out=OUT_DIRGenerate PHP source file. --python_out=OUT_DIRGenerate Python source file. --ruby_out=OUT_DIRGenerate Ruby source file. @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 @ 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的原配置!

    推荐阅读