1. 程式人生 > >Java 遠端呼叫之Hessian簡例

Java 遠端呼叫之Hessian簡例

1,匯入jar包

<dependency>
	<groupId>org.resthub</groupId>
	<artifactId>hessian</artifactId>
	<version>4.0.8</version>
</dependency>
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.3.3</version>
</dependency>

2,提供介面
/**
 * 服務介面
 */
public interface ISayHelloService {

	/**
	 * @param name
	 * @return
	 */
	String doSayHello(String name);
}

3,提供實現類
/**
 * 服務介面實現
 */
@Service
public class SayHelloServiceImpl implements ISayHelloService {
	
	public String doSayHello(String name) {
		return doSayHello(name, "hello");
	}
}
4,在server專案中配置web.xml
  <servlet>
    <servlet-name>hessian</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/hessian-servlet.xml</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>hessian</servlet-name>
    <url-pattern>/hessian/*</url-pattern>
  </servlet-mapping>
5,新建hessian-servlet.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.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
	default-lazy-init="false">
	<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

	<context:component-scan base-package="com.rpc.service.impl"/>

	<!-- 測試 -->
	<bean id="sayHelloService" class="com.rpc.service.impl.SayHelloServiceImpl" /> 

	<bean id="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="sayHelloService" />
		<!-- 服務介面 -->
		<property name="serviceInterface" value="com.hatchet.rpc.service.ISayHelloService" />
	</bean>
	
</beans>
6,在client 專案中引用remote.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"
	xsi:schemaLocation="
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
           <context:property-placeholder ignore-resource-not-found="true"
			location="classpath*:/application.properties" />
     <bean id="hello" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">    
        <property name="serviceUrl" value="http://${rpc.server.ip}:${rpc.server.port}/crowdfunding-rpc-server/hessian/hello" />    
        <property name="serviceInterface" value="com.hatchet.rpc.service.ISayHelloService" />   
         <property name="overloadEnabled" value="true" />  
         <property name="readTimeout" value="${rpc.readTimeout}" />
    </bean>
</beans>
application.properties檔案
rpc.server.ip=127.0.0.1
rpc.server.port=8080
rpc.readTimeout=60000


大功告成,通過spring注入介面呼叫服務即可