rmi需要建立两个项目,一个是服务端的项目,一个是客户端的项目.服务端项目启动后,再启动客户端项目去调用服务端的方法.

我们建立两个maven项目:
pom.xml配置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.xiejl</groupId>
  7. <artifactId>test</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <spring-version>4.3.7.RELEASE</spring-version>
  11. </properties>
  12. <dependencies>
  13. <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
  14. <dependency>
  15. <groupId>org.springframework</groupId>
  16. <artifactId>spring-beans</artifactId>
  17. <version>${spring-version}</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-context</artifactId>
  22. <version>${spring-version}</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-context-support</artifactId>
  27. <version>${spring-version}</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-core</artifactId>
  32. <version>${spring-version}</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-aop</artifactId>
  37. <version>${spring-version}</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-aop</artifactId>
  42. <version>${spring-version}</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-orm</artifactId>
  47. <version>${spring-version}</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-orm</artifactId>
  52. <version>${spring-version}</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework</groupId>
  56. <artifactId>spring-web</artifactId>
  57. <version>${spring-version}</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework</groupId>
  61. <artifactId>spring-webmvc</artifactId>
  62. <version>${spring-version}</version>
  63. </dependency>
  64. <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
  65. <dependency>
  66. <groupId>org.aspectj</groupId>
  67. <artifactId>aspectjrt</artifactId>
  68. <version>1.8.10</version>
  69. </dependency>
  70. <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
  71. <dependency>
  72. <groupId>org.aspectj</groupId>
  73. <artifactId>aspectjweaver</artifactId>
  74. <version>1.8.10</version>
  75. </dependency>
  76. </dependencies>
  77. </project>

第一个是服务端的maven项目:
服务接口,和以前不一样了,不用实现远程接口了。HelloService:
  1. package com.xjl456852.rmi.spring;
  2. /**
  3. * 定义一个远程接口
  4. *
  5. * @author leizhimin 2009-8-17 13:53:38
  6. */
  7. public interface HelloService {
  8. /**
  9. * 简单的返回“Hello World!"字样
  10. *
  11. * @return 返回“Hello World!"字样
  12. */
  13. public String helloWorld();
  14. /**
  15. * 一个简单的业务方法,根据传入的人名返回相应的问候语
  16. *
  17. * @param someBodyName 人名
  18. * @return 返回相应的问候语
  19. */
  20. public String sayHelloToSomeBody(String someBodyName);
  21. }
服务实现类,加入了注解:
  1. package com.xjl456852.rmi.spring;
  2. import org.springframework.stereotype.Service;
  3. /**
  4. * 远程的接口的实现
  5. *
  6. * @author leizhimin 2009-8-17 13:54:38
  7. */
  8. @Service
  9. public class HelloServiceImpl implements HelloService {
  10. public HelloServiceImpl() {
  11. }
  12. /**
  13. * 简单的返回“Hello World!"字样
  14. *
  15. * @return 返回“Hello World!"字样
  16. */
  17. public String helloWorld() {
  18. return "Hello World!";
  19. }
  20. /**
  21. * 一个简单的业务方法,根据传入的人名返回相应的问候语
  22. *
  23. * @param someBodyName 人名
  24. * @return 返回相应的问候语
  25. */
  26. public String sayHelloToSomeBody(String someBodyName) {
  27. return "你好," + someBodyName + "!";
  28. }
  29. }
服务端启动程序:
  1. package com.xjl456852.rmi.spring;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. /**
  5. * 通过Spring发布RMI服务
  6. *
  7. * @author leizhimin 2009-8-17 14:22:06
  8. */
  9. public class HelloHost {
  10. public static void main(String[] args) {
  11. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  12. System.out.println("RMI服务伴随Spring的启动而启动了.....");
  13. }
  14. }

Spring 配置: applicationContext.xml:
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
  11. <!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
  12. <context:component-scan base-package="com.xjl456852" />
  13. <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
  14. <property name="serviceName" value="rmiSpring"/>
  15. <property name="serviceInterface" value="com.xjl456852.rmi.spring.HelloService"/>
  16. <property name="registryPort" value="8888"/>
  17. <property name="service" ref="helloServiceImpl"/>
  18. </bean>
  19. </beans>
启动服务端程序,HelloHost:
 

第二个是客户端的maven项目:
客户端调用有两种方式,一种是使用Spring,一种不使用,这里仅介绍使用Spring的情况。
客户端需要依赖服务端的 HelloService接口,需要将这个接口放入到客户端中.

