先思考下,如果要实现服务消费,需要做哪些事情?
- 生成远程服务的代理
- 获得目标服务的url地址
- 实现远程网络通信
- 实现负载均衡
- 实现集群容错
客户端无外乎就这几件事,但是总的来说,客户端代码比服务端复杂的多,所以分了上下两章,接下来我们用源码分析来论证我们的猜想
服务的消费
消费端的代码解析是从下面这段代码开始的
注解的方式的初始化入口是
ReferenceAnnotationBeanPostProcessor->ReferenceBeanInvocationHandler.init->ReferenceConfig.get() 获得一个远程代理类
ReferenceConfig.get
public synchronized T get() {
checkAndUpdateSubConfigs();//检查和修改配置
if (destroyed) {
throw new IllegalStateException("The invoker of ReferenceConfig(" + url + ") has already destroyed!");
}
if (ref == null) {//如果当前接口的远程代理引用为空,则进行初始化
init();
}
return ref;
}
init
初始化的过程,和服务发布的过程类似,会有特别多的判断以及参数的组装. 我们只需要关注createProxy,创建代理类的方法。
private void init() {
//省略...
ref = createProxy(map);
//省略...
}
createProxy
private T createProxy(Map map) {
if (shouldJvmRefer(map)) {//判断是否是在同一个jvm进程中调用
URL url = new URL(LOCAL_PROTOCOL, LOCALHOST_VALUE, 0, interfaceClass.getName()).addParameters(map);
invoker = REF_PROTOCOL.refer(interfaceClass, url);
if (logger.isInfoEnabled()) {
logger.info("Using injvm service " + interfaceClass.getName());
}
} else {
urls.clear(); // reference retry init will add url to urls, lead to OOM
//url 如果不为空,说明是点对点通信
if (url != null && url.length() > 0) { // user specified URL, could be peer-to-peer address, or register center's address.
String[] us = SEMICOLON_SPLIT_PATTERN.split(url);
if (us != null && us.length > 0) {
for (String u : us) {
URL url = URL.valueOf(u);
if (StringUtils.isEmpty(url.getPath())) {
url = url.setPath(interfaceName);
}
// 检测 url 协议是否为 registry,若是,表明用户想使用指定的注册中心
if (REGISTRY_PROTOCOL.equals(url.getProtocol())) {
// 将 map 转换为查询字符串,并作为 refer 参数的值添加到url 中
urls.add(url.addParameterAndEncoded(REFER_KEY, StringUtils.toQueryString(map)));
} else {
// 合并 url,移除服务提供者的一些配置(这些配置来源于用户配置的 url 属性),
// 比如线程池相关配置。并保留服务提供者的部分配置,比如版本,group,时间戳等
// 最后将合并后的配置设置为 url 查询字符串中。
urls.add(ClusterUtils.mergeUrl(url, map));
}
}
}
} else {//从注册中心去获得服务地址
// if protocols not injvm checkRegistry
if (!LOCAL_PROTOCOL.equalsIgnoreCase(getProtocol())){
//校验注册中心的配置以及是否有必要从配置中心组装url
checkRegistry();
//这里的代码实现和服务端类似,也是根据注册中心配置进行解析得到URL
//这里的URL肯定也是: registry://ip:port/org.apache.dubbo.service.RegsitryService
List us = loadRegistries(false);
if (CollectionUtils.isNotEmpty(us)) {
for (URL u : us) {
URL monitorUrl = loadMonitor(u);
if (monitorUrl != null) {
map.put(MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
}
urls.add(u.addParameterAndEncoded(REFER_KEY, StringUtils.toQueryString(map)));
}
}
//如果没有配置注册中心,则报错
if (urls.isEmpty()) {
throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config to your spring config.");
}
}
}
//如果值配置了一个注册中心或者一个服务提供者,直接使用refprotocol.refer
if (urls.size() == 1) {
//构建一个invoker(Protocol)
//Protocol$Adaptive -> getExtension("registry")->Qos(listener(filter(RegisterProtol
//RegisterProtocol.refer()
invoker = REF_PROTOCOL.refer(interfaceClass, urls.get(0));
} else {
List> invokers = new ArrayList>();
URL registryURL = null;
for (URL url : urls) {
//遍历urls生成多个invoker
invokers.add(REF_PROTOCOL.refer(interfaceClass, url));
if (REGISTRY_PROTOCOL.equals(url.getProtocol())) {
registryURL = url; // use last registry url
}
}
if (registryURL != null) { //如果registryUrl不为空,构建静态directory
// use RegistryAwareCluster only when register's CLUSTER is available
// 使用RegistryAwareCluster
URL u = registryURL.addParameter(CLUSTER_KEY, RegistryAwareCluster.NAME);
// The invoker wrap relation would be: RegistryAwareClusterInvoker(StaticDirectory) -> FailoverClusterInvoker(RegistryDirectory, will execute route) -> Invoker
// 通过Cluster将多个invoker合并 RegistryAwareClusterInvoker(StaticDirectory) -> FailoverClusterInvoker(RegistryDirectory, will execute route) -> Invoker
invoker = CLUSTER.join(new StaticDirectory(u, invokers));
} else { // not a registry url, must be direct invoke.
invoker = CLUSTER.join(new StaticDirectory(invokers));
}
}
}
//检查invoker的有效性
if (shouldCheck() && !invoker.isAvailable()) {
throw new IllegalStateException("Failed to check the status of the service " + interfaceName + ". No provider available for the service " + (group == null ? "" : group + "/") + interfaceName + (version == null ? "" : ":" + version) + " from the url " + invoker.getUrl() + " to the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion());
}
if (logger.isInfoEnabled()) {
logger.info("Refer dubbo service " + interfaceClass.getName() + " from url " + invoker.getUrl());
}
/**
* @since 2.7.0
* ServiceData Store
*/
MetadataReportService metadataReportService = null;
if ((metadataReportService = getMetadataReportService()) != null) {
URL consumerURL = new URL(CONSUMER_PROTOCOL, map.remove(REGISTER_IP_KEY), 0, map.get(INTERFACE_KEY), map);
metadataReportService.publishConsumer(consumerURL);
}
// create service proxy
//调用ProxyFactory 生成代理类,最终用代理类进行客户端调用
return (T) PROXY_FACTORY.getProxy(invoker);
}
代码比较长,但是逻辑相对比较清晰
- 判断是否为本地调用,如果是则使用injvm协议进行调用
- 判断是否为点对点调用,如果是则把url保存到urls集合中,如果url为1,进入步骤4,如果urls>1,则执行5
- 如果是配置了注册中心,遍历注册中心,把url添加到urls集合,url为1,进入步骤4,如果urls>1,则执行5
- 直连构建invoker
- 构建invokers集合,通过cluster合并多个invoker
- 最后调用 ProxyFactory 生成代理类
protocol.refer
这里通过指定的协议来调用refer生成一个invoker对象,invoker前面讲过,它是一个代理对象。那么在当前的消费端而言,invoker主要用于执行远程调用。
这个protocol,又是一个自适应扩展点,它得到的是一个Protocol$Adaptive。
Protocol refprotocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension()
这段代码中,根据当前的协议url,得到一个指定的扩展点,传递进来的参数中,协议地址为
registry://,所以,我们可以直接定位到RegistryProtocol.refer代码
Protocol$Adaptive中的refer方法
根据当前的协议扩展名registry, 获得一个被包装过的RegistryProtocol
public org.apache.dubbo.rpc.Invoker refer(java.lang.Class arg0,
org.apache.dubbo.common.URL arg1) throws org.apache.dubbo.rpc.RpcException {
if (arg1 == null) throw new IllegalArgumentException("url == null");
org.apache.dubbo.common.URL url = arg1;
String extName = (url.getProtocol() == null ? "dubbo" : url.getProtocol());
if (extName == null) throw new IllegalStateException("Failed to getextension(org.apache.dubbo.rpc.Protocol)name from url(" + url.toString() + ")use keys([protocol]) ");
org.apache.dubbo.rpc.Protocol extension =
(org.apache.dubbo.rpc.Protocol) ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
return extension.refer(arg0, arg1);
}
RegistryProtocol.refer
这里面的代码逻辑比较简单
1.组装注册中心协议的url
2.判断是否配置legroup,如果有,则cluster=getMergeableCluster(),构建invoker
3.doRefer构建invoker
@Override
@SuppressWarnings("unchecked")
public Invoker refer(Class type, URL url) throws RpcException {
//这段代码也很熟悉,就是根据配置的协议,生成注册中心的url: zookeeper://
url = URLBuilder.from(url)
.setProtocol(url.getParameter(REGISTRY_KEY, DEFAULT_REGISTRY))
.removeParameter(REGISTRY_KEY)
.build();
//ZookeeperRegistery
Registry registry = registryFactory.getRegistry(url);
//register://
if (RegistryService.class.equals(type)) {
return proxyFactory.getInvoker((T) registry, type, url);
}
// 解析group参数,根据group决定cluster的类型
Map qs = StringUtils.parseQueryString(url.getParameterAndDecoded(REFER_KEY));
String group = qs.get(GROUP_KEY);
if (group != null && group.length() > 0) {
if ((COMMA_SPLIT_PATTERN.split(group)).length > 1 || "*".equals(group)) {
return doRefer(getMergeableCluster(), registry, type, url);
}
}
//Cluster->?
return doRefer(cluster, registry, type, url);
}
doRefer
doRefer里面就稍微复杂一些,涉及到比较多的东西,我们先关注主线
1.构建一个RegistryDirectory
2.构建一个consumer://协议的地址注册到注册中心
3.订阅zookeeper中节点的变化
4.调用cluster.join方法
//首先从zk上获得provider url
//建立连接
private Invoker doRefer(Cluster cluster, Registry registry, Class type, URL url) {
/******************************************/
// 构建一个consumer://ip:port 保存到zk provider /configurator/consumer/router
// 1. 连接到注册中心 ->curator
// 2. 从注册中心拿到地址(providerUrl)
// 3. 基于provider地址建立通信
//RegistryDirectory初始化
RegistryDirectory directory = new RegistryDirectory(type, url);
directory.setRegistry(registry); //registry -> 连接zk的api ->获得url地址
directory.setProtocol(protocol); //protocol -> DubboProtocol() -> 建立通信
// all attributes of REFER_KEY
Map parameters = new HashMap(directory.getUrl().getParameters());
//注册consumer://协议的url
URL subscribeUrl = new URL(CONSUMER_PROTOCOL, parameters.remove(REGISTER_IP_KEY), 0, type.getName(), parameters);
if (!ANY_VALUE.equals(url.getServiceInterface()) && url.getParameter(REGISTER_KEY, true)) {
directory.setRegisteredConsumerUrl(getRegisteredConsumerUrl(subscribeUrl, url));
registry.register(directory.getRegisteredConsumerUrl()); //consumer://
}
directory.buildRouterChain(subscribeUrl);
//订阅事件监听
directory.subscribe(subscribeUrl.addParameter(CATEGORY_KEY,
PROVIDERS_CATEGORY + "," + CONFIGURATORS_CATEGORY + "," + ROUTERS_CATEGORY));
/******************************************/
//MockClusterWrapper(FailoverCluster(
//Directory
//构建invoker
Invoker invoker = cluster.join(directory);
ProviderConsumerRegTable.registerConsumer(invoker, url, subscribeUrl, directory);
return invoker;
}
Cluster是什么
我们只关注一下Invoker这个代理类的创建过程,其他的暂且不关心
Invoker invoker=cluster.join(directory)
cluster其实是在RegistryProtocol中通过set方法完成依赖注入的,并且,它还是一个被包装的
public void setCluster(Cluster cluster) {
this.cluster = cluster;
}
Cluster扩展点的定义, 由于它是一个自适应扩展点,那么会动态生成一个Cluster$Adaptive的动态代理类
@SPI(FailoverCluster.NAME)
public interface Cluster {
@Adaptive
Invoker join(Directory directory) throws RpcException;
}
Cluster$Adaptive
在动态适配的类中会基于extName,选择一个合适的扩展点进行适配,由于默认情况下cluster:failover,所以getExtension("failover")理论上应该返回FailOverCluster。但实际上,这里做了包装MockClusterWrapper(FailOverCluster)
public class Cluster$Adaptive implements org.apache.dubbo.rpc.cluster.Cluster {
public org.apache.dubbo.rpc.Invokerjoin(
org.apache.dubbo.rpc.cluster.Directory arg0)throwsorg.apache.dubbo.rpc.RpcException
{
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.cluster.Directory argument ==null ");
if (arg0.getUrl() == null)
throw new IllegalArgumentException("org.apache.dubbo.rpc.cluster.Directory argumentgetUrl() == null");
org.apache.dubbo.common.URL url = arg0.getUrl();
String extName = url.getParameter("cluster", "failover");
if (extName == null)
throw new IllegalStateException("Failed to getextension(org.apache.dubbo.rpc.cluster.Cluster)name from url(" + url.toString() + ") use keys([cluster])");
org.apache.dubbo.rpc.cluster.Cluster extension = (org.apache.dubbo.rpc.cluster.Cluster) ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.cluster.Cluster.class).getExtension(extName);
return extension.join(arg0);
}
}
cluster.join
所以再回到doRefer方法,下面这段代码, 实际是调用MockClusterWrapper(FailOverCluster.join)
Invoker invoker = cluster.join(directory);
所以这里返回的invoker,应该是MockClusterWrapper(FailOverCluster(directory))
接着回到ReferenceConfig.createProxy方法中的最后一行
return (T) PROXY_FACTORY.getProxy(invoker);
proxyFactory.getProxy
拿到invoker之后,会调用获得一个动态代理类
而这里的proxyFactory又是一个自适应扩展点,所以会进入下面的方法
JavassistProxyFactory.getProxy
通过这个方法生成了一个动态代理类,并且对invoker再做了一层处理,InvokerInvocationHandler。
意味着后续发起服务调用的时候,会由InvokerInvocationHandler来进行处理。
public T getProxy(Invoker invoker, Class>[] interfaces) {
return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker));
}
proxy.getProxy
在proxy.getProxy这个方法中会生成一个动态代理类,通过debug的形式可以看到动态代理类的原貌,在getProxy这个方法位置加一个断点
proxy = (Proxy) pc.newInstance();
然后在debug窗口,找到ccp这个变量 -> mMethods。
调整一下格式
public java.lang.String sayHello(java.lang.String arg0){
Object[] args = new Object[1];
args[0] = ($w)$1;
Object ret = handler.invoke(this, methods[0], args);
return (java.lang.String)ret;
}
从这个sayHello方法可以看出,我们通过@Reference注入的一个对象实例本质上就是一个动态代理类,通过调用这个类中的方法,会触发handler.invoke(), 而这个handler就是InvokerInvocationHandler
网络连接的建立
前面分析的逻辑中,只讲到了动态代理类的生成,那么目标服务地址信息以及网络通信的建立在哪里实现的呢?我们继续回到RegistryProtocol.refer这个方法中
这里我们暂且关注directory.subscribe这个方法,它是实现服务目标服务订阅的
private Invoker doRefer(Cluster cluster, Registry registry, Class type, URL url) {
//省略代码
//订阅事件监听
directory.subscribe(subscribeUrl.addParameter(CATEGORY_KEY,
PROVIDERS_CATEGORY + "," + CONFIGURATORS_CATEGORY + "," + ROUTERS_CATEGORY));
//省略代码
}
RegistryDirectory.subscribe
订阅注册中心指定节点的变化,如果发生变化,则通知到RegistryDirectory。Directory其实和服务的注册以及服务的发现有非常大的关联.
public void subscribe(URL url) {
setConsumerUrl(url);//设置consumerUrl
//把当前RegistryDirectory作为listener,去监听zk上节点的变化
CONSUMER_CONFIGURATION_LISTENER.addNotifyListener(this);
serviceConfigurationListener = new ReferenceConfigurationListener(this, url);
//ZookeeperRegistry ; listener: this ->RegistryDirectory
registry.subscribe(url, this);//订阅 -> 这里的registry是zookeeperRegsitry
}
这里的registry 是ZookeeperRegistry ,会去监听并获取路径下面的节点。监听的路径是:
/dubbo/org.apache.dubbo.demo.DemoService/providers、
/dubbo/org.apache.dubbo.demo.DemoService/configurators、
/dubbo/org.apache.dubbo.demo.DemoService/routers
节点下面的子节点变动
FailbackRegistry.subscribe
listener为RegistryDirectory,后续要用到
移除失效的listener,调用doSubscribe进行订阅
public void subscribe(URL url, NotifyListener listener) {
super.subscribe(url, listener);
removeFailedSubscribed(url, listener);
try {
// Sending a subscription request to the server side
doSubscribe(url, listener);
ZookeeperRegistry.doSubscribe
这个方法是订阅,逻辑实现比较多,可以分两段来看,这里的实现把所有Service层发起的订阅以及指定的Service层发起的订阅分开处理。所有Service层类似于监控中心发起的订阅。指定的Service层发起的订阅可以看作是服务消费者的订阅。我们只需要关心指定service层发起的订阅即可
@Override
public void doSubscribe(final URL url, final NotifyListener listener) {
try {
if (ANY_VALUE.equals(url.getServiceInterface())) {
//省略部分代码
} else {
List urls = new ArrayList<>();
//configurator/ consumer/ router
for (String path : toCategoriesPath(url)) {
ConcurrentMap listeners = zkListeners.get(url);
// 如果之前该路径没有添加过listener,则创建一个map来放置listener
if (listeners == null) {
zkListeners.putIfAbsent(url, new ConcurrentHashMap<>());
listeners = zkListeners.get(url);
}
ChildListener zkListener = listeners.get(listener);
if (zkListener == null) {
// 如果没有添加过对于子节点的listener,则创建,通知服务变化 回调NotifyListener
listeners.putIfAbsent(listener, (parentPath, currentChilds) -> ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds)));
zkListener = listeners.get(listener);
}
zkClient.create(path, false);
//对子节点进行监听 ->children : providerUrl
//添加path节点的当前节点及子节点监听,并且获取子节点信息
//也就是dubbo://ip:port/...
List children = zkClient.addChildListener(path, zkListener);
if (children != null) {
urls.addAll(toUrlsWithEmpty(url, path, children));
}
}
//调用notify进行通知,对已经可用的列表进行通知
notify(url, listener, urls);
}
} catch (Throwable e) {
throw new RpcException("Failed to subscribe " + url + " to zookeeper " + getUrl() + ", cause: " + e.getMessage(), e);
}
}
FailbackRegistry.notify
FailbackRegistry.notify 调用FailbackRegistry.notify, 对参数进行判断。 然后调用AbstractRegistry.notify方法
protected void notify(URL url, NotifyListener listener, List urls) {
if (url == null) {
throw new IllegalArgumentException("notify url == null");
}
if (listener == null) {
throw new IllegalArgumentException("notify listener == null");
}
try {
doNotify(url, listener, urls);
} catch (Exception t) {
// Record a failed registration request to a failed list, retry regularly
addFailedNotified(url, listener, urls);
logger.error("Failed to notify for subscribe " + url + ", waiting for retry, cause: " + t.getMessage(), t);
}
}
AbstractRegistry.notify
这里面会针对每一个category,调用listener.notify进行通知,然后更新本地的缓存文件
protected void notify(List urls) {
if (CollectionUtils.isEmpty(urls)) {
return;
}
for (Map.Entry> entry : getSubscribed().entrySet()) {
URL url = entry.getKey();
if (!UrlUtils.isMatch(url, urls.get(0))) {
continue;
}
Set listeners = entry.getValue();
if (listeners != null) {
for (NotifyListener listener : listeners) {
try {
notify(url, listener, filterEmpty(url, urls));
} catch (Throwable t) {
logger.error("Failed to notify registry event, urls: " + urls + ", cause: " + t.getMessage(), t);
}
}
}
}
}
消费端的listener是最开始传递过来的RegistryDirectory,所以这里会触发RegistryDirectory.notify
RegistryDirectory.notify
Invoker的网络连接以及后续的配置变更,都会调用这个notify方法
urls: zk的path数据,这里表示的是dubbo://
public synchronized void notify(List urls) {
//对url列表进行校验、过滤,然后分成 config、router、provider 3个分组map
Map> categoryUrls = urls.stream()
.filter(Objects::nonNull)
.filter(this::isValidCategory)
.filter(this::isNotCompatibleFor26x)
.collect(Collectors.groupingBy(url -> {
if (UrlUtils.isConfigurator(url)) {
return CONFIGURATORS_CATEGORY;
} else if (UrlUtils.isRoute(url)) {
return ROUTERS_CATEGORY;
} else if (UrlUtils.isProvider(url)) {
return PROVIDERS_CATEGORY;
}
return "";
}));
List configuratorURLs = categoryUrls.getOrDefault(CONFIGURATORS_CATEGORY, Collections.emptyList());
this.configurators = Configurator.toConfigurators(configuratorURLs).orElse(this.configurators);
// 如果router 路由节点有变化,则从新将router 下的数据生成router
List routerURLs = categoryUrls.getOrDefault(ROUTERS_CATEGORY, Collections.emptyList());
toRouters(routerURLs).ifPresent(this::addRouters);
// 获得provider URL,然后调用refreshOverrideAndInvoker进行刷新
List providerURLs = categoryUrls.getOrDefault(PROVIDERS_CATEGORY, Collections.emptyList());
//刷新或者覆盖invoker
//providerURLs: dubboo://ip:port
refreshOverrideAndInvoker(providerURLs);
}
refreshOverrideAndInvoker
1.逐个调用注册中心里面的配置,覆盖原来的url,组成最新的url 放入overrideDirectoryUrl 存储
2.根据 provider urls,重新刷新Invoker
private void refreshOverrideAndInvoker(List urls) {
// mock zookeeper://xxx?mock=return null
overrideDirectoryUrl();
refreshInvoker(urls);
}
refreshInvoker
private void refreshInvoker(List invokerUrls) {
Assert.notNull(invokerUrls, "invokerUrls should not be null");
if (invokerUrls.size() == 1
&& invokerUrls.get(0) != null
&& EMPTY_PROTOCOL.equals(invokerUrls.get(0).getProtocol())) {
//如果是空协议,则直接返回不允许访问
this.forbidden = true; // Forbid to access
this.invokers = Collections.emptyList();
routerChain.setInvokers(this.invokers);
destroyAllInvokers(); // Close all invokers
} else {
this.forbidden = false; // Allow to access
Map> oldUrlInvokerMap = this.urlInvokerMap; // local reference
if (invokerUrls == Collections.emptyList()) {
invokerUrls = new ArrayList<>();
}
if (invokerUrls.isEmpty() && this.cachedInvokerUrls != null) {
invokerUrls.addAll(this.cachedInvokerUrls);
} else {
this.cachedInvokerUrls = new HashSet<>();
this.cachedInvokerUrls.addAll(invokerUrls);//Cached invoker urls, convenient for comparison
}
if (invokerUrls.isEmpty()) {//如果url为空,则直接返回
return;
}
//toInvokers ,把invokerUrls 转化为invoker->
//根据provider url,生成新的invoker
Map> newUrlInvokerMap = toInvokers(invokerUrls);// Translate url list to Invoker map
/**
* If the calculation is wrong, it is not processed.
*
* 1. The protocol configured by the client is inconsistent with the protocol of the server.
* eg: consumer protocol = dubbo, provider only has other protocol services(rest).
* 2. The registration center is not robust and pushes illegal specification data.
*
*/
if (CollectionUtils.isEmptyMap(newUrlInvokerMap)) {
logger.error(new IllegalStateException("urls to invokers error .invokerUrls.size :" + invokerUrls.size() + ", invoker.size :0. urls :" + invokerUrls
.toString()));
return;
}
//转化为list
List> newInvokers = Collections.unmodifiableList(new ArrayList<>(newUrlInvokerMap.values()));
// pre-route and build cache, notice that route cache should build on original Invoker list.
// toMergeMethodInvokerMap() will wrap some invokers having different groups, those wrapped invokers not should be routed.
routerChain.setInvokers(newInvokers);
//如果服务配置了分组,则把分组下的provider包装成StaticDirectory,组成一个invoker
//实际上就是按照group进行合并
this.invokers = multiGroup ? toMergeInvokerList(newInvokers) : newInvokers;
this.urlInvokerMap = newUrlInvokerMap;
try {
//旧的url 是否在新map里面存在,不存在,就是销毁url对应的Invoker
destroyUnusedInvokers(oldUrlInvokerMap, newUrlInvokerMap); // Close the unused Invoker
} catch (Exception e) {
logger.warn("destroyUnusedInvokers error. ", e);
}
}
}
toInvokers
这个方法中有比较长的判断和处理逻辑,我们只需要关心invoker是什么时候初始化的就行。
这里用到了protocol.refer来构建了一个invoker
invoker = new InvokerDelegate<>(protocol.refer(serviceType, url), url, providerUrl);
构建完成之后,会保存在Map
private Map> toInvokers(List urls) {
Map> newUrlInvokerMap = new HashMap<>();
if (urls == null || urls.isEmpty()) {
return newUrlInvokerMap;
}
Set keys = new HashSet<>();
String queryProtocols = this.queryMap.get(PROTOCOL_KEY);
for (URL providerUrl : urls) {
// If protocol is configured at the reference side, only the matching protocol is selected
if (queryProtocols != null && queryProtocols.length() > 0) {
boolean accept = false;
String[] acceptProtocols = queryProtocols.split(",");
for (String acceptProtocol : acceptProtocols) {
if (providerUrl.getProtocol().equals(acceptProtocol)) {
accept = true;
break;
}
}
if (!accept) {
continue;
}
}
if (EMPTY_PROTOCOL.equals(providerUrl.getProtocol())) {
continue;
}
if (!ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(providerUrl.getProtocol())) {
logger.error(new IllegalStateException("Unsupported protocol " + providerUrl.getProtocol() +
" in notified url: " + providerUrl + " from registry " + getUrl().getAddress() +
" to consumer " + NetUtils.getLocalHost() + ", supported protocol: " +
ExtensionLoader.getExtensionLoader(Protocol.class).getSupportedExtensions()));
continue;
}
URL url = mergeUrl(providerUrl);
String key = url.toFullString(); // The parameter urls are sorted
if (keys.contains(key)) { // Repeated url
continue;
}
keys.add(key);
// Cache key is url that does not merge with consumer side parameters, regardless of how the consumer combines parameters, if the server url changes, then refer again
Map> localUrlInvokerMap = this.urlInvokerMap; // local reference
Invoker invoker = localUrlInvokerMap == null ? null : localUrlInvokerMap.get(key);
if (invoker == null) { // Not in the cache, refer again
try {
boolean enabled = true;
if (url.hasParameter(DISABLED_KEY)) {
enabled = !url.getParameter(DISABLED_KEY, false);
} else {
enabled = url.getParameter(ENABLED_KEY, true);
}
if (enabled) {
invoker = new InvokerDelegate<>(protocol.refer(serviceType, url), url, providerUrl);
}
} catch (Throwable t) {
logger.error("Failed to refer invoker for interface:" + serviceType + ",url:(" + url + ")" + t.getMessage(), t);
}
if (invoker != null) { // Put new invoker in cache
newUrlInvokerMap.put(key, invoker);
}
} else {
newUrlInvokerMap.put(key, invoker);
}
}
keys.clear();
return newUrlInvokerMap;
}
protocol.refer
调用指定的协议来进行远程引用。protocol是一个Protocol$Adaptive类
而真正的实现应该是:
ProtocolListenerWrapper(ProtocolFilterWrapper(QosProtocolWrapper(DubboProtocol.refer)
前面的包装过程,在服务发布的时候已经分析过了,我们直接进入DubboProtocol.refer方法
DubboProtocol.refer
1.优化序列化
2.构建DubboInvoker
在构建DubboInvoker时,会构建一个ExchangeClient,通过getClients(url)方法,这里基本可以猜到到是服务的通信建立
@Override
public Invoker refer(Class serviceType, URL url) throws
RpcException {
optimizeSerialization(url);
// create rpc invoker.
DubboInvoker invoker = new DubboInvoker(serviceType, url,
getClients(url), invokers);
invokers.add(invoker);
return invoker;
}
getClients
这里面是获得客户端连接的方法
1.判断是否为共享连接,默认是共享同一个连接进行通信
2.是否配置了多个连接通道 connections,默认只有一个
private ExchangeClient[] getClients(URL url) {
// whether to share connection
boolean useShareConnect = false;
int connections = url.getParameter(CONNECTIONS_KEY, 0);
List shareClients = null;
// if not configured, connection is shared, otherwise, one connection for one service
//如果没有配置连接数,则默认为共享连接
if (connections == 0) {
useShareConnect = true;
/**
* The xml configuration should have a higher priority than properties.
*/
String shareConnectionsStr = url.getParameter(SHARE_CONNECTIONS_KEY, (String) null);
connections = Integer.parseInt(StringUtils.isBlank(shareConnectionsStr) ? ConfigUtils.getProperty(SHARE_CONNECTIONS_KEY,
DEFAULT_SHARE_CONNECTIONS) : shareConnectionsStr);
shareClients = getSharedClient(url, connections);
}
ExchangeClient[] clients = new ExchangeClient[connections];
for (int i = 0; i < clients.length; i++) {
if (useShareConnect) {
clients[i] = shareClients.get(i);
} else {
clients[i] = initClient(url);
}
}
return clients;
}
getSharedClient
获得一个共享连接
private List getSharedClient(URL url, int connectNum) {
String key = url.getAddress();
List clients = referenceClientMap.get(key);
//检查当前的key检查连接是否已经创建过并且可用,如果是,则直接返回并且增加连接的个数的统计
if (checkClientCanUse(clients)) {
batchClientRefIncr(clients);
return clients;
}
//如果连接已经关闭或者连接没有创建过
locks.putIfAbsent(key, new Object());
synchronized (locks.get(key)) {
clients = referenceClientMap.get(key);
// dubbo check
// 在创建连接之前,在做一次检查,防止连接并发创建
if (checkClientCanUse(clients)) {
batchClientRefIncr(clients);
return clients;
}
// connectNum must be greater than or equal to 1
// 连接数必须大于等于1
connectNum = Math.max(connectNum, 1);
// If the clients is empty, then the first initialization is
//如果当前消费者还没有和服务端产生连接,则初始化
if (CollectionUtils.isEmpty(clients)) {
clients = buildReferenceCountExchangeClientList(url, connectNum);
//创建clients之后,保存到map中
referenceClientMap.put(key, clients);
} else {//如果clients不为空,则从clients数组中进行遍历
for (int i = 0; i < clients.size(); i++) {
ReferenceCountExchangeClient referenceCountExchangeClient = clients.get(i);
// If there is a client in the list that is no longer available, create a new one to replace him.
// 如果在集合中存在一个连接但是这个连接处于closed状态,则重新构建一个进行替换
if (referenceCountExchangeClient == null || referenceCountExchangeClient.isClosed()) {
clients.set(i, buildReferenceCountExchangeClient(url));
continue;
}
//增加个数
referenceCountExchangeClient.incrementAndGetCount();
}
}
/**
* I understand that the purpose of the remove operation here is to avoid the expired url key
* always occupying this memory space.
*/
locks.remove(key);
return clients;
}
}
buildReferenceCountExchangeClientList
根据连接数配置,来构建指定个数的链接。默认为1
/**
* Bulk build client
*
* @param url
* @param connectNum
* @return
*/
private List buildReferenceCountExchangeClientList(URL url, int connectNum) {
List clients = new ArrayList<>();
for (int i = 0; i < connectNum; i++) {
clients.add(buildReferenceCountExchangeClient(url));
}
return clients;
}
/**
* Build a single client
*
* @param url
* @return
*/
private ReferenceCountExchangeClient buildReferenceCountExchangeClient(URL url) {
ExchangeClient exchangeClient = initClient(url);
return new ReferenceCountExchangeClient(exchangeClient);
}
initClient
终于进入到初始化客户端连接的方法了,猜测应该是根据url中配置的参数进行远程通信的构建
private ExchangeClient initClient(URL url) {
// client type setting.
// 获得连接类型
String str = url.getParameter(CLIENT_KEY, url.getParameter(SERVER_KEY, DEFAULT_REMOTING_CLIENT));
//添加默认序列化
url = url.addParameter(CODEC_KEY, DubboCodec.NAME);
// enable heartbeat by default
//设置心跳时间
url = url.addParameterIfAbsent(HEARTBEAT_KEY, String.valueOf(DEFAULT_HEARTBEAT));
// BIO is not allowed since it has severe performance issue.
// 判断str是否存在于扩展点中,如果不存在则直接报错
if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
throw new RpcException("Unsupported client type: " + str + "," +
" supported client type is " + StringUtils.join(ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(), " "));
}
ExchangeClient client;
try {
// connection should be lazy
// 是否需要延迟创建连接,注意哦,这里的requestHandler是一个适配器
if (url.getParameter(LAZY_CONNECT_KEY, false)) {
client = new LazyConnectExchangeClient(url, requestHandler);
} else {
client = Exchangers.connect(url, requestHandler);
}
} catch (RemotingException e) {
throw new RpcException("Fail to create remoting client for service(" + url + "): " + e.getMessage(), e);
}
return client;
}
Exchangers.connect
创建一个客户端连接
public static ExchangeClient connect(URL url, ExchangeHandler handler) throws RemotingException {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
if (handler == null) {
throw new IllegalArgumentException("handler == null");
}
url = url.addParameterIfAbsent(Constants.CODEC_KEY, "exchange");
return getExchanger(url).connect(url, handler);
}
HeaderExchange.connect
主要关注transporters.connect
@Override
public ExchangeClient connect(URL url, ExchangeHandler handler) throws RemotingException {
return new HeaderExchangeClient(Transporters.connect(url, new DecodeHandler(new HeaderExchangeHandler(handler))), true);
}
NettyTransport.connect
使用netty构建了一个客户端连接
@Override
public Client connect(URL url, ChannelHandler listener) throws RemotingException {
return new NettyClient(url, listener);
}
总结
我们讲到了RegistryProtocol.refer 过程中有一个关键步骤,即在监听到服务提供者url时触发
RegistryDirectory.notify() 方法。
RegistryDirectory.notify() 方法调用refreshInvoker() 方法将服务提供者urls转换为对应的远程invoker ,最终调用到DubboProtocol.refer() 方法生成对应的DubboInvoker 。
DubboInvoker 的构造方法中有一项入参ExchangeClient[] clients ,即对应网络客户端Client 。DubboInvoker就是通过调用client.request() 方法完成网络通信的请求发送和响应接收功能。
Client 的具体生成过程就是通过DubboProtocol 的initClient(URL url) 方法创建了一个HeaderExchangeClient 。
而具体的invoke调用,和服务端的接收处理,再下一章讲解
——学自咕泡学院