Dubbo集群容错模式之Failover实现
生活随笔
收集整理的這篇文章主要介紹了
Dubbo集群容错模式之Failover实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
注: Dubbo版本是2.6.2
? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?圖1 Dubbo的FailoverClusterInvoker類繼承圖
1.Failover的含義
????Failover可以理解為當(dāng)出現(xiàn)失敗,重試其它服務(wù)器。
2.Failover的實(shí)現(xiàn)
? ? 核心代碼在FailoverClusterInvoker的doInvoke(Invocation,List<Invoker<T>>,LoadBalance)中,源碼如下。
@Override @SuppressWarnings({"unchecked", "rawtypes"}) public Result doInvoke(Invocation invocation, final List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {List<Invoker<T>> copyinvokers = invokers;checkInvokers(copyinvokers, invocation);int len = getUrl().getMethodParameter(invocation.getMethodName(), Constants.RETRIES_KEY, Constants.DEFAULT_RETRIES) + 1;if (len <= 0) {len = 1;}// retry loop.RpcException le = null; // last exception.List<Invoker<T>> invoked = new ArrayList<Invoker<T>>(copyinvokers.size()); // invoked invokers.Set<String> providers = new HashSet<String>(len);for (int i = 0; i < len; i++) {//Reselect before retry to avoid a change of candidate `invokers`.//NOTE: if `invokers` changed, then `invoked` also lose accuracy.if (i > 0) {checkWhetherDestroyed();copyinvokers = list(invocation);// check againcheckInvokers(copyinvokers, invocation);}Invoker<T> invoker = select(loadbalance, invocation, copyinvokers, invoked);invoked.add(invoker);RpcContext.getContext().setInvokers((List) invoked);try {Result result = invoker.invoke(invocation);if (le != null && logger.isWarnEnabled()) {logger.warn("Although retry the method " + invocation.getMethodName()+ " in the service " + getInterface().getName()+ " was successful by the provider " + invoker.getUrl().getAddress()+ ", but there have been failed providers " + providers+ " (" + providers.size() + "/" + copyinvokers.size()+ ") from the registry " + directory.getUrl().getAddress()+ " on the consumer " + NetUtils.getLocalHost()+ " using the dubbo version " + Version.getVersion() + ". Last error is: "+ le.getMessage(), le);}return result;} catch (RpcException e) {if (e.isBiz()) { // biz exception.throw e;}le = e;} catch (Throwable e) {le = new RpcException(e.getMessage(), e);} finally {providers.add(invoker.getUrl().getAddress());}}throw new RpcException(le != null ? le.getCode() : 0, "Failed to invoke the method "+ invocation.getMethodName() + " in the service " + getInterface().getName()+ ". Tried " + len + " times of the providers " + providers+ " (" + providers.size() + "/" + copyinvokers.size()+ ") from the registry " + directory.getUrl().getAddress()+ " on the consumer " + NetUtils.getLocalHost() + " using the dubbo version "+ Version.getVersion() + ". Last error is: "+ (le != null ? le.getMessage() : ""), le != null && le.getCause() != null ? le.getCause() : le); }- 首先得到嘗試次數(shù)len。
- 根據(jù)loadbalance從候選服務(wù)提供者當(dāng)中選取一個(gè)服務(wù)提供者,而后調(diào)用。如果第一次就成功,則直接返回調(diào)用結(jié)果;如果調(diào)用不成功,且異常是Biz異常,則直接拋出該異常;多次調(diào)用之后才成功,則在warn級(jí)別允許的情況下會(huì)將多次調(diào)用的信息記錄到日志,之后返回調(diào)用成功的結(jié)果;如果嘗試的次數(shù)超過了len,還是沒有成功過,則拋出異常。
? ? 由代碼可以看出,拋出異常的情況分倆中,一種是服務(wù)端返回的是業(yè)務(wù)異常;另一種是嘗試次數(shù)超過了設(shè)置的閥值。
轉(zhuǎn)載于:https://my.oschina.net/u/2518341/blog/1814883
總結(jié)
以上是生活随笔為你收集整理的Dubbo集群容错模式之Failover实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用友3.0谋局“新两化” 融合创新迸发新
- 下一篇: tortoiseGit no git.e