Spring RMI (Spring 远程方法调用)

时间:2022-03-31 20:46:41

所需jar包...?    不纠结,一股脑儿全导! 源码地址:http://pan.baidu.com/s/1jG8eOmy

先放结构图如下,客户端和服务端都在一个项目中.也可以把服务端的xxx导成jar包放到新的客户端项目中去.

Spring RMI (Spring 远程方法调用)

其中绿色部分为服务器端代码, 红色部分为客户端代码.

现在是服务/客户端全整到一块去的项目,如果要把客户端独立出来,可以把绿色服务器端代码导成jar包放到单独的客户端项目中,用Client.java进行调用.

Client.java:                              客户端入口调用类

Car.java:                                 POJO类(C/S共用)

IKeyboard.java:                       RMI 接口类(C/S共用)

Keyboard.java:                        RMI实现类

IVehicle.java:                           RMI接口类(C/S共用)

Vehicl:                                    RMI实现类

remote-client-local.xml:             客户端spring配置文件

remote-servlet.xml:                  服务端spring配置文件

1. 服务端

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringRMI</display-name>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/remote-servlet.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

remote-servlet.xml

<?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="vehicle" class="com.king.code.service.invoke.Vehicle"/>
<bean name="/vehicle" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="vehicle" />
<property name="serviceInterface" value="com.king.code.service.invoke.IVehicle" />
</bean> <!-- 键盘 接口-->
<bean id="keyboard" class="com.king.code.service.invoke.Keyboard"/>
<bean name="/keyboard" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="keyboard" />
<property name="serviceInterface" value="com.king.code.service.invoke.IKeyboard" />
</bean>
</beans>

Car.java

package com.king.code.service.invoke;

import java.io.Serializable;

public class Car implements Serializable{
/**
* 车
*/
private static final long serialVersionUID = 1883838732853579826L; String id="6";
String model ="宝马M6";
String speed ="320公里/小时";
String result ="未检测"; public Car() {
super();
} public Car(String id, String model, String speed, String result) {
super();
this.id = id;
this.model = model;
this.speed = speed;
this.result = result;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getModel() {
return model;
} public void setModel(String model) {
this.model = model;
} public String getSpeed() {
return speed;
} public void setSpeed(String speed) {
this.speed = speed;
} public String getResult() {
return result;
} public void setResult(String result) {
this.result = result;
} @Override
public String toString() {
return "Car [id=" + id + ", model=" + model + ", speed=" + speed
+ ", result=" + result + "]";
} }

IKeyboard.java

package com.king.code.service.invoke;
/**
* 键盘服务接口
* @author King
*
*/
public interface IKeyboard {
public String invoke(String requestInfo );
}

Keyboard.java

package com.king.code.service.invoke;
/**
* 键盘服务实现类
* @author King
*
*/
public class Keyboard implements IKeyboard {
@Override
public String invoke(String requestInfo){
System.out.println("press------");
System.out.println(requestInfo);
System.out.println("leave------");
return "success";
}
}

IVehicle.java

package com.king.code.service.invoke;

/**
* 交通公具服务接口
* @author King
*
*/
public interface IVehicle {
/**
* 检测该车是否合格
* @param car
* @return
*/
public Car detect(Car car );
}

Vehicle.java

package com.king.code.service.invoke;

/**
* 交通公具服务实现类
* @author King
*
*/
public class Vehicle implements IVehicle {
@Override
public Car detect(Car car){
System.out.println("start detect------");
System.out.println("该车信息"+car);
car.setResult("99% is ok");
System.out.println("end detect------");
return car;
} }

2. 客户端

Client.java

package com.king.code.client.invoke;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.king.code.service.invoke.Car;
import com.king.code.service.invoke.IKeyboard;
import com.king.code.service.invoke.IVehicle;
/**
* 客户端调用类
* @author King
*
*/
public class Client {
//读取配置文件
static ApplicationContext context = new ClassPathXmlApplicationContext("remote-client-local.xml"); public static void main(String[] args) {
new Client().invokeKeyboard();
new Client().invokeVehicle();
} public void invokeKeyboard() {
IKeyboard service = (IKeyboard) context.getBean("keyboard");
String requestInfo = "i'm logitech keyboard,press twice";
String responseInfo = service.invoke(requestInfo);
System.out.println(responseInfo);
} public void invokeVehicle() {
IVehicle service = (IVehicle) context.getBean("vehicle");
Car request = new Car();
System.out.println("该车状态(检测前):"+request.getResult());
Car response = service.detect(request);
System.out.println("该车状态(检测后):"+response.getResult());
} }

remote-client-local.xml

<?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="vehicle"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8080/SpringRMI/vehicle</value>
</property>
<property name="serviceInterface">
<value>com.king.code.service.invoke.IVehicle</value>
</property>
</bean> <!-- 键盘接口 -->
<bean id="keyboard"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8080/SpringRMI/keyboard</value>
</property>
<property name="serviceInterface">
<value>com.king.code.service.invoke.IKeyboard</value>
</property>
</bean> </beans>

客户端打印:

success
该车状态(检测前):未检测
该车状态(检测后):99% is ok

服务端打印:

press------
i'm logitech keyboard,press twice
leave------
start detect------
该车信息Car [id=6, model=宝马M6, speed=320公里/小时, result=未检测]
end detect------