前言
本文是《分布式java应用基础与实践》读书笔记;另外参考了 此博客 ,感觉讲的挺好的,尤其是其中如下内容:
另外,消息方式实现系统间通信本文不涉及。RMI则只采用spring RMI框架来实现效果,更多的则是来讲讲webService及效果。
RMI (Remote Method Invocation) ----》 spring RMI(配置及实现直接参考spring文档,已经很详细了)
spring RMI工作原理图如下:
RMI代码结构图:
服务端代码
接口Business.java:
package com.rmi.server; public interface Business { /** * 显示客户端提供的消息,并返回 * @param message * @return */ public String echo(String message); } View Code
接口实现类BusinessImpl.java:
package com.rmi.server; public class BusinessImpl implements Business { @Override public String echo(String message) { if("quit".equalsIgnoreCase(message.toString())){ System.out.println("Server will be shutdown!"); System.exit(0);; } System.out.println("Message from client:" + message); return "Server response:" + message; } } View Code
spring RMI配置文件spring-server.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="businessService" class="com.rmi.server.BusinessImpl"/> <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="businessService"/> <property name="serviceName" value="BusinessService"/&g