CAT_source_analyze icon indicating copy to clipboard operation
CAT_source_analyze copied to clipboard

Motan之Client端源码分析(1)——Cluster

Open lytofb opened this issue 7 years ago • 0 comments

Motan Cluster

Motan的client主要由Registry,Cluster,Transport,Protocol等几个模块组成

因为其他模块介绍比较多,所以本文暂时只介绍Cluster和Registry相关模块

Client端启动流程基本如下

motan_client_cluster

RefererConfig

RefererConfig类是RefererConfigBean的父类,RefererConfigBean负责实现spring bean的接口。

RefererConfig中比较重要的属性是

List<ClusterSupport<T>> clusterSupports

ClusterSupport是封装了Cluster的类,RefererConfig主要的功能就是根据Config创建Cluster的实例们

其中createClusterSupport就是用来建立实例的方法,在createClusterSupport中调用SimpleConfigHandler的buildClusterSupport创建Cluster。

SimpleConfigHandler

SimpleConfigHandler类主要有两个方法:buildClusterSupport,refer

buildClusterSupport主要负责创建Cluster实例

refer负责根据代理handler组件referer对象

ClusterSupport

ClusterSupport持有属性如下

    private static ConcurrentHashMap<String, Protocol> protocols = new ConcurrentHashMap<String, Protocol>();//协议名称——协议实例
    private Cluster<T> cluster;//集群实例
    private List<URL> registryUrls;//注册url
    private URL url;//接口地址url
    private Class<T> interfaceClass;//接口类
    private Protocol protocol;//协议
    private ConcurrentHashMap<URL, List<Referer<T>>> registryReferers = new ConcurrentHashMap<URL, List<Referer<T>>>();//url与referer的key-value

ClusterSupport的主要方法有init和notify

init主要作用在于根据config对cluster属性中的loadBalance,Hastrategy进行初始化,并且订阅zookeeper中的通知

notify主要用于接收通知

ZookeeperRegistry

ZookeeperRegistry

zookeeperRegistry主要接口来自于基类的FailbackRegistry,分别是register,unregister,subscribe,unsubscribe,discover,客户端主要调用subscribe和unsubscribe用于订阅

AbstractRegistry主要用于暴露register,unregister,subscribe,unsubscribe,discover接口,FailBackRegistry主要用于封装retry重试机制

CommandFailbackRegistry用于区分command和service的操作,ZooKeeperRegistry用于以zookeeper的方式来实现CommandFailbackRegistry虚类

具体的服务发现的实现,由CommandServiceManager实现

CommandServiceManager

主要属性如下

    private URL refUrl;//服务url
    private ConcurrentHashSet<NotifyListener> notifySet;//cluster状态的listener,client集合
    private CommandFailbackRegistry registry;//registry实例
    // service cache
    private Map<String, List<URL>> groupServiceCache;
    // command cache
    private String commandStringCache = "";
    private volatile RpcCommand commandCache;

CommandServiceManager聚合client和zookeeperRegistry以及rpc的路由规则,用于通知client集群变化,维护rpc调用的路由规则,缓存部分service url

public void notifyService(URL serviceUrl, URL registryUrl, List<URL> urls)
public void notifyCommand(URL serviceUrl, String commandString)

主要用于通知service与command(流量路由规则)的变更

private List<URL> discoverOneGroup(URL urlCopy)

用于根据分组发现service

public List<URL> discoverServiceWithCommand(URL serviceUrl, Map<String, Integer> weights, RpcCommand rpcCommand, String localIP)

用于根据command发现service

lytofb avatar Apr 17 '17 01:04 lytofb