spring远程调用方式之HttpInvoker

项目之间进行通信,会用到不少远程调用技术,今天讲一下httpinvoker的详细配置,具体的配置过程以下web

1.服务器配置

 

1.1在服务器端项目上,web.xml配置servletspring

示例:spring-mvc

 

<servlet>
		<servlet-name>rmiHttp</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
		<init-param>
			  <param-name>contextConfigLocation</param-name>
            <!-- 自定义位置 -->
            <param-value> 
            classpath:config/rmi/rmiHttp-servlet.xml</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>rmiHttp</servlet-name>
		<url-pattern>/rmi/*</url-pattern>
	</servlet-mapping>

 

 

1.2 添加服务器上spring httpinvoker service相关配置(rmiHttp-servlet.xml)服务器

 

示例:mvc

 

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
   <bean name="rmiService" class="com.web.jutong.rmi.RmiServiceImpl"></bean>
   <bean name="/rmiHttp" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
   		<property name="service" ref="rmiService" ></property> 
   		<property name="serviceInterface" value="com.web.jutong.rmi.IRmiService" ></property> 
   </bean>
 
</beans>

 

 

这一段很关键app

 

第一个bean 是实现类bean 在下面实际使用的bean中使用到了。ide

第二个bean 是真正的服务,bean的名称要与servlet名称相同,也就是提供的服务。class须要是工具

org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter

 

或者相关的类,才能达到使用httpinvoker的目的。this

service 就是引用实现类url

serviceInterface指向上层接口类

1.3 后台代码实现

 

package com.web.jutong.rmi;

public interface IRmiService  {
    public String  doQueryVideo(String termno,String start,String end);
}

 

 

package com.web.jutong.rmi;

import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.stereotype.Service;
@Service("rmiService")
public class RmiServiceImpl  implements IRmiService {

	@Override
	public String doQueryVideo(String termno, String start, String end) {
		// TODO Auto-generated method stub
		return "123";
	}

	
}

 

 

至此,服务器端已设置完毕。接下来看客户端配置

2.客户端配置

2.1独立的bean配置(http-invoker.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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
        <bean id="rmiHttpService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        	<property name="serviceUrl" value="http://127.0.0.1:8080/demo/rmi/rmiHttp"></property>
        	<property name="serviceInterface" value="com.jutong.web.rmi.IRmiService"></property>	
        </bean>                

</beans>

 

2.2spring配置修改

contextConfigLocation 增长路径设置

  <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 自定义位置 -->
            <param-value> 
            classpath:config/http-invoker.xml,
            classpath:config/spring-*.xml</param-value>
        </init-param>

2.3客户端接口

复制服务器端的接口文件到客户端,serviceInterface指向本地的接口地址。

2.4客户端调用

 

 

@RequestMapping("/query/result")
@ResponseBody
public String doQueryResult(String termno,String start,String end) {
	RmiUtil rmi =new RmiUtil(); 
	String result=rmi.getService(this.ac).doQueryVideo(termno, start, end);
	return result;
}

 

 

RmiUtil 是一个工具类

 

 

package com.jutong.util;

import org.springframework.context.ApplicationContext; 

import com.jutong.web.rmi.IRmiService;

public class RmiUtil {
	
	 private IRmiService service=null;
	 
	 public IRmiService getService(ApplicationContext context){
		 if(null==service)
		 {
	             service=context.getBean("rmiHttpService",IRmiService.class);
		 }
		 return service;
	 } 
}

 

 

3.结束语

本例详细解说了httpinvoker的详细配置,由于字母太多,改用了rmi做为代替,其实是用的httpinvoker技术。笔者也百度看了不少示例,但都说的不够明白,摸索了好久了,花了一天时间 , 终于搞定了。但愿这篇文章可以让你更清晰的理解。

相关文章
相关标签/搜索