通过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. ...
随机推荐
- 补习系列(3)-springboot中的几种scope
目标 了解HTTP 请求/响应头及常见的属性: 了解如何使用SpringBoot处理头信息 : 了解如何使用SpringBoot处理Cookie : 学会如何对 Session 进行读写: 了解如何在 ...
- ppt制作元素采集
原文链接 https://www.zhihu.com/question/52157612/answer/247501754?utm_source=qq&utm_medium=social 1动 ...
- 五一之起一台服务器玩玩-centosl系统搭建lamp
昨天看到有的扫描软件会扫描公网主机开放的端口,我现在就用了ssh远程登录21和22端口那不是很容易被猜想到.so,我决定要改这个端口,同时这个改端口给我打开了防火墙和ip协议和网络安全的大门. 我之前 ...
- kubernetes学习14—Dashboard搭建和认证
本文收录在容器技术学习系列文章总目录 一.介绍 Kubernetes Dashboard是Kubernetes集群的基于Web的通用UI.它允许用户管理在群集中运行的应用程序并对其进行故障排除,以及管 ...
- springboot情操陶冶-web配置(七)
参数校验通常是OpenApi必做的操作,其会对不合法的输入做统一的校验以防止恶意的请求.本文则对参数校验这方面作下简单的分析 spring.factories 读者应该对此文件加以深刻的印象,很多sp ...
- springboot情操陶冶-web配置(五)
本文讲讲mvc的异常处理机制,方便查阅以及编写合理的异常响应方式 入口例子 很简单,根据之前的文章,我们只需要复写WebMvcConfigurer接口的异常添加方法即可,如下 1.创建简单的异常处理类 ...
- celery4+django2定时任务
网上有很多celery + django实现定时任务的教程,不过它们大多数是基于djcelery + celery3的: 或者是使用django_celery_beat配置较为繁琐的. 显然简洁而高效 ...
- C#工具:利用HttpClient调用WebApi
可以利用HttpClient来进行Web Api的调用.由于WebA Api的调用本质上就是一次普通的发送请求与接收响应的过程, 所有HttpClient其实可以作为一般意义上发送HTTP请求的工具. ...
- redo/declare/typeset
变量设置功能,都是由命令行直接设置的,那么,可不可以让使用者能够经由键盘输入? 什么意思呢?是否记得某些程序执行的过程当中,会等待使用者输入 "yes/no"之类的讯息啊? 在 b ...
- JavaScript 为什么要有 Symbol 类型?
Symbols 是 ES6 引入了一个新的数据类型 ,它为 JS 带来了一些好处,尤其是对象属性时. 但是,它们能为我们做些字符串不能做的事情呢? 在深入探讨 Symbol 之前,让我们先看看一些 J ...