Spring实现RMI远程方法调用

RMI(Remote Method Invocation,远程方法调用)它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法。JVM 可以位于相同或不同计算机上,在多个 JVM 中,一个 JVM 可以调用存储在其它 JVM 的对象的方法。

实现RMI有两种方式,即通过Java和Spring,相比使用Java实现RMI远程方法调用,使用 Spring实现RMI远程方法调用的RMI,提供的服务简单方便,不用继承接口和指定的类,不用抛出异常。

Spring实现RMI远程方法调用

1.使用idea新建一个Maven项目,项目下新建一个model
这里写图片描述
2.api模块下src—>main—>java创建service包,声明服务器接口

package com.shsxt.service;

public interface IHelloService {
    public String sayHello(String msg);
}

3.项目下新建spring_servlet模块,添加api和spring-context依赖

<dependency>
    <groupId>com.shsxt</groupId>
    <artifactId>api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>

src—>main—>java创建service包,实现接口

package com.shsxt.service;

import org.springframework.stereotype.Service;


@Service
public class HelloServiceImpl implements IHelloService {
    @Override
    public String sayHello(String msg) {
        System.out.println("服务器端收到信息: "+msg);
        return "hello:"+msg;
    }
}

src—>main新建resources文件夹,并标记目录为资源文件夹,新建spring.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/springcontext.xsd">
    <!-- 定义扫描范围 -->
    <context:component-scan basepackage="com.shsxt"></context:component-scan>
    <!-- 加入 rmi 相关配置 -->
    <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
        <property name="serviceName" value="hello"></property>
        <property name="service" ref="helloServiceImpl"></property>
        <property name="serviceInterface"
                  value="com.shsxt.service.IHelloService"></property>
        <property name="registryPort" value="1199"></property>
    </bean>
    <bean id="helloServiceImpl" class="java.lang.Object"/>
</beans>

对外发布远程服务

package com.shsxt;

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

public class Publist {
    public static void main (String[] args) {
/**
 * 启动 spring ioc 容器 , 并发布对应服务
 */
    ApplicationContext ac = new ClassPathXmlApplicationContext ("spring.xml");
    }
}

4.项目下新建spring_client模块,添加api和spring-context依赖

<dependency>
    <groupId>com.shsxt</groupId>
    <artifactId>api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>

src—>main—>java创建service包,声明接口

package com.shsxt.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private IHelloService helloService;
    public void test(){
        helloService.sayHello ("I'm hero!!!");
    }
}

创建测试类

package com.shsxt.service;

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


public class UserServiceTest {
    @Test
    public void test1 () throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext ("spring.xml");
        UserService userService = (UserService) context.getBean ("userService");

        userService.test ();
    }
}

src—>main新建resources文件夹,并标记目录为资源文件夹,新建spring.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/springcontext.xsd">
    <!-- 定义扫描范围 -->
    <context:component-scan basepackage="com.shsxt"></context:component-scan>

    <bean class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl"
                  value="rmi://localhost:1199/hello"></property>
        <property name="serviceInterface"
                  value="com.shsxt.service.IHelloService"></property>
    </bean>
</beans>

运行test1
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值