@aloxc
2017-12-05T06:25:25.000000Z
字数 2379
阅读 517
一起学 ignite 源码分析
转载请注明本文作者:aloxc,邮箱leerohwa#gmail.com[#更换为@]
如题,ignite还支持memcache协议,具体就是ignite启动的时候启动了一个绑定到11211--11310这100个端口中的一个的nio服务。
这100个端口是这样选择的,初始是从11211端口起一个nio服务,如果起的这个nio服务没启动成功起来就往之前的端口加1后再次起nio服务,至到成功的起了一个nio服务。
相关代码(org.apache.ignite.internal.processors.rest.protocols.tcp.GridTcpRestProtocol中)如下
public void start(final GridRestProtocolHandler hnd) throws IgniteCheckedException {assert hnd != null;ConnectorConfiguration cfg = ctx.config().getConnectorConfiguration();assert cfg != null;lsnr = new GridTcpRestNioListener(log, this, hnd, ctx);GridNioParser parser = new GridTcpRestParser(false);try {host = resolveRestTcpHost(ctx.config());SSLContext sslCtx = null;if (cfg.isSslEnabled()) {Factory<SSLContext> igniteFactory = ctx.config().getSslContextFactory();Factory<SSLContext> factory = cfg.getSslFactory();// This factory deprecated and will be removed.GridSslContextFactory depFactory = cfg.getSslContextFactory();if (factory == null && depFactory == null && igniteFactory == null)// Thrown SSL exception instead of IgniteCheckedException for writing correct warning message into log.throw new SSLException("SSL is enabled, but SSL context factory is not specified.");if (factory != null)sslCtx = factory.create();else if (depFactory != null)sslCtx = depFactory.createSslContext();elsesslCtx = igniteFactory.create();}int startPort = cfg.getPort();int portRange = cfg.getPortRange();int lastPort = portRange == 0 ? startPort : startPort + portRange - 1;for (int port0 = startPort; port0 <= lastPort; port0++) {if (startTcpServer(host, port0, lsnr, parser, sslCtx, cfg)) {port = port0;if (log.isInfoEnabled())log.info(startInfo());return;}}IgniteUtils.warn(log, "Failed to start TCP binary REST server (possibly all ports in range are in use) " +"[firstPort=" + cfg.getPort() + ", lastPort=" + lastPort + ", host=" + host + ']');}catch (SSLException e) {IgniteUtils.warn(log, "Failed to start " + name() + " protocol on port " + port + ": " + e.getMessage(),"Failed to start " + name() + " protocol on port " + port + ". Check if SSL context factory is " +"properly configured.");}catch (IOException e) {IgniteUtils.warn(log, "Failed to start " + name() + " protocol on port " + port + ": " + e.getMessage(),"Failed to start " + name() + " protocol on port " + port + ". " +"Check restTcpHost configuration property.");}}
如果说我们线上服务器,不希望ignite启动后还兼容memcache命令,可以把这个方法中行lsnr = new GridTcpRestNioListener(log, this, hnd, ctx);后面的代码注释掉。其实本人认为ignite这么做兼容别的命令有点画蛇添足,虽然有很多公司在使用memcache,并且也愿意使用ignite的情况下也不会简单的更换到ignite并且还用memcache命令连接ignite,虽然直接替换下memcache地址,接着使用memcache命令连接ignite这样成本较小。我个人不希望ignite这么做,
