Proto.Actor Bootcamp 学习笔记

玖亖伍
2021-07-29 / 0 评论 / 406 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年02月25日,已超过790天没有更新,若内容或图片失效,请留言反馈。
Proto.Actor Bootcamp

一、介绍Actor模型和Proto.Actor

1. 为何使用Actor模型

源: Docs\Bootcamp\Unit 1\Lesson 1: Why use the actor model
  • 无需手动控制线程
  • 高级抽象
  • 垂直伸缩(扩大)
  • 水平伸缩(扩展)
  • 容错和错误处理
  • 常见的体系结构

2. Actor模型适用的应用类型

源: Docs\Bootcamp\Unit 1\Lesson 2: Types of applications for which actors are suitable.
  • 交易应用
  • 可分包的应用
  • 服务
  • 通信应用
  • 多人游戏
  • 交通管理
  • 数值处理
  • 物联网

3. Proto.Actor在不同类型的应用中的使用

源: Docs\Bootcamp\Unit 1\Lesson 3: Use of Proto.Actor in different types of applications.
  • 服务端

    • 服务后端(Web Api等)
    • Web应用
  • 客户端

    • 控制台应用
    • WPF应用

4. 反应式编程宣言

源: Docs\Bootcamp\Unit 1\Lesson 4: The Reactive Manifesto

Proto.Actor 遵循反应式编程宣言

  • 事件驱动
  • 弹性伸缩
  • 容错恢复
  • 响应性

5. Proto.Actor的关键特性

源: Docs\Bootcamp\Unit 1\Lesson 5: Key features of the Proto.Actor.
  • 并发和异步
  • 分布式处理
  • 高性能
  • 容错
  • 在单个应用程序使用多种编程语言的能力

6. Actor和消息

源: Docs\Bootcamp\Unit 1\Lesson 6: Actors and messages.
  • Actor之间通过消息进行交互
  • 消息是异步的

7. Proto.Actor中Actor是什么

源: Docs\Bootcamp\Unit 1\Lesson 7: What’s an actor in Proto.Actor.
  • 一切都是Actor
  • 遵循 单一功能原则(Single responsibility principle)
  • Actor功能

    • 接收消息并处理
    • 创建更多的Actor
    • 给其他Actor发消息
    • 改变状态以便可以处理后续消息
  • Actor包括

    • State 状态
    • Behavior 行为

      • 对接收到的消息的处理
    • Mailbox 信箱

      • 默认先入先出(FIFO)
      • 可用算法控制优先级
    • Child actors 子级Actor

      • context.actorOf (...) 创建
      • context.stop (child) 停止
    • Supervisor strategy 监管策略

      • 重启子级Actor,保持子级Actor的状态
      • 重启子级Actor,恢复子级Actor到标准状态
      • 停止子级Actor
      • 传递错误到父级Actor

8. Proto.Actor中消息是什么

源: Docs\Bootcamp\Unit 1\Lesson 8: What’s a message in Proto.Actor.
  • Actor之间传递的数据(可以是普通的类)
  • 消息(在创建后)是不可变的
  • 消息是线程安全的

9. Proto.Actor中Props、RootContext和ActorContext分别是什么

源: Docs\Bootcamp\Unit 1\Lesson 9: What’re Props, RootContext, and ActorContext in Proto.Actor.
  • RootContext 创建初始(顶级)Actor, 和应用系统中的Actor交互
  • ActorContext 创建(普通, 非顶级)Actor, 和其他Actor交互, 存储Actor相关的数据
  • Props 一个配置类, 设置创建Actor的参数

10. Proto.Actor监管层级概述

源: Docs\Bootcamp\Unit 1\Lesson 10: Overview of the supervisor hierarchy in Proto. Actor.
  • 错误处理
  • 自我修复

11. 安装Proto.Actor

源: Docs\Bootcamp\Unit 1\Lesson 11: Installing Proto.Actor.
  • Proto. Actor 主要的NuGet包
  • Proto.Schedulers.SimpleScheduler 调度消息(延迟发送或间隔重复发送等)
  • Proto.Persistence.* 持久化Actor状态
  • Proto.Remote 远程
  • Proto.Cluster.* 集群
  • Proto.OpenTracing 追踪和调试Actor

二、 Actor和消息的定义和使用

1. 定义Actor

源: Docs\Bootcamp\Unit 2\Lesson 1: Defining Actors.
  • 一个继承自Proto.Actor的Actor基类的类
  • 定义可以处理的消息类型
  • 确保Actor的内部状态不会被其他Actor改变
  • 一个Actor的功能应该足够简单,如果太负责则应该拆分为单独的Actor
  • 注意Actor使用的计算资源和内存消耗

2. Actor引用

源: Docs\Bootcamp\Unit 2\Lesson 2: Actor References.
  • PID 区分Actor, Actor的抽象层

    • 唯一
    • 增量分配
    • Actor之间通过PID通信

