2

环境:Spring 框架 3.2.3.RELEASE

有没有办法RmiProxyFactoryBean在直接的弹簧上下文配置中实现故障转移或任何其他方法?

我将 ServerA 作为主要服务器,将 ServerB 作为备用 RMI 服务器。如果与 ServerA 的连接断开,则 RMI 调用应定向到 ServerB。

4

1 回答 1

1

我已经覆盖了实现此故障转移切换的方法refreshAndRetrylookupStubRMIProxyFactoryBean

@Override
    protected Object refreshAndRetry(MethodInvocation invocation)   throws Throwable {

        if (logger.isDebugEnabled()) {
            logger.debug("Refreshing & retrying remote invocation.");
        }

        swapHitURLs();//Swap primary<>secondary URL

        return super.refreshAndRetry(invocation);
    }

@Override
    protected Remote lookupStub() throws RemoteLookupFailureException {
        Remote stub=null;

        if (logger.isDebugEnabled()) {
            logger.debug("Looking up the stub.");
        }

        try{
            stub=super.lookupStub();
        }catch(RemoteLookupFailureException rlfx){
            swapHitURLs();//Swap primary<>secondary URL
            if (logger.isDebugEnabled()) {
                logger.debug("Looking up the stub for swapped URL.");
            }
            stub=super.lookupStub();
        }

        return stub;
    }

配置 XML:

<bean id="beanName" class="com.ahamed.poc.rmi.FailOverRMIProxyFactory" lazy-init="true">
    <property name="serviceUrl" value="PrimaryURL"/>
    <property name="alternativeServiceUrl" value="SecondaryURL"/>
    <property name="serviceInterface" value="com.ahamed.poc.rmi.InterfaceClass"/>
</bean> 

如果有更好的选择,请告诉我。

于 2013-12-29T11:41:59.070 回答