spring 对Java 传统RMI 的支持。
首先创建一个接口和一个实现类
接口源代码
package net.oschina.rpc;
public interface Service {
int add(int x, int y);
}
实现类源代码
package net.oschina.rpc;
public class ServiceImp implements Service {
public int add(int x, int y) {
return x+y;
}
}
然后分别创建 server 端和client 端的配置文件。
server 端配置文件
?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="service" />
<property name="service" ref="service" />
<property name="serviceInterface" value="net.oschina.rpc.Service" />
<property name="registryPort" value="8080" />
<property name="servicePort" value="8088" />
</bean>
<bean id="service" class="net.oschina.rpc.ServiceImp" />
</beans>
客户端配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="testService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://192.168.31.177:9090/service" />
<property name="serviceInterface" value="net.oschina.rpc.Service" />
</bean>
</beans>
启动 server 源代码
package net.oschina.rpc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiServer {
public static void main(String[] args) throws InterruptedException {
ApplicationContext context = new ClassPathXmlApplicationContext("rmi-server.xml");
}
}
客户端调用
package net.oschina.rpc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("rmi-client.xml");
Service client = context.getBean(Service.class);
System.out.println(client.add(5, 8));
}
}
正确绑定会看到日志:
[springModel][INFO] [2017-06-07 00:42:58] org.springframework.remoting.rmi.RmiServiceExporter.getRegistry(394) | Looking for RMI registry at port ‘9090’
[springModel][INFO] [2017-06-07 00:42:59] org.springframework.remoting.rmi.RmiServiceExporter.getRegistry(405) | Could not detect RMI registry - creating new one
[springModel][INFO] [2017-06-07 00:42:59] org.springframework.remoting.rmi.RmiServiceExporter.prepare(277) | Binding service ‘service’ to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:192.168.31.177:9090,objID:[0:0:0, 0]]]]
spring 简化了 RMI 服务的发布,可以很容易的发布RMI服务。