Protobuf(一)[环境搭建]

简介

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的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 gcc-c++

2.解决github clone 速度

git clone配置的获取的IP是会变动的,需要及时的获取更新。
hosts绑定前:
在这里插入图片描述
hosts绑定后:
在这里插入图片描述

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

在这里插入图片描述
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

在这里插入图片描述

2.命令方式【推荐】

1.获取信息
github.com

Linux上grep执行不生效,和bsd和GUN协议有关,bsd用grep -oE GUN用grep -oP

curl -skX GET 'https://github.com.ipaddress.com/' | grep -oP '<th>IPv4 Addresses</th><td><ul class="comma-separated"><li>.*?</li>'| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'

在这里插入图片描述
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 '<th>IPv4 Addresses</th><td><ul class="comma-separated"><li>.*?</li>'| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' 

在这里插入图片描述
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 

在这里插入图片描述

3.浏览器方式【推荐】

1.获取信息
浏览器打开

https://github.com.ipaddress.com/

在这里插入图片描述
浏览器打开:

https://fastly.net.ipaddress.com/github.global.ssl.fastly.net

在这里插入图片描述
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

在这里插入图片描述

4.浏览器搜索【推荐】

浏览器访问:

https://www.ipaddress.com/

在这里插入图片描述

3.安装

protobuf的安装参考:

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

1.克隆

git clone https://github.com/protocolbuffers/protobuf.git && ls | grep 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

在这里插入图片描述

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 gcc-c++

在这里插入图片描述

2.解决github clone 速度

git clone配置的获取的IP是会变动的,需要及时的获取更新。
hosts绑定前:
在这里插入图片描述
hosts绑定后:
在这里插入图片描述

1.命令方式【推荐】

1.获取信息
github.com

Linux上grep执行不生效,和bsd和GUN协议有关,bsd用grep -oE GUN用grep -oP

curl -skX GET 'https://github.com.ipaddress.com/' | grep -oP '<th>IPv4 Addresses</th><td><ul class="comma-separated"><li>.*?</li>'| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'

在这里插入图片描述
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 '<th>IPv4 Addresses</th><td><ul class="comma-separated"><li>.*?</li>'| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' 

在这里插入图片描述
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

在这里插入图片描述

2.浏览器方式【推荐】

1.获取信息
浏览器打开

https://github.com.ipaddress.com/

在这里插入图片描述
浏览器打开:

https://fastly.net.ipaddress.com/github.global.ssl.fastly.net

在这里插入图片描述
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

在这里插入图片描述

3.浏览器搜索【推荐】

浏览器访问:

https://www.ipaddress.com/

在这里插入图片描述

3.安装

protobuf的安装参考:

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

1.克隆

git clone https://github.com/protocolbuffers/protobuf.git && ls | grep 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

在这里插入图片描述

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的原配置!

发布了64 篇原创文章 · 获赞 14 · 访问量 4966

猜你喜欢

转载自blog.csdn.net/weixin_42366378/article/details/105698191