Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

RSocket SDK Stack

linux_china edited this page Oct 15, 2020 · 11 revisions

RSocket SDK Stack

RSocket作为一个标准的协议,目前支持各种主流开发语言的SDK对接:

  • Java语言: 支持Kotlin, Groovy, Scala等JVM之上语言
  • JavaScript: 同时支持浏览器端JavaScript、Node.js后端和Deno TypeScript
  • Rust: 支持WebAssembly对接
  • RSocket并没有限制数据流格式,你可以根据实际场景选择各种序列化框架

多语言SDK的Proxy模式介绍

RSocket提供了非常多语言的SDK,那么如何让各种语言SDK调用更能和语言贴近(Language Native),这里我们建议使用Proxy模式。 Proxy模式是将对象的函数调用通过Proxy机制转换到用户自定义的行为处理上,也就是通常说的Unknown method call和method missing。

让我们看一下各个语言对应的Proxy机制

  • Java Proxy InvocationHandler
public class DynamicInvocationHandler implements InvocationHandler {
 
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) 
    
    }
}
  • JavaScript Proxy对象

更详细的介绍可参考: Why Proxy is a Gem in JavaScript? https://medium.com/javascript-in-plain-english/why-proxies-in-javascript-are-fantastic-db100ddc10a0

function constructInvocationHandler(options) {
    let handler = {
        get(target, methodName) {
            let serviceName = options.serviceName;
            return (...args) => {
                console.log(serviceName)
                console.log(methodName)
                console.log(args);
            }
        }
    };
    return new Proxy({}, handler);
}
  • TypeScript Proxy机制:
function constructInvocationHandler<T>(options: any): T {
    let handler = {
        get(target:any, methodName:string) {
            return (...args:any[]) => {
                console.log(options.serviceName)
                console.log(methodName)
                console.log(args);
                return "good"
            }
        }
    };
    return new Proxy({}, handler);
}
  • PHP __call
   public function __call($name, $args)
    {
        echo "Calling $name $args[0]";
    }
  • Ruby' method_missing hook
def method_missing(m, *args, &block) 
   m_to_s
end
  • Python的__getattr__
class PythonInvocationHandler(object):
    def __init__(self, service_name):
        self.service_name = service_name
        
    def __getattr__(self, name):
        def _missing(*args, **kwargs):
            print "A missing method was called."
            print "The object was %r, the method was %r. " % (self, name)
            print "It was called with %r and %r as arguments" % (args, kwargs)
        return _missing

更多语言的Proxy实现机制介绍在这里: https://rosettacode.org/wiki/Respond_to_an_unknown_method_call

Alibaba RSocket多语言接入

Alibaba RSocket Broker采用标准的RSocket协议,但是在接入时还需要提供额外的一些metadata信息,如在连接时,需要提供:

  • 应用信息(message/x.rsocket.application+json): 提供接入方的信息,主要如下:
{
    "id": "uuid-1113" ,
    "name:" : "app-1",
    "ip": "192.168.1.2"
}
  • JWT信息(message/x.rsocket.authentication.v0): 标准的接入的安全认证信息,如果Broker没有启动安全信息,则不需要。

在调用时,还需要提供三个metadata:

  • 路由元信息(message/x.rsocket.routing.v0): 如 com.alibaba.user.UserService.findUserById
  • 数据编码元信息(message/x.rsocket.mime-type.v0): 如 json等

可以参考 RawRSocketTest.java

参考

RSocket

Network Protocol

  • Binary: byte stream
  • Async message
  • Multi transports
  • Reactive Semantics

Symmetric interactions

  • request/response
  • request/stream
  • fire-and-forget
  • channel

Transports

  • TCP+TLS
  • WebSocket+TLS
  • UDP(Aeron)
  • RDMA

Polyglot

Clone this wiki locally