当前位置: 首页 > 图灵资讯 > 技术篇> dubbo源码学习(四)初始化过程细节:解析服务

dubbo源码学习(四)初始化过程细节:解析服务

来源:图灵教育
时间:2023-06-05 09:33:12

今天,我将真正看到dubo内部的实现过程。在看dubo的源代码之前,我将大致阅读dubo的用户指南和开发指南,以便更容易看到dubo源代码。我们可以从用户指南和开发指南中找到相应的切入点。今天,我们将介绍dubo对bean的初步分析过程: 解析服务 基于dubo.jar中的META-INF/spring.handlers配置,Spring在遇到dubbo名称空间时,会回调dubonamespacehandler。 所有dubbo标签均由dubbobeandefinitionParser统一分析,XML标签根据一对一属性映射分析为bean对象。 Serviceconfig.export()或者Referenceconfigggigg.get()初始化时,将Bean对象转换为URL格式,将所有Bean属性转换为URL参数。 然后将URL传输到基于扩展点的Adaptive机制的Protocol扩展点,并根据URL协议头暴露或引用不同协议的服务。 dubbo服务的曝光调用是:ServiceConfig.export()代码如下:

com.alibaba.dubbo.config.ServiceConfig#export //暴露服务  public synchronized void export() {  if (provider != null) {  if (export == null) {  export = provider.getExport();  }  if (delay == null) {  delay = provider.getDelay();  }  }  if (export != null && ! export.booleanValue()) {  return;  }  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();  }  }

查看export调用链时,这种方法可以在两个地方调用: 1、com.alibaba.dubbo.config.spring.AnnotationBean#postProcessAfterInitialization:当注释方法暴露时 2、com.alibaba.dubbo.config.spring.ServiceBean#afterPropertiesSet:当spring配置文件暴露时, AnnnotationBean类继承关系 public class AnnotationBean extends AbstractConfig implements DisposableBean, BeanFactoryPostProcessor, BeanPostProcessor, ApplicationContextAware { AnnnotationBean实现了spring 在spring扫描完注解类后,bean与context相关的界面,并对完时调用进行分析 export()暴露服务的方法 ServiceBean public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener, BeanNameAware { 在spring初始化分析bean中完成,主要是对spring标签的分析,bean的定义,bean的属性分析设值完成后 进行 export() 因为dubbo是自己定制的标签,所以对bean的分析是 export 以前最重要的部分,今天先不看服务的暴露,先看dubo对服务的分析,重要的两类:

com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler com.alibaba.dubbo.config.spring.schema.DubboBeanDefinitionParser#parse 以下是DubbonamespaceHandler代码,加上我的注释(我自己的理解) public class DubboNamespaceHandler extends NamespaceHandlerSupport {  static {  /**  * 检索是否有重复命名空间处理器  */  Version.checkDuplicate(DubboNamespaceHandler.class);  }  public void init() {  /**  * 注册bean,真正的负分析是DubobeandefinitionParser  * dubobeandefinitionParser将分析所有属性,并将属性值放入beandefinition  */  registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));  registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));  registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));  registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));  registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));  registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));  registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));  registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));  registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));  registerBeanDefinitionParser("annotation", new DubboBeanDefinitionParser(AnnotationBean.class, true));  } } 所有的分析工作都在那里 DubboBeanDefinitionParser 中 /**  * 分析dubo自定义标签,将属性值设置为Beandefinition,此时bean还没有创建  * @param element  * @param parserContext  * @param beanClass  * @param required  * @return  */  @SuppressWarnings("unchecked")  private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {  RootBeanDefinition beanDefinition = new RootBeanDefinition();  beanDefinition.setBeanClass(beanClass);  //设置懒加载为false,表示立即加载,spring启动时,立即进行实例化  //如果设置为true,则需要在第一次通过getbean向容器索取bean时进行实例化,spring 可以在bean的配置中配置  ////这里会设置懒加载到false,但也可以得出推断:dubo标签创建的bean是单例bean(singleton bean)  //因为lazy-init的设置只针对singleton bean有效,对原型bean(prototype无效)  beanDefinition.setLazyInit(false);  String id = element.getAttribute("id");  ///如果没有设置beanid  if ((id == null || id.length() == 0) && required) {  String generatedBeanName = element.getAttribute("name");  //name没有配置  if (generatedBeanName == null || generatedBeanName.length() == 0) {  //如果是ProtocolConfig类型,bean 默认name dubbo,其他interface值为配置  if (ProtocolConfig.class.equals(beanClass)) {  generatedBeanName = "dubbo";  } else {  generatedBeanName = element.getAttribute("interface");  }  }  /*  * 假如还是null 那么取 beanClass 的名字,beanClass 其实就是要分析的类型  * 如:com.alibaba.dubbo.config.ApplicationConfig  */  if (generatedBeanName == null || generatedBeanName.length() == 0) {  generatedBeanName = beanClass.getName();  }  //如果id没有设置,那么 id = generatedBeanName,如果是ProtocolConfig类型,自然是 dubbo  id = generatedBeanName;  int counter = 2;  /*  * 因为spring的beang id不能重复,但有些标签可能会配置多个标签,如:<dubbo:registry  * 所以 id 在后面添加数字 2、3、4 区分  */  while(parserContext.getRegistry().containsBeanDefinition(id)) {  id = generatedBeanName + (counter ++);  }  }  if (id != null && id.length() > 0) {  //检查是否有 bean id 相同的  if (parserContext.getRegistry().containsBeanDefinition(id)) {  throw new IllegalStateException("Duplicate spring bean id " + id);  }  /*  * 注册 bean 定义  * org.springframework.beans.factory.support.DefaultListableBeanFactory#registerBeanDefinition  * 会按照 id 也就是说,beannname做一些检查,判断bean是否重载加载等  * 其实跟着代码走 bean 注册也是放进去的 ConcurrentHashMap 里  * beannnname就是这里 id 会放到 list 里  */  parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);  ///给bean添加属性值  beanDefinition.getPropertyValues().addPropertyValue("id", id);  }  if (ProtocolConfig.class.equals(beanClass)) {  for (String name : parserContext.getRegistry().getBeanDefinitionNames()) {  BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name);  PropertyValue property = definition.getPropertyValues().getPropertyValue("protocol");  if (property != null) {  Object value = property.getValue();  if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig) value).getName())) {  definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id));  }  }  }  } else if (ServiceBean.class.equals(beanClass)) { //解析<dubbo:service  String className = element.getAttribute("class");//获取类全名  if(className != null && className.length() > 0) {  RootBeanDefinition classDefinition = new RootBeanDefinition();  //通过反射获取类  classDefinition.setBeanClass(ReflectUtils.forName(className));  classDefinition.setLazyInit(false);  /*  分析子节点,有些配置可能是:  <dubbo:service interface="com.alihealth.dubbo.api.drugInfo.service.DemoService"  executes="10" >  <property ref="demoService" name="ref"></property>  <property value="1.0.0" name="version"></property>  </dubbo:service>  */  parseProperties(element.getChildNodes(), classDefinition);  /*  直接设置ref 接口名 + Impl 的bean ?  如:com.alihealth.dubbo.api.drugInfo.service.DemoService + Impl 为什么bean?  那<dubbo:service中定义的service ref 属性有啥用  */  beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));  }  } else if (ProviderConfig.class.equals(beanClass)) {  /*  <dubbo:provider 为缺省配置 ,所以在分析的时候,如果<dubbo:service有些值没有配置,那么会用<dubbo:provider值填充  */  parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);  } else if (ConsumerConfig.class.equals(beanClass)) {  /*  * 同上  */  parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);  }  Set<String> props = new HashSet<String>();  ManagedMap parameters = null;  for (Method setter : beanClass.getMethods()) {  String name = setter.getName();  ////注入model时,比如ServiceConfig,方法必须从set开始,而且参数只能是1  if (name.length() > 3 && name.startsWith("set")  && Modifier.isPublic(setter.getModifiers())  && setter.getParameterTypes().length == 1) {  //方法参数类型,因为参数只能是1,直接取[0]  Class<?> type = setter.getParameterTypes()[0];  ///根据set方法名获取属性值,如:setListener 获得的属性为:listener  String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");  props.add(property);  Method getter = null;  try {  getter = beanClass.getMethod("get" + name.substring(3), new Class<?>[0]);  } catch (NoSuchMethodException e) {  try {  getter = beanClass.getMethod("is" + name.substring(3), new Class<?>[0]);  } catch (NoSuchMethodException e2) {  }  }  if (getter == null  || !>[0]);  } catch (NoSuchMethodException e2) {  }  }  if (getter == null  || ! Modifier.isPublic(getter.getModifiers())  || ! type.equals(getter.getReturnType())) {  continue;  }  if ("parameters".equals(property)) {  /*  * 如果属性为 parameters,例如Protocolconfig中的setParametersers(Map<String, String> parameters)  * 然后去子节点获取 <dubbo:parameter  * <dubbo:protocol name="dubbo" host="127.0.0.1" port="9998" accepts="1000" >  <dubbo:parameter key="adsf" value="adf" />  <dubbo:parameter key="errer" value="aerdf" />  </dubbo:protocol>  */  parameters = parseParameters(element.getChildNodes(), beanDefinition);  } else if ("methods".equals(property)) {  /*  解析 <dubbo:method 并设置 methods 值 --serviceconfig  */  parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);  } else if ("arguments".equals(property)) {  /*  同上 ,解析<dubbo:argument --- MethodConfig  */  parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);  } else {  String value = element.getAttribute(property);  if (value != null) {  value = value.trim();  if (value.length() > 0) {  ///不在任何注册中心发布 registry = "N/A"  if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {  RegistryConfig registryConfig = new RegistryConfig();  registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);  beanDefinition.getPropertyValues().addPropertyValue(property, registryConfig);  } else if ("registry".equals(property) && value.indexOf(',') != -1) {  //多注册中心使用 , 号分隔  parseMultiRef("registries", value, beanDefinition, parserContext);  } else if ("provider".equals(property) && value.indexOf(',') != -1) {  parseMultiRef("providers", value, beanDefinition, parserContext);  } else if ("protocol".equals(property) && value.indexOf(',') != -1) {  //同上 多协议暴露  parseMultiRef("protocols", value, beanDefinition, parserContext);  } else {  Object reference;  if (isPrimitive(type)) 如果参数类型为{// java 的基本类型  if ("async".equals(property) && "false".equals(value)  || "timeout".equals(property) && "0".equals(value)  || "delay".equals(property) && "0".equals(value)  || "version".equals(property) && "0.0.0".equals(value)  || "stat".equals(property) && "-1".equals(value)  || "reliable".equals(property) && "false".equals(value)) {  /*  与旧版xsd中的default值兼容,上述配置值在xsd中有配置defalt值  <xsd:attribute name="version" type="xsd:string" use="optional" default="0.0.0">  */  value = null;  }  reference = value;  } else if ("protocol".equals(property)  //如果属性是 protocol 所以要判断protocol对应的扩展点配置是否有  && ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value)  ///检查当前使用的协议是否已分析 这里可以分析一下<dubbo:protocol name="dubbo"  && (! parserContext.getRegistry().containsBeanDefinition(value)  || ! ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {  if ("dubbo:provider".equals(element.getTagName())) {  logger.warn("Recommended replace <dubbo:provider protocol=\"" + value + "\" ... /> to <dubbo:protocol name=\"" + value + "\" ... />");  }  // 与旧版配置兼容  ProtocolConfig protocol = new ProtocolConfig();  protocol.setName(value);  reference = protocol;  } else if ("monitor".equals(property)  //同上  && (! parserContext.getRegistry().containsBeanDefinition(value)  || ! MonitorConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {  // 与旧版配置兼容  reference = convertMonitor(value);  } else if ("onreturn".equals(property)) {  ///回调方法 类似于onsucccessss  int index = value.lastIndexOf(".");  // bean的名字  String returnRef = value.substring(0, index);  String returnMethod = value.substring(index + 1);  reference = new RuntimeBeanReference(returnRef);  beanDefinition.getPropertyValues().addPropertyValue("onreturnMethod", returnMethod);  } else if ("onthrow".equals(property)) {  //回调 执行异常的方法 ,类似 onError  int index = value.lastIndexOf(".");  String throwRef = value.substring(0, index);  String throwMethod = value.substring(index + 1);  reference = new RuntimeBeanReference(throwRef);  beanDefinition.getPropertyValues().addPropertyValue("onthrowMethod", throwMethod);  } else {  if ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) {  BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);  /*  必须是单例bean(singleton),原型bean(prototype)不,sevice初始化一次,spring容器中只有一个 实例  是否与dubbo的权力等有关,如果是原型bean,那么服务就会变得有状态。  */  if (! refBean.isSingleton()) {  throw new IllegalStateException("The exported service ref " + value + " must be singleton! Please set the " + value + " bean scope to singleton, eg: <bean id=\"" + value+ "\" scope=\"singleton\" ...>");  }  }  reference = new RuntimeBeanReference(value);  }  /*  设置属性,beann值是另一个相关的  RuntimeBeanReference 当beanfactory作为另一个bean的引用时,固定占位符类作为属性值对象,在运行过程中进行分析  */  beanDefinition.getPropertyValues().addPropertyValue(property, reference);  }  }  }  }  }  }  NamedNodeMap attributes = element.getAttributes();  int len = attributes.getLength();  for (int i = 0; i < len; i++) {  Node node = attributes.item(i);  String name = node.getLocalName();  ///通过上述分析,如果还有一些属性没有被分析  if (! props.contains(name)) {  if (parameters == null) {  parameters = new ManagedMap();  }  String value = node.getNodeValue();  parameters.put(name, new TypedStringValue(value, String.class));  }  }  if (parameters != null) {  beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters);  }  return beanDefinition;  }  private static final Pattern GROUP_AND_VERION = Pattern.compile""""-.0-9_a-zA-Z]+(\\:[\\-.0-9_a-zA-Z]+)?$");  protected static MonitorConfig convertMonitor(String monitor) {  if (monitor == null || monitor.length() == 0) {  return null;  }  if (GROUP_AND_VERION.matcher(monitor).matches()) {  String group;  String version;  int i = monitor.indexOf(':');  if (i > 0) {  group = monitor.substring(0, i);  version = monitor.substring(i + 1);  } else {  group = monitor;  version = null;  }  MonitorConfig monitorConfig = new MonitorConfig();  monitorConfig.setGroup(group);  monitorConfig.setVersion(version);  return monitorConfig;  }  return null;  }  private static boolean isPrimitive(Class<?> cls) {  return cls.isPrimitive() || cls == Boolean.class || cls == Byte.class  || cls == Character.class || cls == Short.class || cls == Integer.class  || cls == Long.class || cls == Float.class || cls == Double.class  || cls == String.class || cls == Date.class || cls == Class.class;  }  @SuppressWarnings("unchecked")  private static void parseMultiRef(String property, String value, RootBeanDefinition beanDefinition,  ParserContext parserContext) {  //解析 registries 、providers、protocols 支持多引用  String[] values = value.split("\\s*[,]+\\s*");  ManagedList list = null;  for (int i = 0; i < values.length; i++) {  String v = values[i];  if (v != null && v.length() > 0) {  if (list == null) {  list = new ManagedList();  }  list.add(new RuntimeBeanReference(v));  }  }  beanDefinition.getPropertyValues().addPropertyValue(property, list);  }  private static void parseNested(Element element, ParserContext parserContext, Class<?> beanClass,  boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) {  NodeList nodeList = element.getChildNodes();  if (nodeList != null && nodeList.getLength() > 0) {  boolean first = true;  for (int i = 0; i < nodeList.getLength(); i++) {  Node node = nodeList.item(i);  if (node instanceof Element) {  if (tag.equals(node.getNodeName())  || tag.equals(node.getLocalName())) {  if (first) {  first = false;  String isDefault = element.getAttribute("default");  /*  如果 <dubbo:provider default开关没有配置标签,然后直接设置 default = "false"  这样做的目的是为了让 <dubbo:provider中的配置只是 <dubbo:service 或 <dubbo:reference的默认或缺乏配置  */  if (isDefault == null || isDefault.length() == 0) {  beanDefinition.getPropertyValues().addPropertyValue("default", "false");  }  }  BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required);  if (subDefinition != null && ref != null && ref.length() > 0) {  subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref));  }  }  }  }  }  }  private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) {  if (nodeList != null && nodeList.getLength() > 0) {  for (int i = 0; i < nodeList.getLength(); i++) {  Node node = nodeList.item(i);  if (node instanceof Element) {  //如果是 <property 元素  if ("property".equals(node.getNodeName())  || "property".equals(node.getLocalName())) {  String name = ((Element) node).getAttribute("name");  if (name != null && name.length() > 0) {  String value = ((Element) node).getAttribute("value");  //获取 ref  String ref = ((Element) node).getAttribute("ref");  if (value != null && value.length() > 0) {  beanDefinition.getPropertyValues().addPropertyValue(name, value);  } else if (ref != null && ref.length() > 0) {  beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref));  } else {  /*  只支持两种property设置方法:  <property ref="" name="">  <property value="" name="">  */  throw new UnsupportedOperationException("Unsupported <property name=\"" + name + "\"> sub tag, Only supported <property name=\"" + name + "\" ref=\"...\" /> or <property name=\"" + name + "\" value=\"...\" />");  }  }  }  }  }  }  }  @SuppressWarnings("unchecked")  private static ManagedMap parseParameters(NodeList nodeList, RootBeanDefinition beanDefinition) {  if (nodeList != null && nodeList.getLength() > 0) {  ManagedMap parameters = null;  for (int i = 0; i < nodeList.getLength(); i++) {  Node node = nodeList.item(i);  if (node instanceof Element) {  //解析 <dubbo:parameter  if ("parameter".equals(node.getNodeName())  || "parameter".equals(node.getLocalName())) {  if (parameters == null) {  parameters = new ManagedMap();  }  String key = ((Element) node).getAttribute("key");  String value = ((Element) node).getAttribute("value");  boolean hide = "true".equals(((Element) node).getAttribute("hide"));  if (hide) {  key = Constants.HIDE_KEY_PREFIX + key;  }  //添加参数,String 类型  parameters.put(key, new TypedStringValue(value, String.class));  }  }  }  return parameters;  }  return null;  }  @SuppressWarnings("unchecked")  private static void parseMethods(String id, NodeList nodeList, RootBeanDefinition beanDefinition,  ParserContext parserContext) {  if (nodeList != null && nodeList.getLength() > 0) {  ManagedList methods = null;  for (int i = 0; i < nodeList.getLength(); i++) {  Node node = nodeList.item(i);  if (node instanceof Element) {  Element element = (Element) node;  //<dubbo:method  if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) {  String methodName = element.getAttribute("name");  if (methodName == null || methodName.length() == 0) {  throw new IllegalStateException("<dubbo:method> name attribute == null");  }  if (methods == null) {  methods = new ManagedList();  }  //解析 <dubbo:method MethodConfig  BeanDefinition methodBeanDefinition = parse(((Element) node),  parserContext, MethodConfig.class, false);  String name = id + "." + methodName;  BeanDefinitionHolder methodBeanDefinitionHolder = new BeanDefinitionHolder(  methodBeanDefinition, name);  methods.add(methodBeanDefinitionHolder);  }  }  }  if (methods != null) {  beanDefinition.getPropertyValues().addPropertyValue("methods", methods);  }  }  }

分析的最终目的是返回 RootBeanDefinition 对象,Rootbeandefinition包含了对bean进行分析的所有信息,注意bean分析后,其实只是spring将其转化为spring内部的抽象数据对象结构,bean的创建(实例化)是第一次调用 getBean 时创建的。以上是dubo对配置文件和服务定义的分析过程。以上是dubbo对配置文件和服务定义的分析过程。然后写下dubo服务的曝光。 关注我获取视频 [img]http://dl2.iteye.com/upload/attachment/0129/523/9485a98f-3715-3b4-a953-a8e2498.jpg[/img]