public abstract class AbstractProtocol implements Protocol, ScopeModelAware { protected final Logger logger = LoggerFactory.getLogger(getClass()); protected final Map> exporterMap = new ConcurrentHashMap<>(); //key:group/serviceName:version:port //value:服务对应的exporter,dubbo协议为dubboExporter、triple协议为new AbstractExporter protected final Map serverMap = new ConcurrentHashMap<>(); //dubbo协议中使用,存储服务端创建的server // TODO SoftReference protected final Set> invokers = new ConcurrentHashSet<>(); protected frameworkModel frameworkModel; @Override public void setframeworkModel(frameworkModel frameworkModel) { this.frameworkModel = frameworkModel; } protected static String serviceKey(URL url) { int port = url.getParameter(Constants.BIND_PORT_KEY, url.getPort()); return serviceKey(port, url.getPath(), url.getVersion(), url.getGroup()); } protected static String serviceKey(int port, String serviceName, String serviceVersion, String serviceGroup) { return ProtocolUtils.serviceKey(port, serviceName, serviceVersion, serviceGroup); } //构建exporterMap的key,格式为:group/serviceName:version:port
服务端服务暴露(dubbo)
public class DubboProtocol extends AbstractProtocol { @Override public Exporter export(Invoker invoker) throws RpcException { checkDestroyed(); URL url = invoker.getUrl(); // export service. String key = serviceKey(url); DubboExporter exporter = new DubboExporter(invoker, key, exporterMap); //export a stub service for dispatching event Boolean isStubSupportEvent = url.getParameter(STUB_EVENT_KEY, DEFAULT_STUB_EVENT); Boolean isCallbackservice = url.getParameter(IS_CALLBACK_SERVICE, false); if (isStubSupportEvent && !isCallbackservice) { String stubServiceMethods = url.getParameter(STUB_EVENT_METHODS_KEY); if (stubServiceMethods == null || stubServiceMethods.length() == 0) { if (logger.isWarnEnabled()) { logger.warn(new IllegalStateException("consumer [" + url.getParameter(INTERFACE_KEY) + "], has set stubproxy support event ,but no stub methods founded.")); } } } openServer(url); optimizeSerialization(url); return exporter; }
服务端接收到消费请求处理
public class DubboProtocol extends AbstractProtocol { private ExchangeHandler requestHandler = new ExchangeHandlerAdapter() { @Override public CompletableFuture