protobuf是一个语言无关、平台无关的序列化协议,由谷歌开源提供。再加上其高性能、存储占用更小等特点,在云原生的应用中越来越广泛。
在C#中主要有两种方法来使用protobuf协议,nuget包分别为Google.Protobuf和protobuf-net,其中Google.Protobuf由谷歌官方提供。本文简要记录和展示Google.Protobuf的使用方法和特点。

项目资料及文档

  • 项目官网:https://developers.google.cn/protocol-buffers?hl=zh-cn
  • github主页:https://github.com/protocolbuffers/protobuf/
  • 官方文档:https://developers.google.cn/protocol-buffers/docs/overview?hl=zh-cn
  • 该nuget包支持.NETFramework 4.5、.NETStandard1.1、.net5等

准备工作

  需要用到的nuget有如下两个:Google.Protobuf、Google.Protobuf.Tools,其中Google.Protobuf是主类库,运行时要用到。Google.Protobuf.Tools提供了命令行工具,用于根据.proto文件转为目标语言的类型,仅开发时使用,运行时不需要。
  本次Demo使用的.proto文件内容如下:
syntax = "proto3";
option cc_enable_arenas = true; package Tccc.Demo.Protobuf; message ErrorLog {
string LogID = 1;
string Context = 2;
string Stack = 3;
}
  首先需要根据.proto文件生成目标类型,操作如下:
./google.protobuf.tools\3.19.1\tools\windows_x64\protoc.exe --csharp_out=./generatedCode ./proto/ErrorLog.proto

  其中--csharp_out选项是生成C#语言的目标类型,运行protoc.exe -h 查看帮助信息,可以看到还支持一下几种选项:

--proto_path=PATH
--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.
--kotlin_out=OUT_DIR Generate Kotlin file.
--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.
  运行上述命令,会根据指定的ErrorLog.proto文件生成ErrorLog.cs文件,文件中就是C#类型ErrorLog。生成的代码中会给此类型增加方法void WriteTo(CodedOutputStream output)和只读属性Parser,接下来进行序列化和反序列化的关键。
 
生成的ErrorLog类的完整代码:

