4.dubbo源码-发布服务

dubbo服务发布

dubbo服务发布只需在spring.xml中如下配置即可:

export初始化

通过2-dubbo结合spring可知,解析后封装到ServiceBean中;ServiceBean定义如下,继承了dubbo定义的类ServiceConfig,实现了5个spring的接口,为了融入spring容器的启动过程中:

public class ServiceBean extends ServiceConfig implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener, BeanNameAware {
    ... ...
}

ServiceBean实现了ApplicationListener接口,当spring容器触发了ContextRefreshedEvent事件时,就会调用ServiceConfig中的export()方法发布申明的dubbo服务,且在dubbo的info级别日志中有相应的日志:

public void onApplicationEvent(ApplicationEvent event) {
    if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName())) {
        if (isDelay() && ! isExported() && ! isUnexported()) {
            if (logger.isInfoEnabled()) {
                logger.info("The service ready on spring started. service: " + getInterface());
            }
            export();
        }
    }
}

info日志示例:The service ready on spring started. service: com.alibaba.dubbo.demo.DemoService

ServiceConfig.export()

ServiceConfigexport()方法部分源码如下,如果中申明了delay(例如),那么延迟调用doExport()发布这个服务,否则直接调用doExport()发布服务:

public synchronized void export() {
    ... ...
    if (delay != null && delay > 0) {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(delay);
                } catch (Throwable e) {
                }
                doExport();
            }
        });
        thread.setDaemon(true);
        thread.setName("DelayExportServiceThread");
        thread.start();
    } else {
        doExport();
    }
}

ServiceConfig.doExport()的作用:

  1. 检查中是否配置了interface, 如果为空,那么抛出异常:
  2. 检查xml配置中申明的interface的类型是否是java interface类型(interfaceClass.isInterface())
  3. 检查xml配置中interface和ref是否匹配(interfaceClass.isInstance(ref))
  4. application®istry&protocol等有效性检查;
  5. 有效性检查通过后,调用doExportUrls()发布dubbo服务;

ServiceConfig.doExportUrls()

通过调用loadRegistries(true)得到所有registry的url地址,例如在dubbo.properties中通过配置dubbo.registry.address=zookeeper://127.0.0.1:2181;protocols就是将要发布服务的协议集合(dubbo服务可以同时暴露多种协议),可以在dubbo.properties中配置,以dubbo协议为例:

dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

ServiceConfig.doExportUrls()源码如下:

private void doExportUrls() {
    List registryURLs = loadRegistries(true);
    // 一般只配置dubbo协议,那么protocols就是:
    for (ProtocolConfig protocolConfig : protocols) {
        doExportUrlsFor1Protocol(protocolConfig, registryURLs);
    }
}

ServiceConfig.doExportUrlsFor1Protocol()

先把所有相关属性封装到Map中,例如protocol=dubbo,host=10.0.0.1,port=20880,path=com.alibaba.dubbo.demo.TestService等,然后构造dubbo定义的统一数据模型URL:

URL url = new URL(name, host, port, (contextPath == null || contextPath.length() == 0 ? "" : contextPath + "/") + path, map);

得到的url如下所示(这个url非常重要,贯穿整个dubbo服务的发布和调用过程,可以在服务发布后在dubbo-monitor中看到):

ServiceConfig.doExportUrlsFor1Protocol()中根据scope判断服务的发布范围:

  1. 如果配置scope=none, 那么不需要发布这个dubbo服务;
  2. 没有配置scope=noe且配置的scope!=remote, 那么本地暴露 这个dubbo服务;
  3. 没有配置scope=noe且配置的scope!=remote且配置的scope!=local,那么远程暴露这个dubbo服务(例如远程暴露这个服务到zk上,默认情况下scope没有配置,就是在这里发布服务);

实现源码如下:

