Spring HTTP Invoker 提供远程 服务
Spring HTTP Invoker例子,Spring HTTP Invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用,也就是说,可以通过防火墙,并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,要注意的一点是,服务端、客户端都是使用Spring框架。下面通过一个例子,来讲解Spring HTTP Invoker的使用,这里使用的是Spring3.0框架。
整体流程如下:
1.服务器端:通过Spring HTTP Invoker,将服务接口的某个实现类提供为远程服务。
2.客户端:通过Spring HTTP Invoker代理,向服务端发送请求,远程调用服务端接口。
注意事项:由于是通过网络传输,所以服务端、客户端的POJO类,都要实现Serializable接口,进行序列化、反序列化。
先看看服务端的配置:
1.先提供一个服务接口
package spring.invoker.service;
import spring.invoker.domain.UserInfo;
/**
* spring http invoker例子
* @author steven
*/
public interface UserService {
/**
* 根据用户名,获取用户信息
* @param userName 用户名
* @return 返回用户信息
*/
public UserInfo getUserInfobyName(String userName);
/**
* 根据用户名,获取用户邮箱
* @param userName 用户名
* @return 返回用户邮箱
*/
public String getUserEmailbyName(String userName);
}
2.再实现这个服务接口,像平时写service一样,这里只是一个简单的例子,可以在这里调用自己的DAO,查询数据库。
package spring.invoker.service.impl;
import spring.invoker.domain.UserInfo;
import spring.invoker.service.UserService;
/**
* spring http invoker例子
* @author steven
*
*/
public class UserServiceImpl implements UserService{
/**
* 根据用户名,获取用户信息
* @param userName 用户名
* @return 返回用户信息
*/
public UserInfo getUserInfobyName(String userName){
UserInfo userInfo = new UserInfo();
userInfo.setUserName(userName);
userInfo.setEmail("xxx@site.com");
return userInfo;
}
/**
* 根据用户名,获取用户邮箱
* @param userName 用户名
* @return 返回用户邮箱
*/
public String getUserEmailbyName(String userName){
return userName + "的邮箱地址为:xxx.site.com";
}
}
3.接下来,就是POJO类了,这个类一定要实现Serializable接口,并指定一个serialVersionUID。
package spring.invoker.domain;
import java.io.Serializable;
/**
* 用户基本信息
* @author steven
*
*/
public class UserInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String userName;
private String email;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
4.将接口声明为HTTP Invoker服务
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 交给Spring管理,bean的名称是:userService -->
<bean id="userService" class="spring.invoker.service.impl.UserServiceImpl"/>
<!-- 这个配置,就是把userService接口,提供给远程调用 -->
<bean id="httpService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<ref bean="userService" />
</property>
<property name="serviceInterface"
value="spring.invoker.service.UserService">
</property>
</bean>
<!-- 远程服务的URL -->
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/test">httpService</prop>
</props>
</property>
</bean>
</beans>
5.WEB-INF/web.xml:配置spring监听及DispatcherServlet
<?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>Spring3.0 Test Demo</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-service.xml
/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
通过以上5步,就配置好了Spring HTTP Invoker服务,地址:http://${serviceName}:${port}/${contextPath}/test
下面就是客户端怎么调用远程Spring HTTP Invoker服务。
1.创建服务接口及网络间传输的POJO类,为了方便,可以将服务器端创建好的的UserService.java和UserInfo.java拷贝到客户端,或打个jar包放到lib下。
2.配置HTTP Invoker的代理,用来调用远程服务,spring配置文件如下:
<bean id="httpService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>
http://${serviceName}:${port}/${contextPath}/test
</value>
</property>
<property name="serviceInterface" value="spring.invoker.service.UserService">
</property>
</bean>
3. 下面是使用Spring注解的方式,当然 ,你也可以使用配置的方式,把下面的类交给Spring管理,再把httpService通过set的方式,注入进来。
package spring.invoker;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import spring.invoker.service.UserService;
@Controller
public class InvokerAction {
@Resource private UserService httpService;
@RequestMapping(value="/httpTest")
public void testInvoker(){
System.out.println(httpService.getUserEmailbyName("xxx"));
}
}
文章来源:http://www.juziku.com/wiki/2326.htm