首页 > 代码库 > 《Distributed Programming With Ruby》读书笔记一Drb:Hellowold and Pass by Reference

《Distributed Programming With Ruby》读书笔记一Drb:Hellowold and Pass by Reference

《Distributed Programming With Ruby》Mark Bates
Preface:
  • The author was using Java and RMI(remote method invocation) as distributed interface in 2001. To solve performence problems, he found DRb. "I was already impressed with Ruby for being a terse language, but when I found the DRb(Distributed Ruby, also known as dRuby)package, I became a believer. "
  • This book is quite simply written for the intermediate to advanced Ruby developer who wants to start developing distributed applications.
  • You‘ll learn about RMI, message queues, and MapReduce, among others.
  • Unless otherwise stated, you should be able to take any code sample, copy it into a Ruby  file, and run it using the ruby command, like this:
$ ruby foo.rb 
Part I standard Library
     The first part of this book takes a deep dive into two main libraries: DRb and Rinda
  • Chapter1: Distributed Ruby(DRb)
    • Including 6 parts:
      •   Hello World
      •   Proprietary Ruby Objects
      •   Security
      •   ID Conversion
      •   Conclusion
      •   Endnotes
    • 技术分享技术分享
    • Here is what the documentation says when introducing DRb:
      • dRuby is a distributed object system for Ruby. It allows an object in one Ruby  process to invoke methods on an object in another Ruby process on the same ora different machine.
    • Hello World
    • Example: Using Java RMI to write a distributed program
      • Difine interface
      • Sever
      • Client
      • Whe you start doing something more complex, you can quickly become swamped with overwhelming interfaces, classes, stubs, registries, and so on.
    • Example: DRy
      • Server:
        • 技术分享技术分享
      • Client:
        • 技术分享
        • #=> "Hello, world!"
      • When we call DRbObject.new_with_uri("druby://127.0.0.1:61676"),we actually get back an instance of DRb::DRbObject and not the HelloWorld-Server class. If we "puts server.inspect" in Client, we will see:"#<DRb::DRbObject:0x1ebc328 @uri=\"druby://127.0.0.1:61676\", @ref=nil>"
      • 技术分享技术分享
    • Example: Distributed Logger
    • Including "Logger" Module
      • Server:
        • 技术分享技术分享
      • Client:
        • 技术分享
      • If we were to run our client application, we would see something like the following show up in our server logs:
        • 技术分享
    • Proprietary Ruby Objects
      • Include DRbUndumped
      • Class User
        • 技术分享
          技术分享
      • Server
      •      技术分享
      • Client
        • 技术分享
          技术分享
      • Error
        •  技术分享
        • 技术分享
      • 技术分享
        技术分享
      • In our “User Server” example, the User class can be serialized, but it can‘t be deserialized in the client, because the client does not have the class definition for User in its virtual machine. That‘s why the inspect on the User class comes back with  DRb::DRbUnknown and you get a NoMethodError when you try to call the username method on the returned class.
      • How do we solve this problem? We could, of course, copy the User class definition from the UserServer to the client. That would work. The problem with this approach is that it starts to get unwieldy after a while, depending on the size of your application. Plus, you are duplicating code and generally confusing the issue.
      • Modify:
      • 技术分享技术分享
      • Output
      • 技术分享技术分享
      • Normally when an object is returned by a method called over DRb, the object is marshaled, sent over the connection, and run on the client side. As mentioned earlier,this works wonderfully if the object returned has a class definition that exists on the client side. When we include DRbUndumped into a class, as we did with the User class, we tell DRb that the object cannot be marshaled and that DRb should send a reference to it instead. The object then stays on the server side, and the  reference on the client side then proxies the method calls back and forth to the server
      • 技术分享技术分享

        技术分享

         

《Distributed Programming With Ruby》读书笔记一Drb:Hellowold and Pass by Reference