(二) Nepxion-Thunder分布式RPC集成框架 - 使用

Nepxion-Thunder(QQ 群 471164539)发布在https://github.com/Nepxion/

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 可以看到这些通信方式的压力测试用例
  • 1.4 Thunder/ trunk / nepxion-thunder / src / test / java / com / nepxion / thunder / test 有怎么使用Thunder的Demo,有5种协议方式的调用
  • 1.5 Thunder/ trunk / nepxion-thunder / src / test / java / com / nepxion / thunder / trace 是比较复杂且几乎囊括了所有的调用方式,具体参考
    (十四) Thunder分布式RPC框架 - 调用

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) ,对于host和port也可以用-DThunderHost,-DThunderPort通过命令行配置,也可以用System.setProperty方式设置 -->
        <thunder:application id="application" application="APP-IOS" group="MY_GROUP" cluster="serverCluster" port="5010"/>
        
        <!-- 协议配置,可选值:netty,hessian,kafka,activemq,tibco -->
        <thunder:protocol id="protocol" type="netty"/>
       
        <!-- 注册中心配置,可选值: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"/>
       
        <!-- 策略配置,负载均衡(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>
  • 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="MY_GROUP" cluster="clientCluster" port="6010"/>
    
        <thunder:protocol id="protocol" type="netty"/>
        
        <thunder:registry id="registry" type="zookeeper" address="localhost:2181" config="remote"/>
       
        <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>
  • 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) {
            // 通过classpath寻址Spring XML的定义文件
            new ClassPathXmlApplicationContext("classpath*:netty-server-context.xml");
    
            // 通过Web系统寻址Spring XML的定义文件     
            // new ClassPathXmlApplicationContext("http://www.nepxion.com/Thunder/netty-server-context.xml");
    
            // 通过远程文件系统寻址Spring XML的定义文件
            // new FileSystemXmlApplicationContext("file://192.168.0.1\\Thunder\\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("abc");
        }
    }
    2.8.2 Application方式启动
        public static void main(String[] args) {
            // 通过classpath寻址Spring XML的定义文件
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:netty-client-context.xml");
    
            // 通过Web系统寻址Spring XML的定义文件     
            // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("http://www.nepxion.com/Thunder/netty-server-context.xml");
    
            // 通过远程文件系统寻址Spring XML的定义文件
            // ApplicationContext applicationContext = new FileSystemXmlApplicationContext("file://192.168.0.1\\Thunder\\netty-server-context.xml"); 
            EchoService echoService= (EchoService) applicationContext.getBean("echoService");
            echoService.getEcho("abc");
        }

猜你喜欢

转载自nepxion.iteye.com/blog/2259909