spring 对java RMI 的支持

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服务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值