关于dubbo:聊聊dubbogo的RPCInvocation

39次阅读

共计 3556 个字符,预计需要花费 9 分钟才能阅读完成。

本文次要钻研一下 dubbo-go 的 RPCInvocation

Invocation

dubbo-go-v1.4.2/protocol/invocation.go

// Invocation ...
type Invocation interface {MethodName() string
    ParameterTypes() []reflect.Type
    ParameterValues() []reflect.Value
    Arguments() []interface{}
    Reply() interface{}
    Attachments() map[string]string
    AttachmentsByKey(string, string) string
    Invoker() Invoker}
  • Invocation 定义了 MethodName、ParameterTypes、ParameterValues、Arguments、Reply、Attachments、AttachmentsByKey、Invoker 办法

RPCInvocation

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

type RPCInvocation struct {
    methodName      string
    parameterTypes  []reflect.Type
    parameterValues []reflect.Value
    arguments       []interface{}
    reply           interface{}
    callBack        interface{}
    attachments     map[string]string
    invoker         protocol.Invoker
    lock            sync.RWMutex
}
  • RPCInvocation 定义了 methodName、parameterTypes、parameterValues、arguments、reply、callBack、attachments、invoker、lock 属性

NewRPCInvocation

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

// NewRPCInvocation ...
func NewRPCInvocation(methodName string, arguments []interface{}, attachments map[string]string) *RPCInvocation {
    return &RPCInvocation{
        methodName:  methodName,
        arguments:   arguments,
        attachments: attachments,
    }
}
  • NewRPCInvocation 办法实例化 RPCInvocation

NewRPCInvocationWithOptions

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

// NewRPCInvocationWithOptions ...
func NewRPCInvocationWithOptions(opts ...option) *RPCInvocation {invo := &RPCInvocation{}
    for _, opt := range opts {opt(invo)
    }
    return invo
}
  • NewRPCInvocationWithOptions 办法通过 option 实例化 RPCInvocation

option

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

type option func(invo *RPCInvocation)

// WithMethodName ...
func WithMethodName(methodName string) option {return func(invo *RPCInvocation) {invo.methodName = methodName}
}

// WithParameterTypes ...
func WithParameterTypes(parameterTypes []reflect.Type) option {return func(invo *RPCInvocation) {invo.parameterTypes = parameterTypes}
}

// WithParameterValues ...
func WithParameterValues(parameterValues []reflect.Value) option {return func(invo *RPCInvocation) {invo.parameterValues = parameterValues}
}

// WithArguments ...
func WithArguments(arguments []interface{}) option {return func(invo *RPCInvocation) {invo.arguments = arguments}
}

// WithReply ...
func WithReply(reply interface{}) option {return func(invo *RPCInvocation) {invo.reply = reply}
}

// WithCallBack ...
func WithCallBack(callBack interface{}) option {return func(invo *RPCInvocation) {invo.callBack = callBack}
}

// WithAttachments ...
func WithAttachments(attachments map[string]string) option {return func(invo *RPCInvocation) {invo.attachments = attachments}
}

// WithInvoker ...
func WithInvoker(invoker protocol.Invoker) option {return func(invo *RPCInvocation) {invo.invoker = invoker}
}
  • option 提供了 WithMethodName、WithParameterTypes、WithParameterValues、WithReply、WithCallBack、WithAttachments、WithInvoker 办法

AttachmentsByKey

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

// AttachmentsByKey ...
func (r *RPCInvocation) AttachmentsByKey(key string, defaultValue string) string {r.lock.RLock()
    defer r.lock.RUnlock()
    if r.attachments == nil {return defaultValue}
    value, ok := r.attachments[key]
    if ok {return value}
    return defaultValue
}
  • AttachmentsByKey 办法通过 r.attachments[key] 获取对应 key 的值

SetAttachments

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

// SetAttachments ...
func (r *RPCInvocation) SetAttachments(key string, value string) {r.lock.Lock()
    defer r.lock.Unlock()
    if r.attachments == nil {r.attachments = make(map[string]string)
    }
    r.attachments[key] = value
}
  • SetAttachments 办法通过 r.attachments[key] 给指定 key 赋值,如果 r.attachments 为 nil 则通过 make 创立

小结

RPCInvocation 定义了 methodName、parameterTypes、parameterValues、arguments、reply、callBack、attachments、invoker、lock 属性;NewRPCInvocation 办法实例化 RPCInvocation;NewRPCInvocationWithOptions 办法通过 option 实例化 RPCInvocation

doc

  • invocation

正文完
 0