聊聊dubbogo的failsafeCluster
序
本文主要研究一下dubbo-go的failsafeCluster
failsafeCluster
dubbo-go-v1.4.2/cluster/cluster_impl/failsafe_cluster.go
type failsafeCluster struct{}const failsafe = "failsafe"
func init() {
extension.SetCluster(failsafe, NewFailsafeCluster)
}
// NewFailsafeCluster ...
func NewFailsafeCluster() cluster.Cluster {
return &failsafeCluster{}
}
func (cluster *failsafeCluster) Join(directory cluster.Directory) protocol.Invoker {
return newFailsafeClusterInvoker(directory)
}
- failsafeCluster的join方法执行newFailsafeClusterInvoker
newFailsafeClusterInvoker
dubbo-go-v1.4.2/cluster/cluster_impl/failsafe_cluster_invoker.go
type failsafeClusterInvoker struct { baseClusterInvoker
}
func newFailsafeClusterInvoker(directory cluster.Directory) protocol.Invoker {
return &failsafeClusterInvoker{
baseClusterInvoker: newBaseClusterInvoker(directory),
}
}
- newFailsafeClusterInvoker方法创建failsafeClusterInvoker
Invoke
dubbo-go-v1.4.2/cluster/cluster_impl/failsafe_cluster_invoker.go
func (invoker *failsafeClusterInvoker) 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{}
}
url := invokers[0].GetUrl()
methodName := invocation.MethodName()
//Get the service loadbalance config
lb := url.GetParam(constant.LOADBALANCE_KEY, constant.DEFAULT_LOADBALANCE)
//Get the service method loadbalance config if have
if v := url.GetMethodParam(methodName, constant.LOADBALANCE_KEY, ""); v != "" {
lb = v
}
loadbalance := extension.GetLoadbalance(lb)
invoked := make([]protocol.Invoker, 0)
var result protocol.Result
ivk := invoker.doSelect(loadbalance, invocation, invokers, invoked)
//DO INVOKE
result = ivk.Invoke(ctx, invocation)
if result.Error() != nil {
// ignore
logger.Errorf("Failsafe ignore exception: %v.
", result.Error().Error())
return &protocol.RPCResult{}
}
return result
}
- Invoke方法先通过invoker.directory.List(invocation)获取invokers,之后通过invoker.doSelect(loadbalance, invocation, invokers, invoked)选择ivk,最后执行ivk.Invoke(ctx, invocation),如果有error,则打印error,返回空结果
GetLoadbalance
dubbo-go-v1.4.2/common/extension/loadbalance.go
// GetLoadbalance ...func GetLoadbalance(name string) cluster.LoadBalance {
if loadbalances[name] == nil {
panic("loadbalance for " + name + " is not existing, make sure you have import the package.")
}
return loadbalances[name]()
}
- GetLoadbalance方法根据执行的name获取cluster.LoadBalance,默认为randomLoadBalance
小结
failsafeCluster的join方法执行newFailsafeClusterInvoker;Invoke方法先通过invoker.directory.List(invocation)获取invokers,之后通过invoker.doSelect(loadbalance, invocation, invokers, invoked)选择ivk,最后执行ivk.Invoke(ctx, invocation),如果有error,则打印error,返回空结果
与failfastCluster的区别就是针对error进行log,然后返回的始终的是没有err的
以上是 聊聊dubbogo的failsafeCluster 的全部内容, 来源链接: utcz.com/z/519183.html