通过C#学Proto.Actor模型》之Remote
Proto.Actor中提供了基于tcp/ip的通迅来实现Remote,可以通过其Remot实现对Actor的调用。
先来看一个极简单片的远程调用。
码友看码:
引用NuGet包
Proto.Actor
Proto.Remote
Proto.Serialization.Wire
共享库:
namespace P009_Lib
{
public class HelloRequest
{
public string Message
{
get; set;
}
}
public class HelloResponse
{
public string Message
{ get; set; }
}
}
服务端:
using P009_Lib;
using Proto;
using Proto.Remote;
using Proto.Serialization.Wire;
using System; using System.Threading;
using System.Threading.Tasks; namespace P009_Server
{
class Program
{
static void Main(string[] args)
{
Console.Title = "服务端";
Console.WriteLine("回车开始");
Console.ReadLine();
//设置序列化类型并注册
var wire = new WireSerializer(new[] { typeof(HelloRequest), typeof(HelloResponse) });
Serialization.RegisterSerializer(wire, true); var props = Actor.FromProducer(() => new HelloQuestActor());
//注册一个为hello类别的
Remote.RegisterKnownKind("hello", props);
//服务端监控端口5001
Remote.Start("127.0.0.1", );
Console.WriteLine("服务端开始……");
Console.ReadLine();
}
} class HelloQuestActor : IActor
{
public Task ReceiveAsync(IContext context)
{
switch (context.Message)
{
case HelloRequest msg:
Console.WriteLine(msg.Message);
context.Respond(new HelloResponse
{
Message = $"回应:我是服务端【{DateTime.Now}】",
});
break;
}
return Actor.Done;
}
}
}
客户端:
using P009_Lib;
using Proto;
using Proto.Remote;
using Proto.Serialization.Wire;
using System;
using System.Threading.Tasks; namespace P009_Client
{
class Program
{
static void Main(string[] args)
{ Console.Title = "客户端";
Console.WriteLine("回车开始");
Console.ReadLine();
//设置序列化类型并注册
var wire = new WireSerializer(new[] { typeof(HelloRequest), typeof(HelloResponse) });
Serialization.RegisterSerializer(wire, true);
//设置自己监控端口5002
Remote.Start("127.0.0.1", );
//连接服务端5001
var pid = Remote.SpawnNamedAsync("127.0.0.1:5001", "clientActor", "hello", TimeSpan.FromSeconds()).Result.Pid;
while (true)
{
var res = pid.RequestAsync<HelloResponse>(new HelloRequest { Message = $"请求:我是客户端 【{DateTime.Now}】" }).Result;
Console.WriteLine(res.Message);
Console.ReadLine();
}
}
}
}
代码很简单,看注释就够了。
……
通过C#学Proto.Actor模型》之Remote的更多相关文章
- 《通过C#学Proto.Actor模型》之 HelloWorld
在微服务中,数据最终一致性的一个解决方案是通过有状态的Actor模型来达到,那什么是Actor模型呢? Actor是并行的计算模型,包含状态,行为,并且包含一个邮箱,来异步处理消息. 关于Actor的 ...
- 《通过C#学Proto.Actor模型》之Mailbox
邮箱是Actor模型的一个重要组成部分,负责接收发过来的消息,并保存起来,等待Actor处理.邮箱中维护着两种队列,一种是存系统消息,另一个是存用户消息,系统省是指Started,Stoping,St ...
- 《通过C#学Proto.Actor模型》之Prpos
在第一篇Proto.Actor博文中,HelloWorld的第一行真正代码是: var props = Actor.FromProducer(() => new HelloActor()) ...
- 《通过C#学Proto.Actor模型》之Persistence
Actor是有状态的,当每一步执行失败后,返回失败地方继续执行时,希望此时的状态是正确的,为了保证这一点,持久化就成了必要的环节了. Proto.Actor提供了三种方式执久化: Event Sour ...
- 《通过C#学Proto.Actor模型》之Behaviors
Behaviors就是Actor接收到消息后可以改变处理的方法,相同的Actor,每次调用,转到不同的Actor内方法执行,非常适合按流程进行的场景.Behaviors就通过在Actor内部实例化一个 ...
- 《通过C#学Proto.Actor模型》之Supervision
Supervision,字面意思是监督,是父Actor发现子Actor有异常发生后,对子Actor产用保种策略处理的机制,如果父Actor不处理,则往上传递. 子Actor发生异常后处理的策略有: R ...
- 《通过C#学Proto.Actor模型》之PID
PID对象是代表Actor对象的进程,是能过Actor.Spawn(props)获取的:它有什么成员呢?既然代理Actor,首先有一个ID,标识自己是谁,Actor在Spawn时可以命名这个ID,否则 ...
- 《通过C#学Proto.Actor模型》之Spawning
Props是配置Actor和实例化Actor,那实例化后,就应该访问了,Props.Actor提供了Actor.Spawn(),Actor.SpawnPrefix(),Actor.SpawnNamed ...
- Proto.Actor模型
Proto.Actor模型 http://proto.actor/ https://github.com/axzxs2001/ProtoActorSample https://www.cnblogs. ...
随机推荐
- RabbitMQ消息队列(十四)-启用SSL安全通讯
如果RabbitMQ服务在内网中,只有内网的应用连接,我们认为这些连接都是安全的,但是个别情况我们需要让RabbitMQ对外提供服务.这种情况有两种解决方案: 在RabbitMQ外层在封装一层应用,应 ...
- RabbitMQ消息队列(十三)-VirtualHost与权限管理
像mysql有数据库的概念并且可以指定用户对库和表等操作的权限.那RabbitMQ呢?RabbitMQ也有类似的权限管理.在RabbitMQ中可以虚拟消息服务器VirtualHost,每个Virtua ...
- shell初识
今天写blog才发现以前还有没写起的,我的天,我是睡着了么... 1,什么是shell? shell是unix/Linux系统的一个用充当内核与用户之间的接口的软件,它读取用户的输入命令,发送给内核让 ...
- tcp没用吗?为什么MOBA、“吃鸡”游戏不推荐用tcp协议
本文由云+社区发表 作者:腾讯云游戏行业资深架构师 余国良 MOBA类和"吃鸡"游戏为什么对网络延迟要求高? 我们知道,不同类型的游戏因为玩法.竞技程度不一样,采用的同步算法不一样 ...
- Magicodes.WeiChat——V3.0(多租户)版本发布
主要内容如下: 添加项目Magicodes.WeiChat.Data.Multitenant,全面支持多租户(基于EF已经ASP.NET Identity) 增加租户管理.租户成员管理.修改密码.公众 ...
- 第13章 切换到混合流并添加API访问 - Identity Server 4 中文文档(v1.0.0)
在之前的快速入门中,我们探讨了API访问和用户身份验证.现在我们想把这两个部分放在一起. OpenID Connect和OAuth 2.0组合的优点在于,您可以使用单个协议和使用令牌服务进行单次交换来 ...
- 【转】mysql数据库优化大全
数据库优化 sql语句优化 索引优化 加缓存 读写分离 分区 分布式数据库(垂直切分) 水平切分 MyISAM和InnoDB的区别: 1. InnoDB支持事务,MyISAM不支持,对于InnoDB每 ...
- Linux基础(Ubuntu16.04):安装vim及配置
1.进入终端 Ctrl + Alt +T 出现终端窗口 2.输入命令: sudo apt-get install vim-gtk 3.验证是否成功 安装完vim后查看命令 vi tab键,就会关联出 ...
- Css实现手机端页面强制横屏的方法示例
样式 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 @media screen ...
- iOS----------弹窗动画
- (void)animationAlert:(UIView *)view { CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation ani ...