Spring HTTP Invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用,也就是说,可以通过防火墙,并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,要注意的一点是,服务端、客户端都是使用Spring框架。
2.客户端:通过Spring HTTP Invoker代理,向服务端发送请求,远程调用服务端接口。
注意事项:由于是通过网络传输,所以服务端、客户端的POJO类,都要实现Serializable接口,进行序列化、反序列化。
Spring HTTP Invoker有两种实现方式
1.基于Url映射方式,远程系统处理请求的方式同SpringMVC的controller类似,所有的请求通过在web.xml中的 org.springframework.web.servlet.DispatcherServlet统一处理,根据url映射,去对应的 【servlet名称-servlet.xml】文件中,查询跟请求的url匹配的bean配置
UserService.java
UserServiceImpl.java
applicationContext-httpinvoker-exporter.xml
工程代码:
http://download.csdn.net/detail/tianwei7518/8466547
整体流程如下:
1.服务器端:通过Spring HTTP Invoker,将服务接口的某个实现类提供为远程服务。2.客户端:通过Spring HTTP Invoker代理,向服务端发送请求,远程调用服务端接口。
注意事项:由于是通过网络传输,所以服务端、客户端的POJO类,都要实现Serializable接口,进行序列化、反序列化。
Spring HTTP Invoker有两种实现方式
1.基于Url映射方式,远程系统处理请求的方式同SpringMVC的controller类似,所有的请求通过在web.xml中的 org.springframework.web.servlet.DispatcherServlet统一处理,根据url映射,去对应的 【servlet名称-servlet.xml】文件中,查询跟请求的url匹配的bean配置
2.基于Servlet方式,由org.springframework.web.context.support.HttpRequestHandlerServlet去拦截url- pattern匹配的请求,如果匹配成功,去ApplicationContext中查找name与servlet-name一致的bean,完成远程方法调用。
下面我们使用第一种方式实现测试。
1.在pom.xml添加依赖jar包
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.0.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-expression</artifactId>
- <version>4.0.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>4.0.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>4.0.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>4.0.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <version>4.3.2</version>
- </dependency>
2.定义接口及实现类
User.Java
- package cn.slimsmart.spring.httpinvoker.demo;
- import java.io.Serializable;
- public class User implements Serializable{
- private static final long serialVersionUID = 1L;
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "name="+name+",age="+age;
- }
- }
- package cn.slimsmart.spring.httpinvoker.demo;
- public interface UserService {
- User getUser(User user);
- }
- package cn.slimsmart.spring.httpinvoker.demo;
- public class UserServiceImpl implements UserService{
- public User getUser(User user) {
- System.out.println(user.toString());
- user.setAge(100);
- user.setName("Hello "+user.getName());
- return user;
- }
- }
3.服务端spring配置
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
- 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-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
- default-lazy-init="true">
- <description>spring invoker服务</description>
- <bean id="userServiceimpl" class="cn.slimsmart.spring.httpinvoker.demo.UserServiceImpl" />
- </beans>
- <?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:context="http://www.springframework.org/schema/context"
- 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-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
- default-lazy-init="true">
- <description>spring invoker服务</description>
- <!-- 通过Spring HttpInvoker机制暴露远程访问服务 -->
- <bean name="/userService" id="userServiceExporter"
- class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
- <property name="service" ref="userServiceimpl"/>
- <property name="serviceInterface" value="cn.slimsmart.spring.httpinvoker.demo.UserService" />
- </bean>
- <!-- 也可以使用如下配置,制定调用注册地址
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <props>
- <prop key="/userService">userServiceExporter</prop>
- </props>
- </property>
- </bean>
- -->
- </beans>
4.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>web_demo</display-name>
- <!-- 加载服务配置文件 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>dispatcherServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/applicationContext-httpinvoker-exporter.xml</param-value>
- </init-param>
- <!-- 配置该Servlet随应用启动时候启动 -->
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcherServlet</servlet-name>
- <url-pattern>/remoting/*</url-pattern>
- </servlet-mapping>
- <session-config>
- <session-timeout>30</session-timeout>
- </session-config>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
5.客户端spring配置
applicationContext-httpinvoker-client.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" xmlns:context="http://www.springframework.org/schema/context"
- 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-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
- default-lazy-init="true">
- <description>spring invoker服务</description>
- <bean id="httpInvokerRequestExecutor"
- class="org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor">
- <property name="connectTimeout" value="3000" />
- <property name="readTimeout" value="60000" />
- </bean>
- <bean id="userService"
- class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
- <property name="serviceUrl"
- value="http://127.0.0.1:8080/spring-httpinvoker-demo/remoting/userService" />
- <property name="serviceInterface"
- value="cn.slimsmart.spring.httpinvoker.demo.UserService" />
- <!-- 默认HttpInvokerProxy使用了JDK的HTTP功能,但是了你也可以使用Apache的HttpComponents客户端来设置httpInvokerRequestExecutor属性 -->
- <property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor" />
- </bean>
- </beans>
6.客户端调用
UserServiceTest.java
- package cn.slimsmart.spring.httpinvoker.demo;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class UserServiceTest {
- public static void main(String[] args) {
- @SuppressWarnings("resource")
- ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-httpinvoker-client.xml");
- UserService userService = (UserService)context.getBean("userService");
- User user = new User();
- user.setAge(25);
- user.setName("张三");
- System.out.println(userService.getUser(user));;
- }
- }