企业级工作流解决方案(七)--微服务Tcp消息传输模型之消息编解码
Tcp消息传输主要参照surging来做的,做了部分裁剪和改动,详细参见:https://github.com/dotnetcore/surging
Json-rpc没有定义消息如何传输,因此,Json-Rpc RpcRequest对象和RpcResponse对象需要一个传输载体,这里的传输对象主是TransportMessage,如下代码,这里的Content请求时为RcpRequest对象,答复时为RpcResponse对象,答复时Header一般情况下为空。
/// <summary>
/// 传输消息模型。
/// </summary>
public class TransportMessage
{ public TransportMessage()
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TransportMessage(object content,object headers)
{
if (content == null)
throw new ArgumentNullException(nameof(content)); Content = content;
Headers = headers;
ContentType = content.GetType().FullName;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TransportMessage(object content, object headers, string fullName)
{
if (content == null)
throw new ArgumentNullException(nameof(content)); Headers = headers;
Content = content;
ContentType = fullName;
} /// <summary>
/// 消息Id。
/// </summary>
public string Id { get; set; } /// <summary>
/// 消息内容。
/// </summary>
public object Content { get; set; } /// <summary>
/// 消息传输Header
/// </summary>
public object Headers { get; set; } /// <summary>
/// 内容类型。
/// </summary>
public string ContentType { get; set; } /// <summary>
/// 是否调用消息。
/// </summary>
/// <returns>如果是则返回true,否则返回false。</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsInvokeMessage()
{
return ContentType == MessagePackTransportMessageType.jsonRequestTypeName;
} /// <summary>
/// 是否是调用结果消息。
/// </summary>
/// <returns>如果是则返回true,否则返回false。</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsInvokeResultMessage()
{
return ContentType == MessagePackTransportMessageType.jsonResponseTypeName;
} /// <summary>
/// 获取内容。
/// </summary>
/// <typeparam name="T">内容类型。</typeparam>
/// <returns>内容实例。</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T GetContent<T>()
{
return (T)Content;
} /// <summary>
/// 获取Header。
/// </summary>
/// <typeparam name="T">Header类型。</typeparam>
/// <returns>Header实例。</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T GetHeaders<T>()
{
return (T)Headers;
} /// <summary>
/// 创建一个调用传输消息。
/// </summary>
/// <param name="invokeMessage">调用实例。</param>
/// <returns>调用传输消息。</returns>
public static TransportMessage CreateInvokeMessage(JsonRequest invokeMessage,NameValueCollection nameValueCollection)
{
return new TransportMessage(invokeMessage, nameValueCollection, MessagePackTransportMessageType.jsonRequestTypeName)
{
Id = Guid.NewGuid().ToString("N")
};
} /// <summary>
/// 创建一个调用结果传输消息。
/// </summary>
/// <param name="id">消息Id。</param>
/// <param name="invokeResultMessage">调用结果实例。</param>
/// <returns>调用结果传输消息。</returns>
public static TransportMessage CreateInvokeResultMessage(string id, JsonResponse jsonResponse,NameValueCollection nameValueCollection)
{
return new TransportMessage(jsonResponse, nameValueCollection, MessagePackTransportMessageType.jsonResponseTypeName)
{
Id = id
};
}
}
TransportMessage需要在dotnetty中传输,则需要对TransportMessage进行编码解码
消息编解码器
public interface ITransportMessageEncoder
{
byte[] Encode(TransportMessage message);
}
public interface ITransportMessageDecoder
{
TransportMessage Decode(byte[] data);
}
Json编解码
平时编码中经常用的方式
public sealed class JsonTransportMessageEncoder : ITransportMessageEncoder
{
#region Implementation of ITransportMessageEncoder public byte[] Encode(TransportMessage message)
{
var content = JsonConvert.SerializeObject(message);
return Encoding.UTF8.GetBytes(content);
} #endregion Implementation of ITransportMessageEncoder
} public sealed class JsonTransportMessageDecoder : ITransportMessageDecoder
{
#region Implementation of ITransportMessageDecoder public TransportMessage Decode(byte[] data)
{
var content = Encoding.UTF8.GetString(data);
var message = JsonConvert.DeserializeObject<TransportMessage>(content);
if (message.IsInvokeMessage())
{
message.Content = JsonConvert.DeserializeObject<JsonRequest>(message.Content.ToString());
}
if (message.IsInvokeResultMessage())
{
message.Content = JsonConvert.DeserializeObject<JsonResponse>(message.Content.ToString());
}
return message;
} #endregion Implementation of ITransportMessageDecoder
}
MessagePack
官网地址:https://msgpack.org/
贴出代码,不过多的解释
[MessagePackObject]
public class MessagePackTransportMessage
{
public MessagePackTransportMessage(TransportMessage transportMessage)
{
Id = transportMessage.Id;
ContentType = transportMessage.ContentType; object contentObject;
if (transportMessage.IsInvokeMessage())
{
contentObject = new MessagePackJsonRequest(transportMessage.GetContent<JsonRequest>());
}
else if (transportMessage.IsInvokeResultMessage())
{
contentObject = new MessagePackJsonResponse(transportMessage.GetContent<JsonResponse>());
}
else
{
throw new NotSupportedException($"无法支持的消息类型:{ContentType}!");
} Content = SerializerUtilitys.Serialize(contentObject); var headersObject = transportMessage.GetHeaders<NameValueCollection>(); Headers = SerializerUtilitys.Serialize(JsonConvert.SerializeObject(MessagePackTransportMessageType.NvcToDictionary(headersObject)));
} public MessagePackTransportMessage()
{
} [Key(0)]
public string Id { get; set; } [Key(1)]
public byte[] Content { get; set; } [Key(2)]
public byte[] Headers { get; set; } [Key(3)]
public string ContentType { get; set; } [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsInvokeMessage()
{
return ContentType == MessagePackTransportMessageType.jsonRequestTypeName;
} [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsInvokeResultMessage()
{
return ContentType == MessagePackTransportMessageType.jsonResponseTypeName;
} public TransportMessage GetTransportMessage()
{
var message = new TransportMessage
{
ContentType = ContentType,
Id = Id,
Content = null,
Headers = null,
}; object contentObject;
if (IsInvokeMessage())
{
contentObject =
SerializerUtilitys.Deserialize<MessagePackJsonRequest>(Content).GetJsonRequest();
}
else if (IsInvokeResultMessage())
{
contentObject =
SerializerUtilitys.Deserialize<MessagePackJsonResponse>(Content)
.GetJsonResponse();
}
else
{
throw new NotSupportedException($"无法支持的消息类型:{ContentType}!");
}
message.Content = contentObject;
var headers = SerializerUtilitys.Deserialize<string>(Headers);
message.Headers = JsonConvert.DeserializeObject(headers);
return message;
}
} public sealed class MessagePackTransportMessageEncoder:ITransportMessageEncoder
{
#region Implementation of ITransportMessageEncoder [MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte[] Encode(TransportMessage message)
{
var transportMessage = new MessagePackTransportMessage(message)
{
Id = message.Id,
ContentType = message.ContentType,
};
return SerializerUtilitys.Serialize(transportMessage);
}
#endregion Implementation of ITransportMessageEncoder
} public sealed class MessagePackTransportMessageDecoder : ITransportMessageDecoder
{
#region Implementation of ITransportMessageDecoder [MethodImpl(MethodImplOptions.AggressiveInlining)]
public TransportMessage Decode(byte[] data)
{
var message = SerializerUtilitys.Deserialize<MessagePackTransportMessage>(data);
return message.GetTransportMessage();
} #endregion Implementation of ITransportMessageDecoder
}
ProtoBuffer
这个应该听得比较多
[ProtoContract]
public class ProtoBufferTransportMessage
{
public ProtoBufferTransportMessage(TransportMessage transportMessage)
{
Id = transportMessage.Id;
ContentType = transportMessage.ContentType; object contentObject;
if (transportMessage.IsInvokeMessage())
{
contentObject = new ProtoBufferJsonRequest(transportMessage.GetContent<JsonRequest>());
}
else if (transportMessage.IsInvokeResultMessage())
{
contentObject = new ProtoBufferJsonResponse(transportMessage.GetContent<JsonResponse>());
}
else
{
throw new NotSupportedException($"无法支持的消息类型:{ContentType}!");
} Content = SerializerUtilitys.Serialize(contentObject);
Headers = SerializerUtilitys.Serialize(transportMessage.GetHeaders<NameValueCollection>());
} public ProtoBufferTransportMessage()
{
} [ProtoMember(1)]
public string Id { get; set; } [ProtoMember(2)]
public byte[] Content { get; set; } [ProtoMember(3)]
public byte[] Headers { get; set; } [ProtoMember(4)]
public string ContentType { get; set; } public bool IsInvokeMessage()
{
return ContentType == MessagePackTransportMessageType.jsonRequestTypeName;
} public bool IsInvokeResultMessage()
{
return ContentType == MessagePackTransportMessageType.jsonResponseTypeName;
} public TransportMessage GetTransportMessage()
{
var message = new TransportMessage
{
ContentType = ContentType,
Id = Id,
Content = null,
Headers = null,
}; object contentObject;
if (IsInvokeMessage())
{
contentObject =
SerializerUtilitys.Deserialize<ProtoBufferJsonRequest>(Content).GetJsonRequest();
}
else if (IsInvokeResultMessage())
{
contentObject =
SerializerUtilitys.Deserialize<ProtoBufferJsonResponse>(Content)
.GetJsonResponse();
}
else
{
throw new NotSupportedException($"无法支持的消息类型:{ContentType}!");
} message.Content = contentObject;
message.Headers = SerializerUtilitys.Deserialize<NameValueCollection>(Headers); return message;
}
} public sealed class ProtoBufferTransportMessageEncoder : ITransportMessageEncoder
{
#region Implementation of ITransportMessageEncoder public byte[] Encode(TransportMessage message)
{
var transportMessage = new ProtoBufferTransportMessage(message)
{
Id = message.Id,
ContentType = message.ContentType,
}; return SerializerUtilitys.Serialize(transportMessage);
} #endregion Implementation of ITransportMessageEncoder
} public sealed class ProtoBufferTransportMessageDecoder : ITransportMessageDecoder
{
#region Implementation of ITransportMessageDecoder public TransportMessage Decode(byte[] data)
{
var message = SerializerUtilitys.Deserialize<ProtoBufferTransportMessage>(data);
return message.GetTransportMessage();
} #endregion Implementation of ITransportMessageDecoder
}
企业级工作流解决方案(七)--微服务Tcp消息传输模型之消息编解码的更多相关文章
- 企业级工作流解决方案(八)--微服务Tcp消息传输模型之服务端处理
服务端启动 服务端启动主要做几件事情,1. 从配置文件读取服务配置(主要是服务监听端口和编解码配置),2. 注册编解码器工厂,3. 启动dotnetty监听端口,4. 读取配置文件,解析全局消息处理模 ...
- 企业级工作流解决方案(六)--微服务消息处理模型之与Abp集成
身份认证传递 对于Abp比较熟悉的朋友应该对他里面的用户身份认证比较熟悉,他是通过实现微软提供的权限认证方式实现的,用户登录身份信息存储在System.Security.Claims.ClaimsPr ...
- .NET Core微服务之基于EasyNetQ使用RabbitMQ消息队列
Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.消息队列与RabbitMQ 1.1 消息队列 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更 ...
- [手把手教你] 用Swoft 搭建微服务(TCP RPC)
序言 Swoft Framework 基于 Swoole 原生协程的新时代 PHP 全栈式协程框架 Swoft 是什么? Swoft 框架是首个基于Swoole 原生协程的新时代 PHP高性能协程全栈 ...
- 微服务SpringCloud之配置中心和消息总线
在微服务SpringCloud之Spring Cloud Config配置中心SVN博客中每个client刷新配置信息时需要post请求/actuator/refresh,但客户端越来越多时,,需要每 ...
- 企业级工作流解决方案(十一)--集成Abp和ng-alain--权限系统服务
权限系统主要定义为管理员增删改查权限数据,直接读取数据库,权限系统服务主要定义为供其他系统调用的权限验证接口,定义为两个不同的微服务. 权限系统有一个特点,数据变动比较小,数据量本身并不是很大,访问量 ...
- 企业级工作流解决方案(九)--微服务Tcp消息传输模型之客户端处理
客户端启动 客户端启动主要做三件事情,1. 从配置文件读取服务调用配置,存储到全局对象中.2. 指定客户端编解码器工厂.3. 预连接,即预先建立与服务端的通信Chanel. [DependsOn(ty ...
- 企业级工作流解决方案(十五)--集成Abp和ng-alain--Abp其他改造
配置功能增强 Abp定义了各种配置接口,但是没有定义这些配置数据从哪里来,但是管理配置数据对于一个应用程序来说,是必不可少的一件事情. .net的配置数据管理,一般放在Web.config文件或者Ap ...
- 企业级工作流解决方案(十四)--集成Abp和ng-alain--自动化脚本
对于.net方向,做过自动化的,应该没有人不熟悉msbuild吧,非常强大的代码编译工具,.net平台的编译工作都是交给他来完成的,包括.net core的命令,本质上都是调用msbuild来执行的 ...
随机推荐
- java 保留小数点后指定位数四种方法
1 package com.itheima_01; 2 3 import java.math.BigDecimal; 4 import java.text.DecimalFormat; 5 impor ...
- 基于risc-v架构cpu
一.定义: CPU ,全称为中央处理器单元,简称为处理器,是一个不算年轻的概念 早在 20 世纪60 年代便己诞生了第一款 CPU请注意区分"处理器"和"处理器核& ...
- EFS加密
目录 EFS简介 EFS的特点 EFS的缺陷 EFS证书 证书的导出 证书的安装 EFS加密 方法一 方法二 EFS简介 EFS(Encrypting File System,加密文件系统)是Wind ...
- MongoDB添加认证
创建用户管理员 在admin数据库中,添加具有该userAdminAnyDatabase角色的用户 .根据需要为此用户添加其他角色. 注意:创建用户的数据库(在此示例中为 admin)是用户的身份验证 ...
- 初学 Python 需要安装哪些软件?
自动配置.有效求助.协作编程.版本控制.一站式解决 Python 新手练习中的痛点. 痛点 这个学期,我在北得克萨斯大学(University of North Texas)教 INFO 5731: ...
- pxe装机部署
批量全自动安装操作系统 dhcp:自动分配IP tftp:微系统安装系统 httpd:网络源 检查环境(永久性关闭selinux) setenforce 0 sed -i s/=enforcing/= ...
- 【DeepLearning】GoogLeNet
InceptionV1 论文原文:Going deeper with convolutions 中英文对照 InceptionBN 论文原文:Batch Normalization: Accel ...
- Linux小项目/rhel-基于同步官网yum仓库数据搭建本地yum服务器
本文的实验环境:aws上的Redhat 7.x , 同样也适用于Centos 7.x 简单说主要分为三步: (1) 向官网同步yum数据,可以根据具体情况,创建脚本及配置周期例行任务 (2) 搭建w ...
- Pytest框架中,conftest.py文件的作用?
conftest.py文件,它主要是实现fixture共享的. 第一,conftest.py文件当中,它储存的都是fixture,就是给用例提供做前置准备工作和后置清理工作的一个东西: 第二,conf ...
- CodeForces 1182E Product Oriented Recurrence
题意 给定五个整数 \(n,f_1,f_2,f_3,c\),其中数列 \(f\) 满足以下递推式: \[f_x=c^{2x-6}f_{x-1}f_{x-2}f_{x-3} \] 求 \(f_n\). ...