Chinaunix首页 | 论坛 | 博客
  • 博客访问: 386237
  • 博文数量: 69
  • 博客积分: 1984
  • 博客等级: 上尉
  • 技术积分: 953
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-28 00:43
个人简介

学无所长,一事无成

文章分类

全部博文(69)

文章存档

2015年(19)

2014年(14)

2013年(9)

2012年(17)

2010年(10)

我的朋友

分类: Python/Ruby

2015-02-08 00:11:32

原文地址:

所有 DCell 提供的 services 都必须注册成 Celluloid actors。以下将简要介绍创建及注册 actors 的最关键内容,要了解更多信息,请仔细。

DCell exposes all Celluloid actors you've registered directly onto the network. The best way to register an actor is by supervising it. 下面这个例子演示如何创建一个 acotr 并将其注册到网络上:

  1. class TimeServer
  2.   include Celluloid

  3.   def time
  4.     "The time is: #{Time.now}"
  5.   end
  6. end
现在我们已经定义了一个 TimeServer,接下来我们需要在 local registry 中将其注册并提交监控(supervise):

  1. >> TimeServer.supervise_as :time_server
  2.  => #<Celluloid::Supervisor(TimeServer):0xee4>
可被监控的 actors 的意思就是,一旦 actors 崩溃了(crash),他们会自动重启,并以同样的名字再次注册。我们可以使用 Celluloid::Actor#[] 方法来访问注册的 actors:

  1. >> Celluloid::Actor[:time_server]
  2.  => #<Celluloid::Actor(TimeServer:0xee8)>
  3. >> Celluloid::Actor[:time_server].time
  4.  => "The time is: 2011-11-10 20:17:48 -0800"
DCell 中一个同样的 actor 也生效了,使用 DCell::Node#[] 语法可以访问到这同一个 actor。下面例子假设 local node (既 DCell.id) 为 "cryptosphere.local":

  1. >> node = DCell::Node["cryptosphere.local"]
  2.  => #<DCell::Node[cryptosphere.local] @addr="tcp://127.0.0.1:1870">
  3. >> node[:time_server].time
  4.  => "The time is: 2011-11-10 20:28:27 -0800"


阅读(651) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~