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>
时间: 2024-08-05 22:42:11

spring RMI的使用的相关文章

Spring+Rmi中的客户端自动重连配置

<?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="xxx" class="org.s

使用Spring RMI调用远程方法

Spring有多种调用远程的方式,今天学习了一下远程方法调用(RMI). RMI需要服务端和客户端 我们先从服务器开始 我的代码结构 package rmi; public interface ServerRmiI { public String sayHi(String name); } package rmi; public class ServerRmiImpl implements ServerRmiI{ public String sayHi(String name) { return

Spring+RMI集成实现远程访问分布式应用

下面是个人感觉比较清晰易懂的spring实现RMI的网络帖子: 使用Spring对RMI的支持,可以非常容易地构建你的分布式应用.在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务:在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便.这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端

Spring RMI (Spring 远程方法调用)

所需jar包...?    不纠结,一股脑儿全导! 先放结构图如下,客户端和服务端都在一个项目中.也可以把服务端的xxx导成jar包放到新的客户端项目中去. 1. 服务端 2.

Spring Remoting: Remote Method Invocation (RMI)--转

原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring provides four ways to develop remote services. Remote services are services hosted on remote servers and accessed by clients over the network. For examp

JAVA RMI技术及其spring封装的使用

java RMI即java远程服务调用,用于各个子系统之间的服务调用.最近应用到了JAVA RMI技术,所以总结一下RMI的使用,另外EJB的实现也是以Java RMI为基础核心的.RMI远程方法调用,可以跨机器通过网络调用,不过Java RMI只支持两边都是Java程序,如果要实现完全两边异构,那么就要用到传说中的Web Service了.为了看好效果,都要建立两个或两个以上工程,当然,如果你有两个机器并通过局域网相连更好,如果能同有互联网的条件就更好了,以下是同一机器不同工程的实验. Jav

几种java通信(rmi,http,hessian,webservice)协议性能比较

一.综述 本文比较了RMI,Hessian,Burlap,Httpinvoker,web service等5种通讯协议的在不同的数据结构和不同数据量时的传输性能.RMI是java语言本身提供的通讯协议,稳定高效,是EJB的基础.但它只能用于JAVA程序之间的通讯.Hessian和Burlap是caucho公司提供的开源协议,基于HTTP传输,服务端不用开防火墙端口.协议的规范公开,可以用于任意语言.Httpinvoker是SpringFramework提供的远程通讯协议,只能用于JAVA程序间的

WebService与RMI(远程调用方式实现系统间通信)

前言 本文是<分布式java应用基础与实践>读书笔记:另外参考了此博客,感觉讲的挺好的,尤其是其中如下内容: 另外,消息方式实现系统间通信本文不涉及.RMI则只采用spring RMI框架来实现效果,更多的则是来讲讲webService及效果. RMI(Remote Method Invocation) ----> spring RMI(配置及实现直接参考spring文档,已经很详细了) spring RMI工作原理图如下: RMI代码结构图: 服务端代码 接口Business.java

rmi简单用例,简单有效

Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方法调用的任何对象必须实现该远程接口.使用spring对RMI的支持,可以非常容易地构建分布式应用.这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端Skeleton和客户端Stub等的处理细节,这些对于服务开发和服务使用的人员来说,都是透明的,无需过度关注,而集中精力开发你的商业逻辑.