本文主要研究一下dubbo-go的broadcastCluster

broadcastCluster

dubbo-go-v1.4.2/cluster/cluster_impl/broadcast_cluster.go

type broadcastCluster struct{}

const broadcast = "broadcast"

func init() {
    extension.SetCluster(broadcast, NewBroadcastCluster)
}

// NewBroadcastCluster ...
func NewBroadcastCluster() cluster.Cluster {
    return &broadcastCluster{}
}

func (cluster *broadcastCluster) Join(directory cluster.Directory) protocol.Invoker {
    return newBroadcastClusterInvoker(directory)
}
  • broadcastCluster的join方法执行newBroadcastClusterInvoker

newBroadcastClusterInvoker

dubbo-go-v1.4.2/cluster/cluster_impl/broadcast_cluster_invoker.go

type broadcastClusterInvoker struct {
    baseClusterInvoker
}

func newBroadcastClusterInvoker(directory cluster.Directory) protocol.Invoker {
    return &broadcastClusterInvoker{
        baseClusterInvoker: newBaseClusterInvoker(directory),
    }
}
  • newBroadcastClusterInvoker方法实例化broadcastClusterInvoker

Invoke

dubbo-go-v1.4.2/cluster/cluster_impl/broadcast_cluster_invoker.go

func (invoker *broadcastClusterInvoker) Invoke(ctx context.Context, invocation protocol.Invocation) protocol.Result {
    invokers := invoker.directory.List(invocation)
    err := invoker.checkInvokers(invokers, invocation)
    if err != nil {
        return &protocol.RPCResult{Err: err}
    }
    err = invoker.checkWhetherDestroyed()
    if err != nil {
        return &protocol.RPCResult{Err: err}
    }

    var result protocol.Result
    for _, ivk := range invokers {
        result = ivk.Invoke(ctx, invocation)
        if result.Error() != nil {
            logger.Warnf("broadcast invoker invoke err: %v when use invoker: %v\n", result.Error(), ivk)
            err = result.Error()
        }
    }
    if err != nil {
        return &protocol.RPCResult{Err: err}
    }
    return result
}
  • Invoke方法首先通过invoker.directory.List(invocation)获取invokers,之后遍历invokers,挨个执行result = ivk.Invoke(ctx, invocation),如果最后err不为nil,则返回err,否则返回最后的result

小结

broadcastCluster的join方法执行newBroadcastClusterInvoker;其Invoke方法首先通过invoker.directory.List(invocation)获取invokers,之后遍历invokers,挨个执行result = ivk.Invoke(ctx, invocation),如果最后err不为nil,则返回err,否则返回最后的result

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论