3. 定义消息

源: Docs\Bootcamp\Unit 2\Lesson 3: Defining Messages.
  • 使用C#创建消息

    • 普通C#类
    • 类属性外部只读
  • Protocol Buffers (protobuf)

    • 字段唯一编号

      • 1~536870911 取值区间

        • 19000~19999 保留值区间(内部使用)
      • 1~15 占用1个字节
      • 16~2047 占用2个字节
    • option csharp_namespace 定义生成的C#代码的namespace
    • package 防止消息定义命名冲突

4. 发送消息的类型:

源: Docs\Bootcamp\Unit 2\Lesson 4: Types of Message Sending.
  • Send() 发送消息, 无需等待响应, 性能消耗小
  • RequestAsync() 发送消息, 等待响应, 可设置等待超时
  • Forward() 转发收到的消息到其他Actor

5. Actor实例化:

源: Docs\Bootcamp\Unit 2\Lesson 5: Actor Instantiation.

6. 定义Actor将要处理的消息:

源: Docs\Bootcamp\Unit 2\Lesson 6: Defining Which Messages an Actor will processing.

7. 发送自定义消息:

源: Docs\Bootcamp\Unit 2\Lesson 7: Sending a Custom Message.

三、 理解Actor的生命周期和状态

1. Actor生命周期

源: Docs\Bootcamp\Unit 3\Lesson 1: Actor Lifecycle.
  • Actor生命周期状态

    1. Starting ProcessMessageAsync()
    2. Receiving Messages
    3. Stopping

      • 产生原因:
      • 调用 ActorContext.Stop()ActorContext.StopAsync()
      • 子级Actor出现错误,父级Actor会发送停止消息给它

        • 状态拆分:
      • Stopping 收到Stop消息, 准备停止
      • Stop Actor已停止, 使用的资源已释放
    4. Restarting 停止之后, 收到Restart消息
    5. Terminated 收到Terminated消息, 不再接受任何消息, 不能被重新加载

2. Actor生命周期消息

源: Docs\Bootcamp\Unit 3\Lesson 2: Actor Lifecycle Messages.

3. 终止Actor和Actor的层次结构

源: Docs\Bootcamp\Unit 3\Lesson 3: Terminating Actors and Hierarchy of Actors.

4. PoisonPill消息及其用法

源: Docs\Bootcamp\Unit 3\Lesson 4: What is the Poison Pill message and how to work with it.

5. 可切换的Actor行为

源: Docs\Bootcamp\Unit 3\Lesson 5: Switchable Actor Behavior.
  • Behavior 运行时动态修改Actor的行为

    • Become() 清空行为栈,添加行为
    • BecomeStacked() 不清空行为栈,追加行为
    • ReceiveAsync(context) 消息处理

6. 使用行为切换重构

源: Docs\Bootcamp\Unit 3\Lesson 6: Refactoring with using behavior switching.

四、 创建Actor层级和错误处理

1. 监控和Actor层级

源: Docs\Bootcamp\Unit 4\Lesson 1: Supervisor and actor hierarchy.
  • system.Root.Spawn(props); 创建顶级(Actor)Actor
  • context.Spawn(props); 创建子级Actor

2. 演示监控者的能力和Actor层级的应用概览

源: Docs\Bootcamp\Unit 4\Lesson 2: Overview of the application that demonstrates the supervisor’s capabilities and the actors hierarchy.

3. Actor的地址和PID

源: Docs\Bootcamp\Unit 4\Lesson 3: Actor’s address and PID.
  • $"{system.ProcessRegistry.NextId()}/{name}" PID格式
  • var moviePlayCounterActorPid = new PID(system.ProcessRegistry.Address, "$1/MoviePlayCounterActor"); 手动创建PID

4. 创建 UserCoordinatorActor

源: Docs\Bootcamp\Unit 4\Lesson 4: Creating UserCoordinatorActor.

5. 创建 MoviePlayCounterActor

源: Docs\Bootcamp\Unit 4\Lesson 5: Creating MoviePlayCounterActor.

6. 父级Actor如何监管他们的子级Actor

源: Docs\Bootcamp\Unit 4\Lesson 6: How parent actors are watching over their children actors.
  • 继续处理(忽略错误)
  • 重启
  • 完全停止
  • 委托上层Actor处理

7. 控制子级Actor的策略

源: Docs\Bootcamp\Unit 4\Lesson 7: Strategies to control the state of children’s actors.
  • WithChildSupervisorStrategy() 重写子级Actor监控策略
  • public delegate SupervisorDirective Decider(PID pid, Exception reason); Decider委托
  • OneForOneStrategy 仅仅产生异常的子级Actor会执行从Decider()方法收到的指令(Directive)
  • AllForOneStrategy 所有的子级Actor都会执行从Decider()方法收到的指令(Directive)