//配置为none不暴露
if (! Constants.SCOPE_NONE.toString().equalsIgnoreCase(scope)) {

    //配置不是remote的情况下做本地暴露 (配置为remote,则表示只暴露远程服务)
    if (!Constants.SCOPE_REMOTE.toString().equalsIgnoreCase(scope)) {
        exportLocal(url);
    }
    //如果配置不是local则暴露为远程服务.(配置为local,则表示只暴露远程服务)
    if (! Constants.SCOPE_LOCAL.toString().equalsIgnoreCase(scope) ){
        if (logger.isInfoEnabled()) {
            logger.info("Export dubbo service " + interfaceClass.getName() + " to url " + url);
        }
        // 如果注册url地址存在,例如申明了注册的zk地址
        if (registryURLs != null && registryURLs.size() > 0
                && url.getParameter("register", true)) {
            // 注册的zk地址可能是集群,那么需要遍历这些地址一一进行注册
            for (URL registryURL : registryURLs) {
                url = url.addParameterIfAbsent("dynamic", registryURL.getParameter("dynamic"));
                // 如果申明了dubbo-monitor,那么再url地址上append类似monitor=monitor全地址
                URL monitorUrl = loadMonitor(registryURL);
                if (monitorUrl != null) {
                    url = url.addParameterAndEncoded(Constants.MONITOR_KEY, monitorUrl.toFullString());
                }
                if (logger.isInfoEnabled()) {
                    logger.info("Register dubbo service " + interfaceClass.getName() + " url " + url + " to registry " + registryURL);
                }
                Invoker invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, registryURL.addParameterAndEncoded(Constants.EXPORT_KEY, url.toFullString()));
                // 默认都是dubbo协议,所以调用DubboProtol.export(Invoker)
                Exporter exporter = protocol.export(invoker);
                exporters.add(exporter);
            }
        } else {
            Invoker invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, url);
            Exporter exporter = protocol.export(invoker);
            exporters.add(exporter);
        }
    }
}

由这段源码可知,如果发布dubbo服务到zookeeper上,invoker.getUrl()的值为:
registry://10.0.53.87:2188/com.alibaba.dubbo.registry.RegistryService?application=dubbo-test&dubbo=2.0.0&export=dubbo%3A%2F%2F10.52.16.218%3A20886%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddubbo-test%26dubbo%3D2.0.0%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dafei%26pid%3D2380%26side%3Dprovider%26timestamp%3D1509953019382&owner=afei&pid=2380®istry=zookeeper×tamp=1509953019349

且会有两行info级别的日志;

Export dubbo service com.alibaba.dubbo.demo.DemoService to url ... ...
Register dubbo service com.alibaba.dubbo.demo.DemoService url ... ...

Protocol.export()

com.alibaba.dubbo.rpc.Protocol中暴露服务接口申明:

/**
 * 暴露远程服务:
* 1. 协议在接收请求时,应记录请求来源方地址信息:RpcContext.getContext().setRemoteAddress();
* 2. export()必须是幂等的,也就是暴露同一个URL的Invoker两次,和暴露一次没有区别。
* 3. export()传入的Invoker由框架实现并传入,协议不需要关心。
* * @param 服务的类型 * @param invoker 服务的执行体 * @return exporter 暴露服务的引用,用于取消暴露 * @throws RpcException 当暴露服务出错时抛出,比如端口已占用 */ @Adaptive Exporter export(Invoker invoker) throws RpcException;

RegistryProtocol.export()

源码如下:

public  Exporter export(final Invoker originInvoker) throws RpcException {
    //export invoker
    final ExporterChangeableWrapper exporter = doLocalExport(originInvoker);
    //registry provider,根据发布的服务originInvoker得到Registry实例,由于一般都使用zookeeper为注册中心,所以这里得到的是ZookeeperRegistry;
    final Registry registry = getRegistry(originInvoker);
    final URL registedProviderUrl = getRegistedProviderUrl(originInvoker);
    // 所以这里调用ZookeeperRegistry.register(URL)把需要发布的服务注册到zookeeper中
    registry.register(registedProviderUrl);
    // 订阅override数据
    // FIXME 提供者订阅时,会影响同一JVM即暴露服务,又引用同一服务的的场景,因为subscribed以服务名为缓存的key,导致订阅信息覆盖。
    final URL overrideSubscribeUrl = getSubscribedOverrideUrl(registedProviderUrl);
    final OverrideListener overrideSubscribeListener = new OverrideListener(overrideSubscribeUrl);
    overrideListeners.put(overrideSubscribeUrl, overrideSubscribeListener);
    registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener);
    //保证每次export都返回一个新的exporter实例
    return new Exporter() {
        public Invoker getInvoker() {
            return exporter.getInvoker();
        }
        public void unexport() {
            try {
                exporter.unexport();
            } catch (Throwable t) {
                logger.warn(t.getMessage(), t);
            }
            try {
                registry.unregister(registedProviderUrl);
            } catch (Throwable t) {
                logger.warn(t.getMessage(), t);
            }
            try {
                overrideListeners.remove(overrideSubscribeUrl);
                registry.unsubscribe(overrideSubscribeUrl, overrideSubscribeListener);
            } catch (Throwable t) {
                logger.warn(t.getMessage(), t);
            }
        }
    };
}

