Spring Rmi配置

现在远程调用一般用RPC,webservice或者Rmi,而目前用的比较多的是webservice和Rmi。

webservice和rmi的最主要的区别,rmi的客户端和服务端都必须是java,webservice没有这个限制,webservice是在http协议上传递xml文本文件。与语言和平台无关,rmi是在tcp协议上传递可序列化的java对象,只能用在java虚拟机上,绑定语言。 RMI是EJB远程调用的基础,仅用RMI技术就可以实现远程调用,使用EJB是为了实现组件,事物,资源池,集群等功能.WebService是通过XML来传输数据,可用http等协议因此可在异构系统间传递,并且可以穿过防火墙,可在公网上远程调用。

以下是spring中配置Rmi的一个简单例子:

1.首先在src下建立一个接口(本人的是com.shinelife.inter):

package com.shinelife.inter;

public interface IBaseServiceRmi {
    public String SayHelloRmi(String name);
}

2.然后实现接口的方法:

package com.shinelife.services;

import com.shinelife.inter.IBaseServiceRmi;

public class BaseServiceRmi implements IBaseServiceRmi {

  public String SayHelloRmi(String name) {
          // TODO Auto-generated method stub
          return "Hello: "+name;
     }

}

3.然后就是配置remote.xml:

在src下面新建一个xml文件然后复制添加以下内容(注:头文件的对象配置很重要,配置不当会报错):

<?xml version="1.0" encoding="UTF-8"?>
<!-- spring 远程服务对象配置-->
<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"
    xmlns:flex="http://www.springframework.org/schema/flex"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

   <!-- 远程对象暴露  开始 -->
    
        <bean id="BaseServiceRmiExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
         <!-- 调用Service -->  
        <property name="service">
            <ref bean="BaseServiceRmi" />
        </property>
        <!-- 客户端调用时使用的名字 -->
        <!-- value值是给用户调用 -->  
        <property name="serviceName">
            <value>BaseServiceRmi</value>
        </property>
        <!-- service 接口 -->
        <property name="serviceInterface">
            <value>com.shinelife.inter.IBaseServiceRmi
            </value>
        </property>
        <!-- 注册端口号 -->  
        <property name="registryPort">
            <value>6100</value>
        </property>
        <property name="servicePort">
            <value>7100</value>
        </property>
    </bean>
  <!-- 远程对象暴露  结束 -->
</beans>

4.本人以上的例子是自己建立的一个remote.xml文件,而一般都是直接把这些配置到spring的配置文件applicationContext.xml里,而这种在这里就不研究了。主要说说外部配置Rmi的方式。

光是添加了一个remote.xml是不够的,还需要在spring的applicationContext.xml里面注入:

<bean id="BaseServiceRmi" class="com.shinelife.services.BaseServiceRmi"></bean>

这样spring才能找到并加载这个bean。

5.由于本人的是webservice项目,所以启动服务是用的配置web.xml,也可以用main入口函数加载,对于main加载,就不讲述了,这里讲述web.xml配置,以下是web.xml的部分内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.5"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/classes/applicationContext.xml,classpath:remote.xml
       </param-value>
    </context-param>

</web-app>

以上是服务端方面的配置,下面是客户端:

6.将com.shinelife.inter整个包拷贝到客户端下(注意服务端与客户端的包名保持一致)

7.在客户端的src下面新建一个Rmi.xml的文件,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
  "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    
    <bean id="BaseServiceRmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">        
         <!-- BaseService是调用服务端serviceName的value -->  
        <property name="serviceUrl">
            <value>rmi://127.0.0.1:6100/BaseServiceRmi</value>
        </property>
         <!-- service接口 -->  
        <property name="serviceInterface">
            <value>com.shinelife.inter.IBaseServiceRmi</value>
        </property>
    </bean>
</beans>

8.然后建立一个main函数测试Rmi服务:

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import com.shinelife.inter.IBaseServiceRmi;

public class testRmi {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:Rmi.xml");  
        IBaseServiceRmi baseService = (IBaseServiceRmi) context.getBean("BaseServiceRmi");
        System.out.println( baseService.SayHelloRmi("Joker"));
    }
}

9.测试结果:Hello: Joker

10.总结:Rmi是一个比较高效的远程调用方式,可以将整个函数方法作为参数进行远程调用,达到通信的目的,利用Rmi也可以减少很多的工作量。当然,在其他方面本人就不做结论,本人这也是分享一下搭建Rmi的方法。以便以后学习。

原文地址:https://www.cnblogs.com/jokerpan/p/3559478.html