Dubbo集群容错模式之Failover实现
生活随笔
收集整理的這篇文章主要介紹了
Dubbo集群容错模式之Failover实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
注: Dubbo版本是2.6.2
? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?圖1 Dubbo的FailoverClusterInvoker類繼承圖
1.Failover的含義
????Failover可以理解為當出現失敗,重試其它服務器。
2.Failover的實現
? ? 核心代碼在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); }- 首先得到嘗試次數len。
- 根據loadbalance從候選服務提供者當中選取一個服務提供者,而后調用。如果第一次就成功,則直接返回調用結果;如果調用不成功,且異常是Biz異常,則直接拋出該異常;多次調用之后才成功,則在warn級別允許的情況下會將多次調用的信息記錄到日志,之后返回調用成功的結果;如果嘗試的次數超過了len,還是沒有成功過,則拋出異常。
? ? 由代碼可以看出,拋出異常的情況分倆中,一種是服務端返回的是業務異常;另一種是嘗試次數超過了設置的閥值。
轉載于:https://my.oschina.net/u/2518341/blog/1814883
總結
以上是生活随笔為你收集整理的Dubbo集群容错模式之Failover实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用友3.0谋局“新两化” 融合创新迸发新
- 下一篇: tortoiseGit no git.e