Nepxion-Thunder(QQ 群 471164539)发布在https://github.com/Nepxion/
Thunder的扫描框架是采用Spring自定义标签扫描来初始化的
1. 定义名称空间
- 定义thunder-1.0.xsd,通过xsd文件进行对Spring配置文件节点的定义,规范对服务端/调用端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自定义扫描线程
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放入缓存,创建RegistryInitializer和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,监听服务上下线,创建动态调用的代理对象
3. Spring Xml格式介绍
- 定义服务方配置,一般命名为xxx-server-context.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"> <!-- 应用配置,配置所属应用,组和集群,port端口一定要配置,(host一般缺省为localhost) ,对于host和port也可以用-DThunderHost,-DThunderPort通过命令行配置,也可以用System.setProperty方式设置 --> <thunder:application id="application" application="APP-IOS" group="MY_GROUP" cluster="serverCluster" port="5010"/> <!-- 注册中心配置,可选值:zookeeper(可配置多个地址,用逗号隔开,例如192.168.0.1:2181,192.168.0.2:2181),可以通过-DThunderRegistryAddress通过命令行配置,也可以用System.setProperty方式设置 --> <!-- config可选值为remote,local。如果启动远程配置,同时也存在本地配置,远程配置将覆盖本地配置 --> <thunder:registry id="registry" type="zookeeper" address="localhost:2181" config="remote"/> <!-- 协议配置,可选值:netty,hessian,kafka,activemq,tibco --> <thunder:protocol id="protocol" type="netty"/> <!-- 策略配置,负载均衡(loadbalance),可选值:consistentHash(一致性Hash,Ketama算法,参考MemCache源码),roundRobin(权重轮循),random(随机轮循),该项不支持MQ(删除该项)--> <thunder:strategy id="strategy" loadbalance="consistentHash"/> <!-- 监控配置,可选值:logService(利用Splunk做日志监控收集),cacheService(利用Redis缓存做日志监控收集),webService(利用webService平台做监控数据接收),可以单个,也可以多个组合使用,该项不支持hessian(删除该项)--> <thunder:monitor id="monitor" type="logService,cacheService"/> <!-- 服务配置,接口名和实例。一旦配置该节点,thunder启动,即作为服务提供方 --> <!-- 当Protocol为MQ时,可以指定MQ服务器,例如server="activeMQ-1",名称为MQ的配置名 --> <thunder:service id="echoServiceImpl" interface="com.nepxion.thunder.service.EchoService" ref="_echoServiceImpl"/> <!-- 异常的EventBus事件发布拦截 --> <bean id="eventInterceptor" class="com.nepxion.thunder.service.ServiceEventInterceptor"/> </beans>
- 定义调用方方配置,一般命名为xxx-client-context.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"> <thunder:application id="application" application="APP-IOS" group="MY_GROUP" cluster="clientCluster" port="6010"/> <thunder:registry id="registry" type="zookeeper" address="localhost:2181" config="remote"/> <thunder:protocol id="protocol" type="netty"/> <thunder:strategy id="strategy" loadbalance="consistentHash"/> <thunder:monitor id="monitor" type="logService,cacheService"/> <!-- 引用配置,接口名。一旦配置该节点,thunder启动,即作为服务调用方 --> <!-- 如果在一个XML里面,既有thunder:service,也有thunder:reference,那它既是服务提供方,又是服务调用方 --> <!-- 当Protocol为MQ时,可以指定MQ服务器,例如server="activeMQ-1",名称为MQ的配置名 --> <thunder:reference id="echoService" interface="com.nepxion.thunder.service.EchoService"> <!-- 方法配置,可选参数如下: --> <!-- method即方法名 --> <!-- traceIdIndex即把方法第几个参数作为全局跟踪Id,它的用处是作为调用链分析。如果不配置该值,默认为第一个参数为traceId --> <!-- parameterTypes即参数类型,不配置,默认为无参 --> <!-- async即同步或者异步方法,值为true,false,不配置,默认为true,即异步方法 --> <!-- timeout即超时毫秒值,如果async=true,不能出现该项;在async=false,如果不配置该值,同步默认为30000毫秒,异步超时默认为60000 --> <!-- broadcast即异步广播方式,值为true,false,如果async=false,不能出现该项,不配置,默认值为false --> <!-- callback即异步回调接口,如果async=false,不能出现该项,即同步方法不支持回调;如果async=true,没有该项,服务端只会处理,不会返回callback结果;如果broadcast=true,不能出现该项,即广播方法不支持回调 --> <!-- callback支持链式调用,当定义成callback="promise"的时候,就采用链式调用,业务端就不必实现回调接口 --> <thunder:method method="getEcho" traceIdIndex="0" parameterTypes="java.lang.String" async="false"/> </thunder:reference> <!-- 异常的EventBus事件发布拦截 --> <bean id="eventInterceptor" class="com.nepxion.thunder.service.ServiceEventInterceptor"/> </beans>
框架提供更为简便的全局配置方式
<?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"> <!-- 如果接口下所有的方法调用方式一致,那么不需要在具体每个方法上做配置,需要配置在reference节点 --> <!-- 如果接口下所有的方法并不是多态方式存在,即不存在同名方法,可以省略一切方法的配置;如果存在同名方法,必须通过parameterTypes参数做区分 --> <!-- 如果接口下希望大多数方法遵照全局配置,而某个方法需要特殊配置,例如5个方法里面4个是同步调用,1个是异步调用,那就具体配置那个方法即可,其它方法配置可省略 --> <thunder:reference id="echoService" interface="com.nepxion.thunder.service.EchoService" async="false" timeout="15000".../> </beans>