Nepxion Thunder(QQ 1394997)发布在淘宝代码基地 http://code.taobao.org/p/Thunder/
1. 运行
- 1.1 Thunder任何Demo运行之前,只需要启动Zookeeper,同时在XML里面指定Zookeeper的地址,无任何其他配置。如果是运行MQ的方式启动,则自行安装MQ(Kafka,ActiveMQ,Tibco,Redis等)
- 1.2 Thunder/ trunk / nepxion-thunder-stock / bin / application / nepxion-thunder-stock-1.0.0-bin.zip 解压,运行里面的分布式.bat,可以看到基于Thunder的股票分布式查询系统
- 1.3 Thunder/ trunk / nepxion-thunder-test 可以看到这些通信方式的压力测试用例
2. 示例
- 2.1 定义序列化的实体类Echo
public class Echo implements Serializable { private static final long serialVersionUID = 4670513495087782005L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
- 2.2 定义Echo的接口EchoService
public interface EchoService { Echo getEcho(String name) }
- 2.3 实现Echo的接口EchoService
public class EchoServiceImpl implements EchoService { @Override public Echo getEcho(String name) { return new Echo(); } }
- 2.4 定义服务方配置,把Echo的Service配上去,比如名字叫netty-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) --> <thunder:application id="application" application="APP-IOS" group="POA_EA_INF" port="5010"/> <!-- 注册中心配置,可选值:zookeeper(可配置多个地址,用逗号隔开,例如192.168.0.1:2181,192.168.0.2:2181) --> <!-- 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(随机轮循),该项不支持activemq(删除该项)--> <thunder:strategy id="strategy" loadbalance="consistentHash"/> <!-- 监控配置,可选值:logService(利用Splunk做日志监控),webService(利用webService平台做监控数据接收),该项不支持hessian(删除该项)--> <thunder:monitor id="monitor" type="logService"/> <!-- 服务配置,接口名和实例。一旦配置该节点,thunder启动,即作为服务提供方 --> <!-- 当Protocol为MQ时,可以指定MQ服务器,例如server="activeMQ-1",名称为MQ的配置名 --> <thunder:service id="echoServiceImpl" interface="com.nepxion.thunder.service.EchoService" ref="_echoServiceImpl"/> <!-- 服务所对应的实例配置 --> <bean name="_echoServiceImpl" class="com.nepxion.thunder.service.EchoServiceImpl"/> </beans>
- 2.5 定义调用方方配置,把Echo的Reference配上去,比如名字叫netty-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="POA_EA_INF" 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"/> <!-- 引用配置,接口名。一旦配置该节点,thunder启动,即作为服务调用方 --> <!-- 如果在一个XML里面,既有thunder:service,也有thunder:reference,那它既是服务提供方,又是服务调用方 --> <!-- 当Protocol为MQ时,可以指定MQ服务器,例如server="activeMQ-1",名称为MQ的配置名 --> <thunder:reference id="echoService" interface="com.nepxion.thunder.service.EchoService"> <!-- 方法配置,可选参数如下: --> <!-- method即方法名 --> <!-- parameterTypes即参数类型,不配置,默认为无参 --> <!-- async即同步或者异步方法,值为true,false,不配置,默认为true,即异步方法 --> <!-- timeout即同步超时毫秒值,如果async=true,不能出现该项;在async=false,如果不配置该值,默认为30000毫秒 --> <!-- broadcast即异步广播方式,值为true,false,如果async=false,不能出现该项,不配置,默认值为false --> <!-- callback即异步回调接口,如果async=false,不能出现该项,即同步方法不支持回调;如果async=true,没有该项,服务端只会处理,不会返回callback结果;如果broadcast=true,不能出现该项,即广播方法不支持回调 --> <thunder:method method="getEcho" parameterTypes="java.lang.String" async="false"/> </thunder:reference> </beans>
- 2.6 配置 thunder-ext.properties,包括MQ连接信息配置,调优配置等,参照(十二) Nepxion分布式RPC框架 - 配置调优
- 2.7 定义服务方启动类
2.7.1 JUnit方式启动
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:netty-server-context.xml") @TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class }) public class NettyServerTest { @Test public void test() throws IOException { System.in.read(); } }
2.7.2 Application方式启动
public static void main(String[] args) { new ClassPathXmlApplicationContext("classpath*:netty-server-context.xml"); }
2.7.3 Web容器方式启动
定义在web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:netty-server-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
- 2.8 定义调用方启动类
2.8.1 JUnit方式启动
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:netty-client-context.xml") @TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class }) public class NettyClientTest { @Autowired private EchoService echoService; @Test public void test() throws Exception { echoService.getEcho(""); } }
2.8.2 Application方式启动
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:netty-client-context.xml"); EchoService echoService= (EchoService) context.getBean("echoService"); echoService.getEcho(""); }