Spring+Hessian搭建远程方法调用

1、hessian调用分为服务端和客户端

2、总体图

3、服务端配置

(1)web.xml

<!-- hessian 配置  默认会加载hessian-servlet.xml-->
    <servlet>
        <servlet-name>hessian</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hessian</servlet-name>
        <url-pattern>/hessian/*</url-pattern>
    </servlet-mapping>
    <!-- hessian 配置 -->

(2)Interface

public interface WebHessianService {
    void test();
}

(3)Implement

@Service("webHessianService")
public class WebHessianServiceImpl implements WebHessianService {

    public void test(){
          system.out.println();
    }
}

(4)HessianServerProxyExporter:

Servlet-API-based HTTP request handler that exports the specified service bean as Hessian service endpoint, accessible via a Hessian proxy.
public class HessianServerProxyExporter extends HessianServiceExporter {
    private static Logger logger = LoggerFactory.getLogger(HessianServerProxyExporter.class);
    PropertyUtil propertyUtil = PropertyUtil.getInstance("project");
    public String hessianAuth;

    public HessianServerProxyExporter() {
        this.hessianAuth = this.propertyUtil.getProperty("hessianAuth");
    }

    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("++++ hessian request clientIp:" + request.getRemoteAddr() + "++++requestData:" + request.getRequestURL());
        String auth = request.getHeader("hessianAuth");
        if(auth != null && auth.equalsIgnoreCase(this.hessianAuth)) {
            super.handleRequest(request, response);
        } else {
            logger.info("+++++hessianAuth->fail :" + request.getRemoteAddr() + "," + request.getRequestURL());
        }
    }
}
(5)ServiceBean
 <!--  Hessian访问路径 含权限认证 -->
    <bean name="/webHessian" class="com.qysxy.hessian.server.HessianServerProxyExporter">
        <property name="service" ref="webHessianService"/>
        <property name="serviceInterface">
            <value>
                com.qysxy.framework.hessian.WebHessianService
            </value>
        </property>
    </bean>

 4、客户端配置

(1)HessianClientProxyFactory   :hessian客户端代理

public class HessianClientProxyFactory extends HessianProxyFactory {
    PropertyUtil propertyUtil = PropertyUtil.getInstance("project");
    private final String hessianAuth;

    public HessianClientProxyFactory() {
        this.hessianAuth = this.propertyUtil.getProperty("hessianAuth");
    }

    protected URLConnection openConnection(URL url) throws IOException {
        URLConnection conn = super.openConnection(url);
        conn.setRequestProperty("hessianAuth", this.hessianAuth);
        return conn;
    }
}

(2)Interface

public interface WebHessianService {
    void test();
}

(3)clientBean:

    <bean id="webHessianService"
        class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceInterface"
            value="com.qysxy.framework.hessian.service.WebHessianService"></property>
        <property name="serviceUrl">
            <value>${hessianPath}/hessian/webHessian</value>
        </property>
        <property name="readTimeout">
            <value>100000</value>
        </property>
        <property name="proxyFactory">
            <bean
                class="com.*.hessian.client.HessianClientProxyFactory" />
        </property>
        <property name="chunkedPost" value="false" />
    </bean>

5、测试

 @Test
    public void hessian() {
        String url = "http://127.0.0.1/hessian/webHessian";
        HessianClientProxyFactory  factory = new HessianClientProxyFactory();
        try {
            WebHessianService service = (WebHessianService) factory.create(WebHessianService.class, url);
           service.test();
            System.err.println("map=" + map.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

 6、总结:

服务端是需要定义方法接口并且实现方法,利用servlet和spring的拦截器控制访问;

客户端可以用bean组件定义好调用方法接口、访问URL、hessian代理工厂

posted @ 2017-04-14 14:38  构建巨人肩膀  阅读(5091)  评论(0编辑  收藏  举报