核心调用registry.register(registedProviderUrl)

  1. 调用AbstractRegistry.register(URL),把这次需要注册的URL加到Set registered中,即本地缓存新的注册URL;
  2. 在ZookeeperRegistry.doRegister(URL)调用AbstractZookeeperClient.create(),toUrlPath将URL形式的地址转换成zookeeper路径,最终在AbstractZookeeperClient中把需要发布的服务的URL保存到zookeeper中;(依赖第三方jar包:org.I0Itec.zkclient)
  3. ZookeeperRegistry.doRegister(url)注册服务如果失败:
  • 如果开启了启动检查check=true,那么直接抛出异常;
  • 如果没有开启启动检查,那么将失败的注册请求记录到失败列表,定时重试;

核心调用registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener):

  1. 对发布的dubbo服务的这个url进行监听, 当服务变化有时通知重新暴露服务, 以zookeeper为例,暴露服务会在zookeeper生成一个节点,当节点发生变化的时候会触发overrideSubscribeListener的notify方法重新暴露服务;

重试机制

注册服务失败后,会将url加入重试url集合中,failedRegistered.add(url); 重试任务在FailbackRegistry中实现:

public FailbackRegistry(URL url) {
    super(url);
    int retryPeriod = url.getParameter(Constants.REGISTRY_RETRY_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RETRY_PERIOD);
    // retryExecutor是一个单独的线程池Executors.newScheduledThreadPool(1, new NamedThreadFactory("DubboRegistryFailedRetryTimer", true)); 默认重试周期是5s;
    this.retryFuture = retryExecutor.scheduleWithFixedDelay(new Runnable() {
        public void run() {
            // 检测并连接注册中心
            try {
                retry();
            } catch (Throwable t) { // 防御性容错
                logger.error("Unexpected error occur at failed retry, cause: " + t.getMessage(), t);
            }
        }
    }, retryPeriod, retryPeriod, TimeUnit.MILLISECONDS);
}

监听机制

  1. 订阅并设置监听registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener);
    --> FailbackRegistry.subscribe(URL url, NotifyListener listener)
    --> ZookeeperRegistry.doSubscribe(final URL url, final NotifyListener listener),部分实现源码如下:
ChildListener zkListener = listeners.get(listener);
if (zkListener == null) {
    listeners.putIfAbsent(listener, new ChildListener() {
        public void childChanged(String parentPath, List currentChilds) {
            ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds));
        }
    });
    zkListener = listeners.get(listener);
}
zkClient.create(path, false);
// 在准备监听的path上添加ChildListener
List children = zkClient.addChildListener(path, zkListener);
  1. 服务有变化时notify:
    FailbackRegistry.notify(URL url, NotifyListener listener, List urls)
    以path:/dubbo/com.alibaba.dubbo.demo.TestService/providers为例,Consumer会监听这个zk路径;
  • 假设在consumer启动时只有1个provider:dubbo://10.52.17.98:20888... ;当再启动一个provider:dubbo://10.52.17.98:20886后,path路径/dubbo/com.alibaba.dubbo.demo.TestService/providers就会变化,结果就触发notify(URL url, NotifyListener listener, List urls),此时List urls中有两个provider,即dubbo://10.52.17.98:20886...和dubbo://10.52.17.98:20888...
  • 或者在consumer启动时有2个provider:dubbo://10.52.17.98:20886... 和 dubbo://10.52.17.98:20888... ;当关闭一个provider:dubbo://10.52.17.98:20886后,path路径/dubbo/com.alibaba.dubbo.demo.TestService/providers也会变化,结果就触发notify(URL url, NotifyListener listener, List urls),此时List urls中只有1个provider,即dubbo://10.52.17.98:20888...;
    --> AbstractRegistry.notify(URL url, NotifyListener listener, List urls)
    --> RegistryDirectory.notify(List urls)
    --> RegistryDirectory.refreshInvoker(List invokerUrls),这里调用toMethodInvokers(Map> invokersMap)的实现比较重要,将invokers列表转成与方法的映射关系,且每个方法对应的List需要通过Collections.sort(methodInvokers, InvokerComparator.getComparator());排序,排序后,还要将其转为unmodifiable的map:
