spring RMI的使用

Spring整合RMI的原理

客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。

通过JRMP访问服务。JRMP JRMP:java remote method protocol,Java特有的,基于流的协议。

服务端暴露远程服务

RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。

RMI服务端实现类

package com.impl;

import com.interfaces.IHelloWord;

/**
 * Created by lunhui.wei on 2014/11/7.
 */
public class HelloWorld implements IHelloWord{

    @Override
    public String helloWorld() {
        return "Hello World";
    }

    @Override
    public String sayHelloToSomeBody(String name) {
        return name+" say:"+" Hello world";
    }
}

服务端RMI接口类

public interface IHelloWord {
    public String helloWorld();
    public String sayHelloToSomeBody(String  name);
}

服务端运行类

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by lunhui.wei on 2014/11/7.
 */
public class Run {
    public static void main(String args[]){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
        System.out.println("RMI服务伴随Spring的启动而启动了.....");
    }
}

服务端spring-config.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="helloWorld" class="com.impl.HelloWorld"/>
    <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
        <property name="service" ref="helloWorld"/>
        <property name="serviceName" value="hello"/>
        <property name="serviceInterface" value="com.interfaces.IHelloWord"/>
        <property name="registryPort" value="8088"/>
     </bean>

</beans>

客户端接口类直接使用服务端的接口类直接粘贴过去。

客户端的运行类:

import com.interfaces.IHelloWord;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by lunhui.wei on 2014/11/7.
 */
public class Run {
    public static void main(String args[]){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
        IHelloWord helloWord= (IHelloWord) applicationContext.getBean("helloWorld");
        System.out.println(helloWord.helloWorld());
        System.out.println(helloWord.sayHelloToSomeBody("zhangsan"));
    }
}

客户端Spring 的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl" value="rmi://10.61.5.14:8088/hello"/>
        <property name="serviceInterface" value="com.interfaces.IHelloWord"/>
    </bean>
</beans>
原文地址:https://www.cnblogs.com/weilunhui/p/4080945.html