Distributed Ruby Objects (以下簡稱 drb),是 Ruby Standard Library 中用來做到類似 RPC (Remote procedure call) 的函式庫,以下紀錄最基本的 Server 端和 Client 端的 sample。
Server:
require ‘drb’
class TimeServer
def get_current_time
Time.now
enddef stop_server
DRb.stop_service
endend
uri = ‘druby://localhost:7788′
front_object = TimeServer.newDRb.start_service(uri, front_object)
# Wait for the drb server thread to finish before exiting.
DRb.thread.join
這樣一來最簡單的 Server 端就OK了,只要執行這段 Ruby code,Client端就可以用 druby://localhost:7788 來叫用服務。
Client:
require ‘drb’
server_uri = “druby://localhost:7788″
timeserver = DRbObject.new_with_uri(server_uri)
#call remote object method ‘get_current_time’
puts timeserver.get_current_time#stop remote service
timeserver.stop_server
就這麼簡單,不過方便歸方便,安全的問題也要特別注意
若想深入了解的人可以參考以下連結: