(四) Nepxion分布式RPC框架 - Spring框架

Nepxion Thunder(QQ 1394997)发布在淘宝代码基地 http://code.taobao.org/p/Thunder/

 

Thunder的主体框架是采用Spring自定义标签扫描来初始化的

1. 定义名称空间

(四) Nepxion分布式RPC框架 - Spring框架_第1张图片

  • 定义thunder-1.0.xsd,规范对服务端/调用端XML格式
  • 定义spring.schemas,实现对thunder-1.0.xsd的引用
    http\://www.nepxion.com/schema/thunder/thunder-1.0.xsd=com/nepxion/thunder/thunder-1.0.xsd
  • 定义spring.handlers,定义对Spring扫描线程的入口
    http\://www.nepxion.com/schema/thunder=com.nepxion.thunder.framework.ThunderNamespaceHandlerSupport
  • 服务端/调用端XML格式的名称空间为
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:aop="http://www.springframework.org/schema/aop" 
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:thunder="http://www.nepxion.com/schema/thunder"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
               http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
               http://www.nepxion.com/schema/thunder http://www.nepxion.com/schema/thunder/thunder-1.0.xsd">
    
    </beans>

2. Spring自定义扫描线程

 (四) Nepxion分布式RPC框架 - Spring框架_第2张图片
    Spring扫描线程的过程是,先依次初始化全部的BeanDefinitionParser,再依次初始化全部的FactoryBean,所以怎么初始化有点讲究

  •  入口:ThunderNamespaceHandlerSupport.java,注册BeanDefinitionParser
    public class ThunderNamespaceHandlerSupport extends NamespaceHandlerSupport {
        @Override
        public void init() {
            ThunderProperties properties = ThunderPropertiesManager.getProperties();
                    
            CacheContainer cacheContainer = new CacheContainer();
            ExecutorContainer executorContainer = new ExecutorContainer();
            
            ThunderDelegate delegate = new ThunderDelegateImpl();
            delegate.setProperties(properties);
            delegate.setCacheContainer(cacheContainer);
            delegate.setExecutorContainer(executorContainer);
    
            registerBeanDefinitionParser(ThunderConstants.APPLICATION_ELEMENT_NAME, new ApplicationBeanDefinitionParser(delegate));
            registerBeanDefinitionParser(ThunderConstants.REGISTRY_ELEMENT_NAME, new RegistryBeanDefinitionParser(delegate));
            registerBeanDefinitionParser(ThunderConstants.PROTOCOL_ELEMENT_NAME, new ProtocolBeanDefinitionParser(delegate));
            registerBeanDefinitionParser(ThunderConstants.STRATEGY_ELEMENT_NAME, new StrategyBeanDefinitionParser(delegate));
            registerBeanDefinitionParser(ThunderConstants.MONITOR_ELEMENT_NAME, new MonitorBeanDefinitionParser(delegate));
            registerBeanDefinitionParser(ThunderConstants.SERVICE_ELEMENT_NAME, new ServiceBeanDefinitionParser(delegate));
            registerBeanDefinitionParser(ThunderConstants.REFERENCE_ELEMENT_NAME, new ReferenceBeanDefinitionParser(delegate));
            registerBeanDefinitionParser(ThunderConstants.METHOD_ELEMENT_NAME, new MethodBeanDefinitionParser(delegate));
        }
    }
  • BeanDefinitionParser,下列BeanDefinitionParser都继承于AbstractExtensionBeanDefinitionParser.java
    1)ApplicationBeanDefinitionParser.java - 实现对<thunder:application id="application"...>节点的解析,创建ApplicationEntity放入缓存,初始化ApplicationFactoryBean
    2)RegistryBeanDefinitionParser.java - 实现对<thunder:registry id="registry"...>节点的解析,创建RegistryEntity放入缓存,创建RegistryExecutor(注册中心),初始化RegistryFactoryBean
    3)ProtocolBeanDefinitionParser.java - 实现对<thunder:protocol id="protocol"...>节点的解析,创建ProtocolEntity放入缓存(如果是Hessian,则要创建WebServiceEntity放入缓存;如果是MQ,则要创建MQEntity放入缓存),初始化ProtocolFactoryBean
    4)StrategyBeanDefinitionParser.java - 实现对<thunder:strategy id="strategy"...>节点的解析,创建StrategyEntity(包含LoadBalanceType等信息)放入缓存,创建LoadBalanceExecutor(负载均衡)和ConsistencyExecutor(同步中心),初始化StrategyFactoryBean
    5)MonitorBeanDefinitionParser.java - 实现对<thunder:monitor id="monitor"...>节点的解析,创建MonitorEntity放入缓存,创建MonitorExecutor(监控中心),初始化MonitorFactoryBean
    6)ServiceBeanDefinitionParser.java - 实现对<thunder:service...>节点的解析,创建ServiceEntity放入缓存,创建ServerExecutor和ServerHandlerAdapter(服务提供者类),SecurityExecutor(服务端安全控制中心),初始化ServiceFactoryBean
    7)ReferenceBeanDefinitionParser.java - 实现对<thunder:reference...>节点的解析,创建ReferenceEntity放入缓存,创建ClientExecutor,ClientHandlerAdapter和ClientInterceptorAdapter(服务调用者类), 初始化ReferenceFactoryBean
    8)MethodBeanDefinitionParser.java - 实现对<thunder:method...>节点的解析,初始化MethodEntity
  • FactoryBean,下列FactoryBean都继承于AbstractFactoryBean.java
    1)ApplicationFactoryBean.java - 空实现
    2)RegistryFactoryBean.java - 实现和注册中心连接,初始化初始化注册中心,注册和获取远程配置,初始化一些工厂类,注册和监听ApplicationConfig,注册重连监听
    3)ProtocolFactoryBean.java - 初始化一些工厂类
    4)StrategyFactoryBean.java - 空实现
    5)MonitorFactoryBean.java - 实现从注册中心获取监控中心URL列表,监听监控中心上下线
    6)ServiceFactoryBean.java - 缓存服务(ServiceEntity),启动服务端,注册和监听ServiceConfig,开启限流
    7)ReferenceFactoryBean.java - 缓存调用(ReferenceEntity),从注册中心获取服务列表来启动调用端,注册和监听ReferenceConfig,监听服务上下线,创建动态调用的代理对象

你可能感兴趣的:(redis,kafka,activemq,hessian,netty)