Spring框架实现——远程方法调用RMI代码演示

Posted 戒急静心

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring框架实现——远程方法调用RMI代码演示相关的知识,希望对你有一定的参考价值。

1、spring_RMI02_server服务端02

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4      xmlns:context="http://www.springframework.org/schema/context"
 5      xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"  
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10         http://www.springframework.org/schema/context 
11         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop.xsd
14         http://www.springframework.org/schema/tx 
15         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
16         
17     <!-- 扫描包基础目录 -->
18     <context:component-scan base-package="com.wisezone"></context:component-scan>
19     
20     <!-- 配置RMI -->
21     <bean class="org.springframework.remoting.rmi.RmiServiceExporter">  
22         <property name="serviceName" value="hello"></property>
23         <property name="service" ref="helloServiceImpl"></property>
24         <property name="serviceInterface" value="com.wisezone.service.IHelloService"></property>
25         <property name="registryPort" value="1199"></property>
26     </bean>    
27 </beans>
1 package com.wisezone.service;
2 
3 public interface IHelloService {
4     
5     public String sayHello(String msg);
6 }
 1 package com.wisezone.service.impl;
 2 
 3 import org.springframework.stereotype.Service;
 4 
 5 import com.wisezone.service.IHelloService;
 6 
 7 @Service
 8 public class HelloServiceImpl implements IHelloService {
 9 
10     @Override
11     public String sayHello(String msg) {
12         
13         System.out.println("服务端接受消息:"+msg);
14         return "hello-->>"+msg;
15     }
16 
17 }
 1 package com.wisezone.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Publish {
 7     
 8     public static void main(String[] args) {
 9         
10         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
11     }
12 }

2、spring_RMI02_client客户端02

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4      xmlns:context="http://www.springframework.org/schema/context"
 5      xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"  
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10         http://www.springframework.org/schema/context 
11         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop.xsd
14         http://www.springframework.org/schema/tx 
15         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
16         
17     <!-- 扫描包基础目录 -->
18     <context:component-scan base-package="com.wisezone"></context:component-scan>
19     
20     <bean class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
21         <property name="serviceUrl" value="rmi://localhost:1199/hello"></property>
22         <property name="serviceInterface" value="com.wisezone.service.IHelloService"></property>
23     </bean>
24     
25 </beans>
1 package com.wisezone.service;
2 
3 public interface IHelloService {
4 
5     public String sayHello(String msg);
6 }
 1 package com.wisezone.impl;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.springframework.stereotype.Service;
 6 
 7 import com.wisezone.service.IHelloService;
 8 
 9 @Service
10 public class UserServiceImpl {
11 
12     @Resource
13     private IHelloService helloService;
14     
15     public void test(){
16         System.out.println("客户端发送消息:"+helloService.sayHello("im window7SP1旗舰版"));
17     }
18     
19 }
 1 package com.wisezone.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.wisezone.impl.UserServiceImpl;
 7 
 8 public class Test {
 9     public static void main(String[] args) {
10         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
11         UserServiceImpl userServiceImpl = (UserServiceImpl) ac.getBean("userServiceImpl");
12         userServiceImpl.test();
13     }
14 }

发布顺序:先发布服务端,再发布客户端

结果:

 

以上是关于Spring框架实现——远程方法调用RMI代码演示的主要内容,如果未能解决你的问题,请参考以下文章

使用 RMI + ZooKeeper 实现远程调用框架

ZooKeeper伪分布集群安装及使用 RMI+ZooKeeper实现远程调用框架

Spring 配置RMI远程调用

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

4_分布式通信框架RMI

RMI的实现