序
本文主要研究一下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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。