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技術。筆者也百度看了不少示例,但都說的不夠明白,摸索了好久了,花了一天時間 , 終於搞定了。但願這篇文章可以讓你更清晰的理解。

相關文章
相關標籤/搜索