序
本文次要钻研一下 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
- broadcast_cluster