Skip to content

Commit

Permalink
feat: 杂\n test \n Closes apache#1
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaochangbai committed Dec 20, 2021
1 parent ad2b60f commit a9ca7e9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,25 @@ public Class<T> getInterface() {
@Override
public Result invoke(Invocation invocation) throws RpcException {
Result result;

// 获取 mock 配置值
String value = getUrl().getMethodParameter(invocation.getMethodName(), MOCK_KEY, Boolean.FALSE.toString()).trim();
if (value.length() == 0 || "false".equalsIgnoreCase(value)) {
//no mock
// 无 mock 逻辑,直接调用其他 Invoker 对象的 invoke 方法,
// 比如 FailoverClusterInvoker
result = this.invoker.invoke(invocation);
} else if (value.startsWith(FORCE_KEY)) {
if (logger.isWarnEnabled()) {
logger.warn("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + getUrl());
}
//force:direct mock
// force:xxx 直接执行 mock 逻辑,不发起远程调用
result = doMockInvoke(invocation, null);
} else {
//fail-mock
//// fail:xxx 表示消费方对调用服务失败后,再执行 mock 逻辑,不抛出异常
try {
//// 调用其他 Invoker 对象的 invoke 方法
result = this.invoker.invoke(invocation);

//fix:#4585
Expand All @@ -122,6 +127,7 @@ public Result invoke(Invocation invocation) throws RpcException {
if (logger.isWarnEnabled()) {
logger.warn("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + getUrl(), e);
}
// 调用失败,执行 mock 逻辑
result = doMockInvoke(invocation, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ private void doExportUrls() {

repository.registerProvider(providerModel);

// 加载注册中心链接
List<URL> registryURLs = ConfigValidationUtils.loadRegistries(this, true);

// 遍历 protocols,并在每个协议下导出服务
for (ProtocolConfig protocolConfig : protocols) {
String pathKey = URL.buildKey(getContextPath(protocolConfig)
.map(p -> p + "/" + path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ public Result invoke(Invocation invocation) throws RpcException {
return invoker.invoke(invocation);
}
}
//MockClusterInvoker(MockClusterInvoker(服务降级逻辑))
return currentAvailableInvoker.invoke(invocation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,14 @@ public <T> Exporter<T> export(final Invoker<T> originInvoker) throws RpcExceptio

providerUrl = overrideUrlWithConfig(providerUrl, overrideSubscribeListener);
//export invoker
// 导出服务
final ExporterChangeableWrapper<T> exporter = doLocalExport(originInvoker, providerUrl);

// url to registry
// 根据 URL 加载 Registry 实现类,比如 ZookeeperRegistry
final Registry registry = getRegistry(registryUrl);
// 获取注册中心 URL,以 zookeeper 注册中心为例,得到的示例 URL 如下:
// zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.2&export=dubbo%3A%2F%2F172.17.48.52%3A20880%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider
final URL registeredProviderUrl = getUrlToRegistry(providerUrl, registryUrl);

// decide if we need to delay publish
Expand Down Expand Up @@ -288,7 +292,9 @@ private <T> ExporterChangeableWrapper<T> doLocalExport(final Invoker<T> originIn
String key = getCacheKey(originInvoker);

return (ExporterChangeableWrapper<T>) bounds.computeIfAbsent(key, s -> {
// 创建 Invoker 为委托类对象
Invoker<?> invokerDelegate = new InvokerDelegate<>(originInvoker, providerUrl);
// 调用 protocol 的 export 方法导出服务 DubboProtocol
return new ExporterChangeableWrapper<>((Exporter<T>) protocol.export(invokerDelegate), originInvoker);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public int getDefaultPort() {
@Override
public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
if (UrlUtils.isRegistry(invoker.getUrl())) {
//InjvmProtocol
//本地导出用InjvmProtocol,远程导出用RegistryProtocol
return protocol.export(invoker);
}
//protocol = DubboProtocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
rpcInvocation.put(Constants.METHOD_MODEL, ((ConsumerModel) serviceModel).getMethodModel(method));
}

//MigrationInvoker
return invoker.invoke(rpcInvocation).recreate();
}
}

0 comments on commit a9ca7e9

Please sign in to comment.