RRiBbit,一个事件总线.基于spring配置不同服务通信!

1.何为RRiBbit?

*一种开源事件总线技术,能够让模块(组件)之间双向通讯,也被称为请求相应总线(request-response-bus),使用简单,相对其他框架来说,RRiBbit只需要加个注解@Listener即可,不需要改变源码,上手容易.

2.RRiBbit maven依赖:

注意版本问题: 如果需要整合spring的话,根据spring版本选择适当的版本,spring版本低,建议使用低版本的rribbit

<dependency>

<groupId>org.rribbit</groupId>

<artifactId>rribbit</artifactId>

<version>5.0.0</version>

</dependency>

3.架构图:

*此图是使用Rmi作为不同服务之间通信

RRiBbit,一个事件总线.基于spring配置不同服务通信!

* local开头代表单应用通信,Rmi代表不同服务通信

4.spring配置rribbit,单个模块事件发送和监听方法

(1).配置spring配置文件

<bean id="creator" class="org.rribbit.creation.SpringBeanClassBasedListenerObjectCreator">

<property name="packageNames">

<list>

<value>com.cx</value>

</list>

</property>

</bean>

<bean id="requestResponseBus" class="org.rribbit.util.RRiBbitUtil" factory-method="createRequestResponseBusForLocalUse">

<constructor-arg ref="creator" />

<constructor-arg value="true" />

</bean>

(2),在代码注入RequestResponseBus,调用send方法,设置监听的标识符和值,监听方只需加上@listener注解,设置监听的标识符就Ok了,官方文档地址:http://www.rribbit.org/documentation/howtouse.html

5.spring配置rribbit,不同服务模块事件发送和监听方法

*rribbit不同服务之间通信有三种方法,基于RMI,基于JMS,还有一种基于http(可根据业务需求选择合适的方法使用)

官方文档地址:http://www.rribbit.org/documentation/remoting.html

(1).使用基于Rmi不同服务模块通信

*java代码实现

//设置要扫描的包

SpringBeanClassBasedListenerObjectCreator springBeanClassBasedListenerObjectCreator=new SpringBeanClassBasedListenerObjectCreator();

List<String> packageNames =new ArrayList<>();

packageNames.add("com.cx");

springBeanClassBasedListenerObjectCreator.setPackageNames(packageNames);

ListenerObjectRetriever listenerObjectRetriever=new DefaultListenerObjectRetriever(springBeanClassBasedListenerObjectCreator);

ListenerObjectExecutor listenerObjectExecutor=new MultiThreadedListenerObjectExecutor();

//设置监听的端口号

//可以把rmiRequestProcessor交给spring管理

RmiRequestProcessorImpl rmiRequestProcessor=new RmiRequestProcessorImpl(8001,listenerObjectRetriever,listenerObjectExecutor);

//设置调用的服务端口号和ip地址

RmiRequestDispatcher rmiRequestDispatcher=new RmiRequestDispatcher(8001,"127.0.0.1");

//创建发送对象

RequestResponseBus requestResponseBus=new DefaultRequestResponseBus();

((DefaultRequestResponseBus) requestResponseBus).setRequestDispatcher(rmiRequestDispatcher);

//发送的事件信息数据

requestResponseBus.send("add","123");

注意! 需要监听该事件的模块也要配置rmiRequestProcessor,方法一样的

*把rmiRequestProcessor交由spring管理,spring配置使用如下

<!--      SpringBeanClassBasedListenerObjectCreator springBeanClassBasedListenerObjectCreator=new SpringBeanClassBasedListenerObjectCreator();

List<String> packageNames =new ArrayList<>();

packageNames.add("com.cx");

springBeanClassBasedListenerObjectCreator.setPackageNames(packageNames);-->

<bean id="springBeanClassBasedListenerObjectCreator" class="org.rribbit.creation.SpringBeanClassBasedListenerObjectCreator">

<property name="packageNames">

<list>

<value>com.cx</value>

</list>

</property>

</bean>

<!--ListenerObjectRetriever listenerObjectRetriever=new DefaultListenerObjectRetriever(springBeanClassBasedListenerObjectCreator);-->

<bean id="listenerObjectRetriever" class="org.rribbit.retrieval.DefaultListenerObjectRetriever">

<property name="listenerObjectCreator" ref="springBeanClassBasedListenerObjectCreator" />

</bean>

<!--ListenerObjectExecutor listenerObjectExecutor=new MultiThreadedListenerObjectExecutor();-->

<bean id="listenerObjectExecutor" class="org.rribbit.execution.MultiThreadedListenerObjectExecutor"></bean>

<!--RmiRequestProcessorImpl rmiRequestProcessor=new RmiRequestProcessorImpl(8001,listenerObjectRetriever,listenerObjectExecutor);-->

<bean id="requestProcessor" class="org.rribbit.processing.RmiRequestProcessorImpl">

<property name="listenerObjectExecutor" ref="listenerObjectExecutor"/>

<property name="listenerObjectRetriever" ref="listenerObjectRetriever"/>

<constructor-arg name="portnumber" value="8001"/>

</bean>

<!--配置发送对象

RequestResponseBus requestResponseBus=new DefaultRequestResponseBus();-->

<bean id="requestResponseBus" class="org.rribbit.DefaultRequestResponseBus"></bean>

</beans>

使用方法:

//创建调用的服务端口号和ip地址

RmiRequestDispatcher rmiRequestDispatcher=new RmiRequestDispatcher(8001,"127.0.0.1");

注入requestResponseBus,

设置requestResponseBus.setRequestDispatcher(rmiRequestDispatcher);

使用requestResponseBus.send方法发送事件就OK了,

注意!! rribbit会返回服务监听方抛出的异常!!

RRiBbit,一个事件总线.基于spring配置不同服务通信!

上一篇:【Xamarin 跨平台机制原理剖析】


下一篇:【转】Git常用命令备忘