下面是我的感受比較清晰易懂的spring實現RMI的網絡帖子:html
使用Spring對RMI的支持,能夠很是容易地構建你的分佈式應用。在服務端,能夠經過Spring的org.springframework.remoting.rmi.RmiServiceExporter能夠暴露你的服務;在客戶端,經過org.springframework.remoting.rmi.RmiProxyFactoryBean能夠使用服務端暴露的服務,很是方便。這種C/S模型的訪問方式,能夠屏蔽掉RMI自己的複雜性,如服務端Skeleton和客戶端Stub等的處理細節,這些對於服務開發和服務使用的人員來講,都是透明的,無需過分關注,而集中精力開發你的商業邏輯。java
下面經過一個例子,說明如何經過Spring集成RMI。spring
服務端發佈服務apache
咱們定義了服務接口,服務端實現該服務接口來完成其複雜的邏輯,客戶端能夠經過該接口調用服務端暴露的服務,以下所示:網絡
[java] view plaincopyapp
package org.shirdrn.spring.remote.rmi; 分佈式
public interface AccountService { ide
int queryBalance(String mobileNo); oop
String shoopingPayment(String mobileNo, byte protocol); url
}
服務實現,示例以下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
public class MobileAccountServiceImpl implements AccountService {
private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);
public int queryBalance(String mobileNo) {
if (mobileNo != null)
return 100;
return 0;
}
public String shoopingPayment(String mobileNo, byte protocol) {
StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append(
mobileNo).append("/", protocol type is /"").append(protocol)
.append("/".");
LOG.info("Message is: " + sb.toString());
return sb.toString();
}
}
服務端發佈服務,供客戶端進行(遠程方法)調用,Spring配置server.xml以下所示:
[xhtml] view plaincopy
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="MobileAccountService" />
<property name="service" ref="accountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
<property name="registryPort" value="8080" />
<property name="servicePort" value="8088" />
</bean>
<bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" />
</beans>
上面配置,指定了暴露的服務的名稱,經過serviceName屬性注入到RmiServiceExporter中,服務名稱爲MobileAccountService,客戶端經過該服務名稱就可以進行調用。
下面啓動服務端,發佈服務,以下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiServer {
public static void main(String[] args) throws InterruptedException {
new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");
Object lock = new Object();
synchronized (lock) {
lock.wait();
}
}
}
客戶端調用服務
客戶端配置client.xml以下所示:
[xhtml] view plaincopy
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
</bean>
</beans>
配置中,將一個serviceUrl和serviceInterface注入給RmiProxyFactoryBean,便可進行遠程方法調用。調用示例以下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiClient {
private static final Logger LOG = Logger.getLogger(RmiClient.class);
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"org/shirdrn/spring/remote/rmi/client.xml");
AccountService accountService = (AccountService) ctx
.getBean("mobileAccountService");
String result = accountService.shoopingPayment("13800138000", (byte) 5);
LOG.info(result);
}
}
可見,實現遠程訪問變得很是容易。