// <auto-generated>
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: ProtoFiles/ErrorLog.proto
// </auto-generated>
#pragma warning disable 1591, 0612, 3021
#region Designer generated code using pb = global::Google.Protobuf;
using pbc = global::Google.Protobuf.Collections;
using pbr = global::Google.Protobuf.Reflection;
using scg = global::System.Collections.Generic;
namespace Tccc.Demo.Protobuf { /// <summary>Holder for reflection information generated from ProtoFiles/ErrorLog.proto</summary>
public static partial class ErrorLogReflection { #region Descriptor
/// <summary>File descriptor for ProtoFiles/ErrorLog.proto</summary>
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
}
private static pbr::FileDescriptor descriptor; static ErrorLogReflection() {
byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat(
"ChlQcm90b0ZpbGVzL0Vycm9yTG9nLnByb3RvEhJUY2NjLkRlbW8uUHJvdG9i",
"dWYiOQoIRXJyb3JMb2cSDQoFTG9nSUQYASABKAkSDwoHQ29udGV4dBgCIAEo",
"CRINCgVTdGFjaxgDIAEoCUID+AEBYgZwcm90bzM="));
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
new pbr::FileDescriptor[] { },
new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] {
new pbr::GeneratedClrTypeInfo(typeof(global::Tccc.Demo.Protobuf.ErrorLog), global::Tccc.Demo.Protobuf.ErrorLog.Parser, new[]{ "LogID", "Context", "Stack" }, null, null, null, null)
}));
}
#endregion }
#region Messages
public sealed partial class ErrorLog : pb::IMessage<ErrorLog>
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
, pb::IBufferMessage
#endif
{
private static readonly pb::MessageParser<ErrorLog> _parser = new pb::MessageParser<ErrorLog>(() => new ErrorLog());
private pb::UnknownFieldSet _unknownFields;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public static pb::MessageParser<ErrorLog> Parser { get { return _parser; } } [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public static pbr::MessageDescriptor Descriptor {
get { return global::Tccc.Demo.Protobuf.ErrorLogReflection.Descriptor.MessageTypes[0]; }
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public ErrorLog() {
OnConstruction();
} partial void OnConstruction(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public ErrorLog(ErrorLog other) : this() {
logID_ = other.logID_;
context_ = other.context_;
stack_ = other.stack_;
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public ErrorLog Clone() {
return new ErrorLog(this);
} /// <summary>Field number for the "LogID" field.</summary>
public const int LogIDFieldNumber = 1;
private string logID_ = "";
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public string LogID {
get { return logID_; }
set {
logID_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
}
} /// <summary>Field number for the "Context" field.</summary>
public const int ContextFieldNumber = 2;
private string context_ = "";
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public string Context {
get { return context_; }
set {
context_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
}
} /// <summary>Field number for the "Stack" field.</summary>
public const int StackFieldNumber = 3;
private string stack_ = "";
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public string Stack {
get { return stack_; }
set {
stack_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
}
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public override bool Equals(object other) {
return Equals(other as ErrorLog);
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public bool Equals(ErrorLog other) {
if (ReferenceEquals(other, null)) {
return false;
}
if (ReferenceEquals(other, this)) {
return true;
}
if (LogID != other.LogID) return false;
if (Context != other.Context) return false;
if (Stack != other.Stack) return false;
return Equals(_unknownFields, other._unknownFields);
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public override int GetHashCode() {
int hash = 1;
if (LogID.Length != 0) hash ^= LogID.GetHashCode();
if (Context.Length != 0) hash ^= Context.GetHashCode();
if (Stack.Length != 0) hash ^= Stack.GetHashCode();
if (_unknownFields != null) {
hash ^= _unknownFields.GetHashCode();
}
return hash;
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public override string ToString() {
return pb::JsonFormatter.ToDiagnosticString(this);
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public void WriteTo(pb::CodedOutputStream output) {
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
output.WriteRawMessage(this);
#else
if (LogID.Length != 0) {
output.WriteRawTag(10);
output.WriteString(LogID);
}
if (Context.Length != 0) {
output.WriteRawTag(18);
output.WriteString(Context);
}
if (Stack.Length != 0) {
output.WriteRawTag(26);
output.WriteString(Stack);
}
if (_unknownFields != null) {
_unknownFields.WriteTo(output);
}
#endif
} #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) {
if (LogID.Length != 0) {
output.WriteRawTag(10);
output.WriteString(LogID);
}
if (Context.Length != 0) {
output.WriteRawTag(18);
output.WriteString(Context);
}
if (Stack.Length != 0) {
output.WriteRawTag(26);
output.WriteString(Stack);
}
if (_unknownFields != null) {
_unknownFields.WriteTo(ref output);
}
}
#endif [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public int CalculateSize() {
int size = 0;
if (LogID.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(LogID);
}
if (Context.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(Context);
}
if (Stack.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(Stack);
}
if (_unknownFields != null) {
size += _unknownFields.CalculateSize();
}
return size;
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public void MergeFrom(ErrorLog other) {
if (other == null) {
return;
}
if (other.LogID.Length != 0) {
LogID = other.LogID;
}
if (other.Context.Length != 0) {
Context = other.Context;
}
if (other.Stack.Length != 0) {
Stack = other.Stack;
}
_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
} [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public void MergeFrom(pb::CodedInputStream input) {
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
input.ReadRawMessage(this);
#else
uint tag;
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
case 10: {
LogID = input.ReadString();
break;
}
case 18: {
Context = input.ReadString();
break;
}
case 26: {
Stack = input.ReadString();
break;
}
}
}
#endif
} #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
uint tag;
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
break;
case 10: {
LogID = input.ReadString();
break;
}
case 18: {
Context = input.ReadString();
break;
}
case 26: {
Stack = input.ReadString();
break;
}
}
}
}
#endif } #endregion } #endregion Designer generated code

序列化操作

        public static byte[] Serialize(ErrorLog log)
{
using (MemoryStream output = new MemoryStream())
{
log.WriteTo(output);
return output.ToArray();
}
}

反序列化操作

            ErrorLog desErrorLog= ErrorLog.Parser.ParseFrom(data);

使用特点和理解

  • protoc.exe是支持生成多语言类型,这对于跨语言的混合编程比较方便。
  • 根据上述使用步骤可以看到,必须先使用工具protoc生成目标类型,才能调用序列化和反序列化方法,这有些不符合.net平台的编码习惯。
  • 一堆自动生成的C#类在可维护性方面欠佳,当需要调整属性字段时,还要通过工具重新生成,较为麻烦。

如何在C#中使用Google.Protobuf工具的更多相关文章

  1. 在UnrealEngine4中使用Google Protobuf

    转自:https://blog.csdn.net/or_7r_ccl/article/details/54986393 在UnrealEngine4中使用Google Protobuf         ...

  2. 如何在Blog中加入Google Analytics

    原文链接:https://www.cnblogs.com/procoder/archive/2010/03/04/Google-Analytics-Blog.html 背景 在之前加入了一个网站统计工 ...

  3. 如何在sharepoint2010中配置Google Anlytics 分析服务

      简介 Google Analytics(分析)不仅可以帮助您衡量销售与转化情况,而且能为您提供新鲜的深入信息,帮助您了解访问者如何使用您的网站,他们如何到达您的网站,以及您可以如何吸引他们不断回访 ...

  4. 如何在eclipse中配置反编译工具JadClipse

    Q:为什么有必要在开发环境中配置反编译工具呢? A:  当运行引用了第三方jar包项目时,突然报出了jar包中的某个类的某一行出现异常.我们想看一下这个class文件的代码时,经常出现了如下图所示的场 ...

  5. 如何在Linux中使用rz/sz工具进行文件传输

    在Linux中,使用rz/sz工具能够进行Linux和windows之间的文件传输,那么要如何使用rz/sz工具工具呢?下面小编就给大家介绍下Linux下如何使用rz/sz工具进行文件传输,一起来学习 ...

  6. 《Dotnet9》系列-Google ProtoBuf在C#中的简单应用

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  7. Netty学习——Google Protobuf使用方式分析和环境搭建

    Google Protobuf使用方式分析 在RPC框架中,Google Protobuf是很常用的一个库,和Apache Thrift 是同款的用于进行序列化的第三方库.原理都是大同小异,无非就是使 ...

  8. Java中使用google.zxing快捷生成二维码(附工具类源码)

    移动互联网时代,基于手机端的各种活动扫码和收付款码层出不穷:那我们如何在Java中生成自己想要的二维码呢?下面就来讲讲在Java开发中使用 google.zxing 生成二维码. 一般情况下,Java ...

  9. (原)python中import caffe提示no module named google.protobuf.internal

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5993405.html 之前在一台台式机上在python中使用import caffe时,没有出错.但是 ...

随机推荐

  1. 从零入门 Serverless | Serverless 应用如何管理日志 & 持久化数据

    作者 | 竞霄 阿里巴巴开发工程师 本文整理自<Serverless 技术公开课>,关注"Serverless"公众号,回复"入门",即可获取 Se ...

  2. 2021MySQL 8.0.26安装教程,目前最新版(详细全面)

    MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能!所以为什么不用MySQL8呢!下面是MySQL 8.0.26的安装教程. 安装网址: https://dev.my ...

  3. Java初步学习——2021.10.05每日总结,第五周周二

    (1)今天做了什么: (2)明天准备做什么? (3)遇到的问题,如何解决? 今天学习了二维数组,包括二维数组的声明,和二维数组的创建.以及获取二维数组的长度,其中要注意的是二维数组是每个元素都是一维数 ...

  4. Java(34)IO流之字符流

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228453.html 博客主页:https://www.cnblogs.com/testero ...

  5. 【Java技术专题】「性能优化系列」针对Java对象压缩及序列化技术的探索之路

    序列化和反序列化 序列化就是指把对象转换为字节码: 对象传递和保存时,保证对象的完整性和可传递性.把对象转换为有字节码,以便在网络上传输或保存在本地文件中: 反序列化就是指把字节码恢复为对象: 根据字 ...

  6. 【数据结构 C++】排序——冒泡、插入、选择、希尔、归并、快排、堆排序

    LeetCode 912. 排序数组 给你一个整数数组 nums,请你将该数组升序排列. 示例 1: 输入:nums = [5,2,3,1] 输出:[1,2,3,5] 示例 2: 输入:nums = ...

  7. [Beta]the Agiles Scrum Meeting 10

    会议时间:2020.5.25 21:00 1.每个人的工作 今天已完成的工作 成员 已完成的工作 issue yjy 暂无 tq 暂无 wjx 实现创建.显示博客作业功能 增加博客作业功能 dzx 实 ...

  8. Mac 系统如何利用软链接在根目录创建文件夹?

    作者:泥瓦匠 出处:https://www.bysocket.com/2021-10-26/mac-create-files-from-the-root-directory.html Mac 操作系统 ...

  9. SkyWalking配上告警更优秀

    前言 对于监控系统来说,不可能让人一直盯着监控看板,而更多的是以自动提醒的方式,比如邮件.短信或微信推送等,当达到或超出预设的告警指标时,就自动发送消息提醒,下面就来说说如何配置SkyWalking的 ...

  10. linux下创建文件的文件权限问题

    今天发现创建文件的权限和自己规定的权限不一致,了解到了权限掩码的问题,这里总结一下. 首先权限掩码umask是chmod配套的,总共为4位(gid/uid,属主,组权,其它用户的权限),不过通常我们都 ...