Broadcast Cluster
廣播調用所有提供者,逐個調用,任意一台報錯則報錯 2。通常用于通知所有提供者更新緩存或日志等本地資源資訊。
public class BroadcastCluster implements Cluster {
public <T> Invoker<T> join(Directory<T> directory) throws RpcException {
//廣播調用的處理操作在BroadcastClusterInvoker中
return new BroadcastClusterInvoker<T>(directory);
}
}
public class BroadcastClusterInvoker<T> extends AbstractClusterInvoker<T> {
private static final Logger logger = LoggerFactory.getLogger(BroadcastClusterInvoker.class);
public BroadcastClusterInvoker(Directory<T> directory) {
super(directory);
}
@SuppressWarnings({"unchecked", "rawtypes"})
public Result doInvoke(final Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
checkInvokers(invokers, invocation);
RpcContext.getContext().setInvokers((List) invokers);
RpcException exception = null;
Result result = null;
//依次調用所有的服務,不需要負載均衡政策
for (Invoker<T> invoker : invokers) {
try {
result = invoker.invoke(invocation);
} catch (RpcException e) {
exception = e;
logger.warn(e.getMessage(), e);
} catch (Throwable e) {
exception = new RpcException(e.getMessage(), e);
logger.warn(e.getMessage(), e);
}
}
//隻要存在某個服務調用失敗就抛出異常
if (exception != null) {
throw exception;
}
return result;
}
}