聊聊dubbo-go的broadcastCluster

2020-08-11 10:17:29 浏览数 (1)

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

broadcastCluster

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

代码语言:javascript复制
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

代码语言:javascript复制
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

代码语言:javascript复制
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: %vn", 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

  • broadcast_cluster

0 人点赞