`
nepxion
  • 浏览: 37927 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

(二) 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");
        }
分享到:
评论
1 楼 zhjxzhj 2016-05-11  

相关推荐

    基于Java的Thunder分布式RPC框架设计源码

    Nepxion Thunder是一个基于Java的分布式RPC框架,集成了Netty、Hessian、Kafka、ActiveMQ、Tibco、Zookeeper、Redis、Spring Web MVC、Spring Boot和Docker等技术。它支持多协议、多组件和多序列化,为开发者提供了...

    协程式驱动框架Nepxion-Coroutine.zip

    Coroutine是基于Kilim/Promise JDeferred的协程式驱动框架,基于Apache Zookeeper的分布式规则存储和动态规则变更通知。 主要特性: 1. 基于微服务框架理念设计 2. 支持同步/异步调用 3. 支持串行/并行调用 4....

    Thunder::high_voltage: Nepxion Thunder is a distribution RPC framework based on Netty + Hessian + Kafka + ActiveMQ + Tibco + Zookeeper + Redis + Spring Web MVC + Spring Boot + Docker 多协议、多组件、多序列化的分布式RPC调用框架

    Nepxion Thunder是一款基于Netty + Hessian + Kafka + ActiveMQ + Tibco + Zookeeper(Curator Framework) + Redis + FST + Spring + Spring Web MVC + Spring Boot + Docker分布式RPC调用框架。架构思想主要是来自...

    yinheli/docker-thunder-xware:latest 镜像打包下载

    yinheli/docker-thunder-xware:latest 镜像打包下载 群晖 NAS DSM 系统,只要三步使用 Docker 安装迅雷远程下载

    Go-Thunder⚡️一个Go框架用于快速构建强大的graphql服务

    在压缩包"thunder-master"中,包含了Thunder框架的源码和其他相关资源。开发者可以通过查看源码,了解其内部实现原理,也可以直接使用它来快速搭建自己的GraphQL服务。在实际开发过程中,结合Go语言的标准库和第三方...

    wine-thunder_0.6-2_all.deb

    wine-thunder_0.6-2_all.deb用于在linux系统下,使用wine直接按装的迅雷软件,实现高速下载,在ubunut,fedora等linux版本中,实现直接点击安装

    开源项目-omeid-thunder.zip

    "thunder-master"这个压缩包子文件名可能代表项目的主分支或主代码库,这在Git等版本控制系统中很常见,"master"通常指的是默认分支,存放着项目的最新稳定版本。解压后,用户可以访问到项目的源代码、文档、构建...

    Go-Thunder是BoltDB的交互式Shell

    使用Go-Thunder时,用户可以通过输入特定的命令来执行各种操作,如`put`用于存储键值对,`get`用于检索,`del`用于删除,而`begin`、`commit`和`rollback`则用于事务处理。此外,它可能还提供了一些辅助命令,如`...

    home-work-thunder

    2. **测试驱动开发(TDD)**:编写可编译的测试用例是TDD的关键步骤,需要了解如何编写单元测试、集成测试,并使用测试框架如JUnit、pytest等。 3. **错误排查**:学习如何通过编译错误信息定位问题,使用调试工具如...

    A10-Thunder_1030S方案白皮书.pdf

    A10-Thunder_1030S方案白皮书.pdf

    系统工具-文件下载-thunder_3.4.0.4338.zip

    标题中的“系统工具-文件下载-thunder_3.4.0.4338.zip”表明这是一款系统工具,具体来说是与文件下载相关的。这里的“thunder”很可能指的是迅雷,一个在中国广为人知的下载管理软件。版本号“3.4.0.4338”指示这是...

    开源项目-muesli-thunder.zip

    在使用Thunder时,用户应首先下载并解压“muesli-thunder.zip”文件,得到“thunder-master”目录。然后按照项目提供的安装指南编译并安装Thunder,最后通过命令行启动Thunder,开始探索和操作BoltDB数据库。对于...

    A10-Thunder_6430S方案白皮书.pdf

    A10-Thunder_6430S方案白皮书.pdf

    3D-Thunder-Lightning.zip

    3D-Thunder-Lightning.zip,受航母指令启发的开源未来动作飞行模拟器游戏,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他应用程序。

    A10-Thunder_930方案白皮书.pdf

    A10 Thunder 930方案白皮书 A10 Thunder 930是A10 Networks公司推出的统一应用服务网关(UASG),采用64位系统、1U硬件,提供了极具性价比的解决方案。该设备基于A10极具扩展性的灵活高级核心操作系统(ACOS)架构...

    系统工具-文件下载-Thunderbird91.0b4.zip

    Thunderbird是一款由Mozilla基金会开发的开源邮件客户端,它集成了电子邮件、新闻组、RSS阅读器和日历功能,为用户提供了一站式的通信解决方案。Thunderbird91.0b4是该软件的一个版本,其中“91.0b4”表示的是版本号...

    A10-Thunder-5430S方案白皮书

    《A10 Thunder 5430S方案白皮书》是针对A10 Networks公司推出的Thunder 5430S高性能负载均衡解决方案的一份详细技术文档。在IT行业中,负载均衡是一项至关重要的技术,它确保了网络服务的高可用性和性能优化。A10 ...

    Android代码-Thunder

    Thunder Android OkHttp util package let response callback at MainThread(UIThread), also it‘s lifecycle safety. ⚠️ Thunder‘s code is based on SugarTask(Very nice code

    live-transit-thunder-bay:Thunder Bay 实时 GTFS 数据馈送的 JSON 代理

    实时过境雷湾 ... 运行npm install live-transit-thunder-bay --save 使用 API: var liveTransit = require ( 'live-transit-thunder-bay' ) ; liveTransit . start ( ) ; 运行node index.js 。 Expres

    Thunder-Download-Extension-for-Chrome_v3.1等.zip

    【Thunder-Download-Extension-for-Chrome_v3.1等.zip】这个压缩包文件包含的是针对谷歌浏览器(Chrome)的迅雷下载扩展程序的版本v3.1。迅雷下载工具是一款在中国广泛使用的下载管理器,它以其高速下载能力和对各种...

Global site tag (gtag.js) - Google Analytics