for (String method : new HashSet(newMethodInvokerMap.keySet())) {
    List> methodInvokers = newMethodInvokerMap.get(method);
    Collections.sort(methodInvokers, InvokerComparator.getComparator());
    newMethodInvokerMap.put(method, Collections.unmodifiableList(methodInvokers));
}
return Collections.unmodifiableMap(newMethodInvokerMap);

其中InvokerComparator 的定义如下,即直接根据url进行比较排序:

private static class InvokerComparator implements Comparator> {   
    private static final InvokerComparator comparator = new InvokerComparator();  
    public static InvokerComparator getComparator() {
        return comparator;
    }   
    private InvokerComparator() {}
    public int compare(Invoker o1, Invoker o2) {
        return o1.getUrl().toString().compareTo(o2.getUrl().toString());
    }
}

最后刷新本地缓存的方法和List关系:
this.methodInvokerMap = multiGroup ? toMergeMethodInvokerMap(newMethodInvokerMap) : newMethodInvokerMap;

DubboProtocol.export()

dubbo协议发布服务会调用DubboProtocol.export(),

  1. 从Invoker中获取URL:
    URL url = invoker.getUrl();
  2. 根据URL得到key, 由暴露的服务接口+端口组成,例如com.alibaba.dubbo.demo.DemoService:20886
    String key = serviceKey(url);
  3. 构造DubboExporter存到Map中local cache化:
    DubboExporter exporter = new DubboExporter(invoker, key, exporterMap);
    exporterMap.put(key, exporter);
  4. 调用DubboProtocol.openServer()开启netty(默认)服务保持通信,并设置requestHandler处理consumer对provider的调用请求;

DubboProtocol.openServer():
key的值就是IP:Port,例如10.52.17.167:20886,根据key从serverMap中如果取不到ExchangeServer,表示还没绑定服务端口,需要调用createServer(url)-->Exchangers.bind(url, requestHandler)-->Transporters.getTransporter().bind(url, handler)(dubbo支持mina,netty,grizzly,默认实现是netty) --> NettyTransporter.bind(URL, ChannelHandler) --> NettyServer.open();

NettyServer.open()源码如下:

@Override
rotected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", true));
    ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true));
    ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS));
    bootstrap = new ServerBootstrap(channelFactory);
    
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    channels = nettyHandler.getChannels();
    // https://issues.jboss.org/browse/NETTY-365
    // https://issues.jboss.org/browse/NETTY-379
    // final Timer timer = new HashedWheelTimer(new NamedThreadFactory("NettyIdleTimer", true));
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec() ,getUrl(), NettyServer.this);
            ChannelPipeline pipeline = Channels.pipeline();
            /*int idleTimeout = getIdleTimeout();
            if (idleTimeout > 10000) {
                pipeline.addLast("timer", new IdleStateHandler(timer, idleTimeout / 1000, 0, 0));
            }*/
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
    // bind
    channel = bootstrap.bind(getBindAddress());
}

开启Netty服务几个重要的地方

  1. 构造ChannelPipeline时指定了编码&解码,其中编码为NettyCodecAdapter.getEncoder(),解码为NettyCodecAdapter.getDncoder();
  2. 指定了handler为final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);处理请求;

附dubbo官方给出的暴露服务时序图:
https://dubbo.gitbooks.io/dubbo-dev-book/design.html

你可能感兴趣的:(4.dubbo源码-发布服务)