Java&Spring基础技术Java学习笔记JAVA

Spring中的RMI

2017-07-01  本文已影响157人  大华夏

服务端

通过org.springframework.remoting.rmi.RmiServiceExporter来暴露接口,供远程来调用

<?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="HelloService" />
        <!--服务实现类引用-->
        <property name="service" ref="helloServiceImpl" />
        <!--暴露的服务接口-->
        <property name="serviceInterface"
            value="com.study.remoting.IHelloService" />
        <property name="registryPort" value="8080" />
        <property name="servicePort" value="8088" />
    </bean>
    <!--服务实现类对象-->
    <bean id="helloServiceImpl" class="com.study.remoting.HelloServiceImpl" />

</beans>

客户端

通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务

<?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="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <!--引用RMI服务的地址-->
        <property name="serviceUrl" value="rmi://192.168.1.103:8080/HelloService" />
        <!--服务端暴露的接口类-->
        <property name="serviceInterface"
            value="com.study.remoting.IHelloService" />
    </bean>

</beans>

使用spring对RMI的支持,可以非常容易地构建你的分布式应用!!

上一篇 下一篇

猜你喜欢

热点阅读