private ExchangeServer getServer(URL url) { // 指定自己的exchanger url = url.addParameterIfAbsent(Constants.EXCHANGER_KEY, HeaderExchanger2.NAME); // server type setting url = url.addParameterIfAbsent(Constants.SERVER_KEY, NettyTransporter2.NAME); // check server type String str = url.getParameter(Constants.SERVER_KEY, NettyTransporter2.NAME); if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) { throw new RpcException("Unsupported server type: " + str + ", url: " + url); } ExchangeServer server; try { server = Exchangers.bind(url, requestHandler); } catch (RemotingException e) { throw new RpcException("Fail to start server(url: " + url + ") " + e.getMessage(), e); } str = url.getParameter(Constants.CLIENT_KEY); if (str != null && str.length() > 0) { Set<String> supportedTypes = ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(); if (!supportedTypes.contains(str)) { throw new RpcException("Unsupported client type: " + str); } } return server; }
/** 创建新连接. */ private ExchangeClient initClient(URL url) { // 指定自己的exchanger url = url.addParameterIfAbsent(Constants.EXCHANGER_KEY, HeaderExchanger2.NAME); // client type setting. url = url.addParameterIfAbsent( Constants.CLIENT_KEY, url.getParameter(Constants.SERVER_KEY, NettyTransporter2.NAME)); // check client type String str = url.getParameter( Constants.CLIENT_KEY, url.getParameter(Constants.SERVER_KEY, NettyTransporter2.NAME)); // BIO存在严重性能问题,暂时不允许使用 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 { // 设置连接应该是lazy的 if (url.getParameter(Constants.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; }