12. EventMachineのサンプル1
エコーサーバ
require 'eventmachine'
module Echo
def receive_data(data)
send_data(data)
end
end
EM.run do
EM.start_server("0.0.0.0", 10000, Echo)
end
デモ
13. EventMachineのサンプル2
HTTPクライアント
require 'eventmachine'
hosts = [“www.google.co.jp”, “www.yahoo.co.jp”]
hosts_size = hosts.size
EM.run do
hosts.each do |h|
http = EM::Protocols::HttpClient.request(:host =>
h, :port => 80, :request => “/”)
http.callback do |response|
p response[:status]
EM.stop if (hosts_size -= 1) <= 0
end
end
end
デモ
17. サンプルを動かす1
Sub
subject:foo
Pub
NATS Sub
subject:foo
subject:foo
message:hello Server
Sub
subject:bar
デモ
18. サンプルを動かす2
Sub
subject:foo.>
Pub
subject:foo.bar
message:hello
NATS Sub
subject:foo.*
Server
Pub
subject:foo.bar.hoge
message:hello
Sub
subject:foo
デモ
19. サンプルを動かす3
queue
Sub
subject:foo
Pub
NATS Sub
subject:foo
subject:foo
message:hello Server
3つのうち Sub
どれか1つにpublish subject:foo
デモ
20. NATSサーバの起動
@ lib/nats/server.rb 25行目~
• 左:EMのechoサーバ • 右:NATSサーバの開始
require 'eventmachine' EM.run do
EM.start_server(
module Echo NATSD::Server.host,
def receive_data(data) NATSD::Server.port,
send_data(data) NATSD::Connection
end )
end end
EM.run do
EM.start_server("0.0.0.0",
10000, Echo)
Echo
end
30. 青い
矢印が
NATSとCloud
NATSの Foundry(2)
メッセージ
NATSが
落ちても
Serviceは
動くはず?
Cloud Foundry The building of the Open PaaS OSCON2011
http://www.slideshare.net/derekcollison/oscon-2011