天天看点

解决分布式服务技术方案----单点故障及负载均衡处理

spring有比较好的远程服务解决方案,但其静态导入方案,只能使用一个server端,在大型分布式系统中无法很好的解决负载均衡及单点故障问题。某一个server放生故障,client就会受到影响。

    为解决该问题,cluster4spring应运而生,提供一个很好的解决方案。一个client端可以同时链接n个服务节点,其中某一个服务节点发生故障不会影响整个系统运行。

    目前0.85版本,仅支持rmi远程协议,后续预计会继续支持很多其他协议。

使用很简单,只需xml配置即可:

Client端配置:

<bean name="REMOTE_SERVICE_CLIENT" class="org.softamis.cluster4spring.rmi.RmiUrlListProxyFactoryBean"
        abstract="true">
    <property name="refreshEndpointsOnConnectFailure" value="true"/>
    <property name="refreshEndpointsOnStartup" value="true"/>
    <property name="registerTraceInterceptor" value="true"/>
    <property name="switchEndpointOnFailure" value="true"/>
    <property name="interceptorNames" value="_TestClientLoggingInterceptor, _TestClientEmptyInterceptor"/>
    <property name="endpointFactory" ref="_RmiEndpointFactory"/>
    <property name="endpointSelectionPolicy" ref="_endpointSelectionPolicy"/>
  </bean>
<bean name="TestService1" parent="REMOTE_SERVICE_CLIENT">
    <property name="serviceInterface"              value="org.softamis.cluster4spring.examples.TestServiceImpl"/>
    <property name="serviceURLs">
      <list>
        <value>rmi://server1:1199/TestService</value>
        <value>rmi://server2:1199/TestService</value>
        <value>rmi://server3:1199/TestService</value>
      </list>
    </property>
  </bean>
Server端:
<bean id="_RMIRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
    <property name="port" value="1099"/>
  </bean>

  <bean name="_RemoteInvcationExecutor" class="org.springframework.remoting.support.DefaultRemoteInvocationExecutor"/>
  <bean name="_RMIClientSocketsFactory" class="java.rmi.server.RMIClientSocketFactory"/>
  <bean name="TestService.bean" class="org.softamis.cluster4spring.examples.TestServiceImpl"/>

  <bean name="TestService.custom.verbose" class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="clientSocketFactory" ref="_RMIClientSocketsFactory"/>
    <property name="registerTraceInterceptor" value="true"/>
    <property name="registry" ref="_RMIRegistry"/>
    <property name="remoteInvocationExecutor" ref="_RemoteInvocationExecutor"/>
    <property name="service" ref="TestService.bean"/>
    <property name="serviceInterface" value="org.softamis.cluster4spring.examples.TestServiceImpl"/>
    <property name="serviceName" value="TestService"/>
  </bean>
           

Server端可以直接使用spring默认配置,或者使用上述方案,cluster4spring会自动判断处理。

具体的参数会在稍后给出,或请参考官网http://www.soft-amis.com/index.html