远程服务调用协议-Jgroup协议配置
Bboss Aop1.0.7 集成了Jgroup 2.8.0 GA,并对其进行扩展,嵌入了aop框架提供的安全认证、鉴权、消息加密功能。
Bboss Aop通过jgroup来支持带有集群特性的远程服务功能,其集群功能依赖于ip 组播协议,远程通讯的范围受ip组播协议的范围限制,因此只能在一个局域网段内使用其组播协议,udp协议上有此限制,其他协议(比如tcp)不受此限制。Jgroup本身的特性可参考其官方网站【http://www.jgroups.org/】。
下面具体介绍bboss aop 框架中如何使用jgroup 协议来实现远程服务调用。
1.1.1.1 Jgroup协议配置
1.1.1.2 配置文件
Ø 远程服务配置文件
/bbossaop/resources/org/frameworkset/spi/manager-rpc-service.xml
与jgroup相关的内容:
<!--
jgroups集群协议配置
-->
<property name="cluster_name" value="Cluster"/>
<property name="cluster_protocol" value="tcp"/>
<property name="cluster_protocol.tcp.configfile" value="etc/META-INF/replSync-service-aop-tcp.xml"/>
<property
name="cluster_protocol.udp.configfile" value="etc/META-INF/replSync-service-aop.xml"/>
cluster_name 用来指定集群名称
cluster_protocol 用来指定jgroup的网络通讯协议
cluster_protocol.xxx.configfile 特定协议的配置文件,其中的xxx对应cluster_protocol属性的value值,例如:
cluster_protocol.udp.configfile指定了udp协议的配置文件为
etc/META-INF/replSync-service-aop.xml,这是一个相对于classpath根目录的文件路径
cluster_protocol.tcp.configfile指定了tcp协议的配置文件为
etc/META-INF/replSync-service-aop-tcp.xml,这是一个相对于classpath根目录的文件路径
1.1.1.3 Jgroup协议配置文件
/bbossaop/resources/etc/META-INF/replSync-service-aop-tcp.xml
/bbossaop/resources/etc/META-INF/replSync-service-aop.xml
Jgroup可以采用多种网络通讯协议来实现远程通讯:tcp,udp等等,每种协议都有自己的配置文件,这些文件在manager-rpc-service.xml中导入。
1.1.1.4 协议配置
在部署jgroup协议时,首先需要指定jgroup通讯协议cluster_protocol协议
<property name="cluster_protocol" value="tcp"/>
然后修改相关协议配置文件中的内容,这里只介绍几个常用的配置项,其他的配置项可以采用默认值即可。
Udp 协议
/bbossaop/resources/etc/META-INF/replSync-service-aop.xml
设置组播地址
mcast_addr="232.10.10.99",相互通讯的应用必须配置一致的组播地址,这样所有的服务器都会动态加入组播地址相同的服务器组中。
设置绑定端口
bind_port="1187" ,要保证端口不会和其他的应用相冲突
设置绑定ip地址,可以不配置
bind_addr="172.16.17.216"
附内容如下:
<UDP mcast_addr="232.10.10.99" mcast_port="45588" tos="8"
ucast_recv_buf_size="20M" ucast_send_buf_size="640K"
mcast_recv_buf_size="25M" mcast_send_buf_size="640K" loopback="false"
discard_incompatible_packets="true" max_bundle_size="64K"
max_bundle_timeout="30" ip_ttl="2" enable_bundling="true"
enable_diagnostics="true" thread_naming_pattern="cl"
timer.num_threads="4" thread_pool.enabled="true"
thread_pool.min_threads="2" thread_pool.max_threads="8"
thread_pool.keep_alive_time="5000" thread_pool.queue_enabled="true"
thread_pool.queue_max_size="10000"thread_pool.rejection_policy="discard"
oob_thread_pool.enabled="true" oob_thread_pool.min_threads="1"
oob_thread_pool.max_threads="8" oob_thread_pool.keep_alive_time="5000"
oob_thread_pool.queue_enabled="false"
oob_thread_pool.queue_max_size="100"
oob_thread_pool.rejection_policy="Run" bind_port="1187"bind_addr="172.16.17.216" />
<PING timeout="2000" num_initial_members="3" />
<MERGE2 max_interval="30000" min_interval="10000" />
<FD_SOCK />
<FD_ALL />
<VERIFY_SUSPECT timeout="1500" />
<BARRIER />
<pbcast.NAKACK use_stats_for_retransmission="false"
exponential_backoff="0" use_mcast_xmit="true" gc_lag="0"
retransmit_timeout="300,600,1200" discard_delivered_msgs="true" />
<UNICAST timeout="300,600,1200" />
<pbcast.STABLE stability_delay="1000"
desired_avg_gossip="50000" max_bytes="1M" />
<pbcast.GMS print_local_addr="true" join_timeout="3000"
view_bundling="true" />
<FC max_credits="500K" min_threshold="0.20" />
<FRAG2 frag_size="60K" />
<!--pbcast.STREAMING_STATE_TRANSFER /-->
<pbcast.STATE_TRANSFER />
tcp协议
/bbossaop/resources/etc/META-INF/replSync-service-aop-tcp.xml
设置一组相互通讯的静态地址列表,格式为ip[port],ip[port]
<TCPPING timeout="3000" initial_hosts="172.16.17.216[1186],172.16.17.216[1187]"
port_range="1" num_initial_members="3" />
设定绑定端口
<TCP bind_port="1186"。。。。
附详细内容如下:
<TCP bind_port="1186" loopback="true" recv_buf_size="20M"
send_buf_size="640K"
discard_incompatible_packets="true" max_bundle_size="64K"
max_bundle_timeout="30" enable_bundling="true" use_send_queues="true"
sock_conn_timeout="300" timer.num_threads="4"thread_pool.enabled="true"
thread_pool.min_threads="1" thread_pool.max_threads="10"
thread_pool.keep_alive_time="5000" thread_pool.queue_enabled="false"
thread_pool.queue_max_size="100" thread_pool.rejection_policy="run"
oob_thread_pool.enabled="true" oob_thread_pool.min_threads="1"
oob_thread_pool.max_threads="8" oob_thread_pool.keep_alive_time="5000"
oob_thread_pool.queue_enabled="false"
oob_thread_pool.queue_max_size="100"
oob_thread_pool.rejection_policy="run" />
<TCPPING timeout="3000"
initial_hosts="172.16.17.216[1186],172.16.17.216[1187]"
port_range="1" num_initial_members="3" />
<MERGE2 min_interval="10000" max_interval="30000" />
<FD_SOCK />
<FD timeout="3000" max_tries="3" />
<VERIFY_SUSPECT timeout="1500" />
<BARRIER />
<pbcast.NAKACK use_mcast_xmit="false" gc_lag="0"
retransmit_timeout="300,600,1200,2400,4800"discard_delivered_msgs="true" />
<UNICAST timeout="300,600,1200" />
<pbcast.STABLE stability_delay="1000"
desired_avg_gossip="50000" max_bytes="400K" />
<pbcast.GMS print_local_addr="true" join_timeout="3000"
view_bundling="true" />
<FC max_credits="2M" min_threshold="0.10" />
<FRAG2 frag_size="60K" />
<pbcast.STREAMING_STATE_TRANSFER />
1.1.1.5 Jgroup调用方法
1.1.1.5.1 启动服务器
采用jgroup协议的通讯双方必须先启动jgroup协议,然后才能完成远程服务调用,启动方法如下:
org.frameworkset.spi.remote.RPCHelper.getRPCHelper().startJGroupServer();
双方都启动后客服端就可以发起服务调用,服务器端就可以接收服务调用了。
1.1.1.5.2 服务组件配置
客服端和服务器端配置组件:
<property name="rpc.test" singlable="true" class="org.frameworkset.spi.remote.RPCTest"/>
1.1.1.5.3 获取远程组件实例
单点调用
RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(jgroup::172.16.17.216:1186)/rpc.test");
多点调用
RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(jgroup::192.168.11.102:1186;192.168.11.102:12346)/rpc.test");
组播调用
RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(jgroup::all)/rpc.test");
单点调用-传递认证信息
RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(jgroup::172.16.17.216:1186)/rpc.test?user=admin&password=123456");
多点调用-传递认证信息
RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(jgroup::192.168.11.102:1186;192.168.11.102:12346)/rpc.test?user=admin&password=123456");
组播调用-传递认证信息
RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(jgroup::all)/rpc.test?user=admin&password=123456");
1.1.1.5.4 远程方法调用
Object count = testInf.getCount();
1.1.1.5.5 调用结果处理
单点调用的结果就是服务接口返回的值,无需处理。如果方法调用失败,系统将抛出具体的远程异常。
多点调用和组播调用的结果处理方法一样,以多点调用为例:
RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(jgroup::192.168.11.102:1186;192.168.11.102:12346)/rpc.test?User=admin&password=123456");
Object count = testInf.getCount();
获取192.168.11.102:1186返回的结果:
Object count_1186 = BaseSPIManager.getRPCResult("192.168.11.102", "1186", count);如果返回值是异常,那么直接抛出该异常。
或者
Object count_1186 = BaseSPIManager. getJGroupRPCResult
("192.168.11.102:1186", count);如果返回值是异常,那么直接抛出该异常。
或者
Object count_1186 =
BaseSPIManager.getRPCResult("192.168.11.102:1186", count,
org.frameworkset.remote.Target.BROADCAST_TYPE_JRGOUP); 如果返回值是异常,那么直接抛出该异常。
获取192.168.11.102:12346返回的结果:
Object count_12346 = BaseSPIManager.getRPCResult("192.168.11.102", "12346", count); 如果返回值是异常,那么直接抛出该异常。
或者
Object count_12346 = BaseSPIManager. getJGroupRPCResult
("192.168.11.102:12346", count);
或者
Object count_12346 =
BaseSPIManager.getRPCResult("192.168.11.102:12346", count,
org.frameworkset.remote.Target.BROADCAST_TYPE_JRGOUP); 如果返回值是异常,那么直接抛出该异常。