客户端的启动类:
  1. package com.xjl456852.rmi.spring;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import javax.annotation.Resource;
  5. import java.rmi.RemoteException;
  6. /**
  7. * 通过Spring来调用RMI服务
  8. *
  9. * @author leizhimin 2009-8-17 14:12:46
  10. */
  11. public class HelloClient {
  12. @Resource
  13. private HelloService helloService;
  14. public static void main(String[] args) throws RemoteException {
  15. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  16. HelloService hs = (HelloService) ctx.getBean("helloService");
  17. System.out.println(hs.helloWorld());
  18. System.out.println(hs.sayHelloToSomeBody("xjl456852"));
  19. }
  20. }

在Spring中配置客户端要调用服务, applicationContext.xml:
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
  11. <!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
  12. <context:component-scan base-package="com.xjl456852" />
  13. <bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  14. <property name="serviceUrl" value="rmi://192.168.176.131:8888/rmiSpring"/>
  15. <property name="serviceInterface" value="com.xjl456852.rmi.spring.HelloService"/>
  16. </bean>
  17. </beans>


启动客户端,查看结果:
 

下面是服务端发布多个服务接口的配置:
假设服务端还有一个服务,叫OtherService:
  1. package com.xjl456852.rmi.spring;
  2. /**
  3. * Created by xjl on 2017/3/19.
  4. */
  5. public interface OtherService {
  6. public int random();
  7. }
服务端的实现类:
  1. package com.xjl456852.rmi.spring;
  2. import org.springframework.stereotype.Service;
  3. import java.util.Random;
  4. /**
  5. * Created by xjl on 2017/3/19.
  6. */
  7. @Service
  8. public class OtherServiceImpl implements OtherService{
  9. Random random = new Random();
  10. public int random() {
  11. System.out.println("invoke random method");
  12. return random.nextInt(100);
  13. }
  14. }
服务端的Spring配置:
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
  11. <!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
  12. <context:component-scan base-package="com.xjl456852" />
  13. <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
  14. <property name="serviceName" value="rmiSpring"/>
  15. <property name="serviceInterface" value="com.xjl456852.rmi.spring.HelloService"/>
  16. <property name="registryPort" value="8888"/>
  17. <property name="service" ref="helloServiceImpl"/>
  18. </bean>
  19. <bean id="serviceExporter_Other" class="org.springframework.remoting.rmi.RmiServiceExporter">
  20. <property name="serviceName" value="rmiSpringOther"/>
  21. <property name="serviceInterface" value="com.xjl456852.rmi.spring.OtherService"/>
  22. <property name="registryPort" value="8888"/>
  23. <property name="service" ref="otherServiceImpl"/>
  24. </bean>
  25. </beans>


下面是客户端的配置:
客户端需要依赖服务端的OtherService接口
然后客户端的Spring配置如下:
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
  11. <!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
  12. <context:component-scan base-package="com.xjl456852" />
  13. <bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  14. <property name="serviceUrl" value="rmi://192.168.176.131:8888/rmiSpring"/>
  15. <property name="serviceInterface" value="com.xjl456852.rmi.spring.HelloService"/>
  16. </bean>
  17. <bean id="otherService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  18. <property name="serviceUrl" value="rmi://192.168.176.131:8888/rmiSpringOther"/>
  19. <property name="serviceInterface" value="com.xjl456852.rmi.spring.OtherService"/>
  20. </bean>
  21. </beans>
客户端的测试类:
  1. package com.xjl456852.rmi.spring;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import javax.annotation.Resource;
  5. import java.rmi.RemoteException;
  6. /**
  7. * 通过Spring来调用RMI服务
  8. *
  9. * @author leizhimin 2009-8-17 14:12:46
  10. */
  11. public class HelloClient {
  12. @Resource
  13. private HelloService helloService;
  14. @Resource
  15. private OtherService otherService;
  16. public static void main(String[] args) throws RemoteException {
  17. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  18. HelloService hs = (HelloService) ctx.getBean("helloService");
  19. System.out.println(hs.helloWorld());
  20. System.out.println(hs.sayHelloToSomeBody("xjl456852"));
  21. OtherService os = (OtherService) ctx.getBean("otherService");
  22. System.out.println("otherService:" + os.random());
  23. }
  24. }
先启动服务端,然后再启动客户端进行调用.
服务端显示如下:
 
客户端显示如下:
 

感谢原作者:




Spring 4 整合RMI技术及发布多个服务(xjl456852原创)的更多相关文章

  1. 黑马_13 Spring Boot:05.spring boot 整合其他技术

    13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 05.spring boot 整合其他 ...

  2. Spring+CXF整合来管理webservice(服务器启动发布webservice)

    Spring+CXF整合来管理webservice    实现步骤:      1. 添加cxf.jar 包(集成了Spring.jar.servlet.jar ),spring.jar包 ,serv ...

  3. Spring Boot 整合视图层技术,application全局配置文件

    目录 Spring Boot 整合视图层技术 Spring Boot 整合jsp Spring Boot 整合freemarker Spring Boot 整合视图层技术 Spring Boot 整合 ...

  4. Spring 5.x 、Spring Boot 2.x 、Spring Cloud 与常用技术栈整合

    项目 GitHub 地址:https://github.com/heibaiying/spring-samples-for-all 版本说明: Spring: 5.1.3.RELEASE Spring ...

  5. Spring Boot从入门到精通之:二、Spring Boot整合JPA

    springboot-jpa 开发工具 系统: windows10 开发工具: Intellij IDEA 2018.2.6 springboot: 2.0.6.RELEASE jdk: 1.8.0_ ...

  6. Spring MVC & Boot & Cloud 技术教程汇总(长期更新)

    昨天我们发布了Java成神之路上的知识汇总,今天继续. Java成神之路技术整理(长期更新) 以下是Java技术栈微信公众号发布的关于 Spring/ Spring MVC/ Spring Boot/ ...

  7. spring boot 2.0(一)权威发布spring boot2.0

    Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...

  8. spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的。

    Spring jar包的描述:针对3.2.2以上版本 org.springframework spring-aop ——Spring的面向切面编程,提供AOP(面向切面编程)实现 org.spring ...

  9. WebService之Spring+CXF整合示例

    一.Spring+CXF整合示例 WebService是一种跨编程语言.跨操作系统平台的远程调用技术,它是指一个应用程序向外界暴露一个能通过Web调用的API接口,我们把调用这个WebService的 ...

随机推荐

  1. libpcap 中调用ctime()时警告提示:

    warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]    ...

  2. bzoj 4031: [HEOI2015]小Z的房间【矩阵树定理】

    是板子题,因为mod不是质数所以需要辗转相除然而并不知道为啥 高斯消元部分还不知道原理呢--先无脑背过的 #include<iostream> #include<cstdio> ...

  3. bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】

    这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便 先按起始时间从小到大排序. 我用的是greater重定义优先队列(小根堆).用pair存牛棚用完时间(first)和牛棚编号(second),每 ...

  4. random模块思维导图

  5. [GDOI2014]拯救莫莉斯

    题目描述 莫莉斯·乔是圣域里一个叱咤风云的人物,他凭借着自身超强的经济头脑,牢牢控制了圣域的石油市场. 圣域的地图可以看成是一个n*m的矩阵.每个整数坐标点(x , y)表示一座城市吗,两座城市间相邻 ...

  6. linux学习之路1 Linux系统安装

    VMware workstation虚拟器 网上下载VMware workstation,然后安装任一系统的linux系统,不过选的系统一定要跟你下载好的linux镜像保持一致,博主装的是Red Ha ...

  7. ACM_排序

    除了sort,你还会什么 Time Limit: 1000/500ms (Java/Others) Problem Description: 给出若干人的年龄(1~100之间的整数),把它们按照从小到 ...

  8. ibatis入门教程一

    这几天研究ibatis玩,参考一篇贴子进行安装配置:蓝雪森林 选择这个帖子来跟随配置是因为这个帖子看着比较干净,但是我仍旧在配置得过程中出现了好几个问题,所以我决定在这个帖子的基础上将更多细节加上,做 ...

  9. docloud后台管理项目(开篇)

    最近朋友做app需要web做后台管理,所以花了一周时间做了这个项目. 废话不多说,开发环境是nginx+php5.3,使用thinkphp框架.是一个医疗器械数据统计的后台,业务功能很简单就是查看用户 ...

  10. HDU_2112_最短路

    题目链接:http://acm.hdu.edu.cn/status.php?user=l1526789512&pid=2112&status=5 HDU Today Time Limi ...