五、 消息路由

使用路由控制消息流:

  • 性能 (使用Proto.Actor路由)
  • 消息内容 (使用常规Actor)
  • 状态

1. 路由模式

源: Docs\Bootcamp\Unit 5\Lesson 1: Router pattern.
  • 性能
  • 收到不同内容的消息
  • 取决于路由的状态

2. 利用Proto.Actor路由负载均衡

源: Docs\Bootcamp\Unit 5\Lesson 2: Load balancing with Proto.Actor routers.
  • 两种内置路由类型:

    • Pool 负责Actor的创建和完成后的移除,是Actor的父级Actor
    • Group 不参与Actor的创建,与Actor的层级结构无关
  • 几个内置路由:

    • Round-robin 顺序发送消息到Actor
    • Hash key 消息携带地址
    • Random 随机选择接收者
    • Weighted round robinRound-robin类似,但支持为特定的接收者定制权重
    • Broadcast 消息发给组里面的所有Actor

3. 路由池

源: Docs\Bootcamp\Unit 5\Lesson 3: Pool Router.
  • 路由创建
  • 远程路由 - 特殊消息:

    • RouterAddRoutee 将本地Actor或远程Actor的PID添加到路由表中
    • RouterRemoveRoutee 从路由表中移除PID
    • RouterGetRoutees 获取(当前路由器)使用的路由表
    • Routees 包含当前(路由器的)路由表状态
  • 检测

4. 路由组

源: Docs\Bootcamp\Unit 5\Lesson 4: Group Router.
  • 组创建

5. 一致性哈希路由

源: Docs\Bootcamp\Unit 5\Lesson 5: ConsistentHashing Router.
  1. 消息(Message)转换为消息键(Message Key)对象。IHashable接口
  2. 基于消息键(Message Key)创建散列码(Hash Code)
  3. 根据哈希码(Hash Code)选择虚拟主机(Virtual host)
  4. 发送消息到选择的虚拟主机

6. 使用Actor实现路由模式

源: Docs\Bootcamp\Unit 5\Lesson 6: Implementation of the router pattern with using actors.
  • 根据内容路由
  • 基于状态路由

六、 消息通道

1. 通道类型

源: Docs\Bootcamp\Unit 6\Lesson 1: Channels Types.
  • point-to-point, 连接发送者(sender)和接受者(recipient)
  • publisher/subscriber, 可运行期间动态改变接受者,实现EventStream

2. 点对点通道

源: Docs\Bootcamp\Unit 6\Lesson 2: Point-to-point Channel.
  • 每条消息只会发给某一个接收者
  • 发送者知道接收者

3. 发布/订阅通道

源: Docs\Bootcamp\Unit 6\Lesson 3: Publisher/Subscriber Channel.
  • 单条消息可以发送给多个接收者
  • 发送者不关心接收者是谁

4. 事件流

源: Docs\Bootcamp\Unit 6\Lesson 4: EventStream.
  • Subscribe<T>()
  • Unsubscribe()
  • Publish(msg)

5. 死信(无法投寄的信)

源: Docs\Bootcamp\Unit 6\Lesson 5: DeadLetter Channel.

6. 保证投递

源: Docs\Bootcamp\Unit 6\Lesson 6: Guaranteed delivery.

七、Proto.Actor远程

1. 水平伸缩是什么

源: Docs\Bootcamp\Unit 7\Lesson 1: What is horizontal scaling.
  • 典型网络拓扑

    • Bus 总线
    • Star 星形
    • Ring 环形
    • Mesh 网状
    • Tree 树形
    • Point-to-Point 点对点

2. Proto.Actor远程概览

源: Docs\Bootcamp\Unit 7\Lesson 2: Overview Proto.Actor Remote.
  • Proto.Remote关键特性

    • 位置透明(Location transparency)
    • 远程创建(Remote spawning)
    • gRPC流(gRPC Streams)

3. Proto.Actor远程使用示例

源: Docs\Bootcamp\Unit 7\Lesson 3: Example of working with Proto.Actor Remote.

八、Proto.Actor集群

1. 为什么你需要集群

源: Docs\Bootcamp\Unit 8\Lesson 1: Why do you need clusters.

2. 集群中的成员

源: Docs\Bootcamp\Unit 8\Lesson 2: Membership in the cluster.

3. 加入集群

源: Docs\Bootcamp\Unit 8\Lesson 3: Joining to the cluster.
  • 分布式架构中,服务发现的功能

    • 集群中的元信息服务的一致性
    • 注册和监控组件可用性的机制
    • 组件发现的机制
  • 服务发现

    • Proto.Cluster.Consul
    • Proto.Cluster.SingleRemoteInstance

4. 在集群中处理任务

源: Docs\Bootcamp\Unit 8\Lesson 4: Processing tasks in the Cluster
  • TODO: 学习记录, 此处继续
0

评论 (0)

取消