一、简单介绍下:Spring HTTP invoker 是 spring 框架中的一个远程调用模型,执行基于 HTTP 的远程调用,具体实体类需要序
列化,如此才可以实现网络间对象的传递,以便客户端调用远程服务器上的对象。主要使用了三个重要对象:
HttpInvokerServiceExporter绑定接口及接口实现,由此可知spring需配置两个属性,serviceInterface及service。
HttpRequestHandlerServlet将客户端请求转给同名bean(即下面需要配置的服务名称)。
HttpInvokerProxyFactoryBean绑定服务端URL及服务端设定的接口,由此可知spring需配置两个属性,serviceUrl及
serviceInterface。废话不多说,看实现。
二、具体实现如下:
1、jar包准备:commons-logging-1.1.jar;spring-2.5.6.jar
2、服务端:
(1)新建个实体对象,实现serializable接口
package com.erayt.domain;
import java.io.Serializable;
public class User implements Serializable{
private String userName;
private String passWord;
private int age;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [userName=" + userName + ", passWord=" + passWord
+ ", age=" + age + "]";
}
}
(2)新建接口类
package com.erayt.service;
import com.erayt.domain.User;
public interface IUserHttpService {
public void addUser(User user);
}
(3)简单实现(2)中的接口
package com.erayt.service.impl;
import com.erayt.domain.User;
import com.erayt.service.IUserHttpService;
public class UserHttpServiceImpl implements IUserHttpService {
public void addUser(User user) {
System.out.println(user);
}
}
(4)配置spring,在Spring配置文件中声明一个HttpInvokerServiceExporter类的bean,共三部分
--服务名称(如userExporter)
--服务类型(如com.erayt.service.IUserHttpService )
--服务实现类,一般引用其它bean(如userHttpService)
<?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="userHttpService" class="com.erayt.service.impl.UserHttpServiceImpl"></bean>
<bean id="userExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="userHttpService"></property>
<property name="serviceInterface" value="com.erayt.service.IUserHttpService"></property>
</bean>
</beans>
(5)web.xml配置,在web.xml中声明一个与服务与服务名称同名的Servlet(当然这个Servlet类Spring已经提供即HttpRequestHandlerServlet,这家伙的作用就是直接把请求扔给同名的bean),然后声明servlet-mapping将其map到指定URL,这样客户就可以通过这个URL访问到对应服务。
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>JavaRemoteCall</display-name>
<context-param>
<param-name>contextConfigLocation </param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>userExporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userExporter</servlet-name>
<url-pattern>/remoting/RMI</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、客户端
(1)在spring bean配置文件中创建一个类HttpInvokerProxyFactoryBean的bean,指定serviceUrl属性为服务器端的服务提供的URL,serviceInterface属性为服务器端配置的服务类型。
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<bean id="userProvider" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8085/JavaRemoteCall/remoting/RMI</value>
</property>
<property name="serviceInterface">
<value>com.erayt.service.IUserHttpService</value>
</property>
</bean>
</beans>
(2)新建接口,需与服务端保持一致
package com.erayt.service;
import com.erayt.domain.User;
public interface IUserHttpService {
public void addUser(User user);
}
(3)客户端测试类(前提服务端要部署到Tomcat并启动着)
package com.erayt.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.erayt.domain.User;
import com.erayt.service.IUserHttpService;
public class RMITest {
public static void main(String[] args) {
try{
ApplicationContext ctx= new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
IUserHttpService batchService=(IUserHttpService)ctx.getBean("userProvider");
User user = new User();
user.setUserName("张三");
user.setPassWord("123");
user.setAge(18);
batchService.addUser(user);
}catch(Exception e){
e.printStackTrace();
System.exit(1);//同步调用失败
}
}
}
结果如下:User [userName=张三, passWord=123, age=18]