论坛首页 Java企业应用论坛

HttpInvoker远程调用实例

浏览 7206 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-06-06  

一、服务器端:

1、服务接口与实现类

package org.eesite.bbs.remote;

/**

 * 远程服务接口

 * 

 * @author zhanjia

 * 

 */

public interface IRemoteService {

    public String getString(String msg);

}

 

package org.eesite.bbs.remote;

/**

 * 远程服务接口实现类

 * 

 * @author zhanjia

 * 

 */

public class RemoteServiceImpl implements IRemoteService {

    public String getString(String msg) {

       String str = "正在请求调用...远程服务调用成功! " + msg;

       return str;

    }

}

  

2、服务暴露配置remote-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <!-- 通过Spring HttpInvoker机制暴露远程访问服务 -->

    <bean id="rmiService"

       class="org.eesite.bbs.remote.RemoteServiceImpl" />

 

    <bean name="/remoteService"

class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">

       <property name="service" ref="rmiService" />

       <property name="serviceInterface"

           value="org.eesite.bbs.remote.IRemoteService" />

    </bean>

</beans>

 

 

 

3web.xml配置

<!-- 加载服务配置文件 -->

<context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>/WEB-INF/remote-servlet.xml</param-value>

    </context-param>

 

<!-- 配置DispatcherServlet -->

    <servlet>

        <servlet-name>remote</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 配置该Servlet随应用启动时候启动 -->

        <load-on-startup>1</load-on-startup>

    </servlet>

<!-- 配置DispatcherServlet映射的url -->

    <servlet-mapping>

       <servlet-name>remote</servlet-name>

       <url-pattern>/remoting/*</url-pattern>

    </servlet-mapping>

  

注意:

注册servlet名为remote,此名字要和服务配置文件的名字的第一部分相同,即servlet的名字必须为remote-servlet中的remote

 

 

二、客户端

1、配置文件

<!-- 通过Spring HttpInvoker机制代理远程访问服务 -->

    <bean id="remoteService"

    class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">

       <property name="serviceUrl"

           value="http://localhost:2601/EEweb/remoting/remoteService" />

       <property name="serviceInterface"

           value="org.eesite.bbs.remote.IRemoteService" />

    </bean>

其中

1)IRemoteService只要有与服务端接口的方法一样就可以调用,此处的包名和类名可以根据实际情况给出,不过一般建议和服务器端的接口/类名一样,这样更合理些。

2)EEweb为服务端应用名称,remotingweb.xmlservlet过滤url/remoting/*中的remoting

 

 

2、测试类

package test;

import org.eesite.bbs.remote.IRemoteService;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

/**

 * @author zhanjia

 *

 */

public class TestRemote {

 

    /**

     * @param args

     */

    public static void main(String[] args) {

       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("remote.xml");

       IRemoteService service = (IRemoteService) applicationContext

              .getBean("remoteService");

       String msg = service.getString("哈哈,我来了!");

       System.out.println(msg);

    }

}

 

测试结果:正在请求调用...远程服务调用成功! 哈哈,我来了!

 

 

   发表时间:2008-08-20  
写得很好,但是我还是存在一些疑惑的地方,主要有:

1):注册servlet名为remote,此名字要和服务配置文件的名字的第一部分相同,即servlet的名字必须为remote-servlet中的remote.这里是为什么呢?
2):EEweb为服务端应用名称,remoting为web.xml中servlet过滤url的/remoting/*中的remoting.这里的EEweb为服务端应用名称是什么意思啊?

麻烦您说详细点好吗?
0 请登录后投票
   发表时间:2008-08-23  
注册servlet名为remote,此名字要和服务配置文件的名字的第一部分相同,即servlet的名字必须为remote-servlet中的remote。表述不清楚
0 请登录后投票
   发表时间:2008-09-10  
EEweb就是项目名称
0 请登录后投票
   发表时间:2008-09-10  
看上去挺不错的,不过若若的问下,这种用法一般在什么应用上用比较好(或者说是方便)?期待高手们的见解。
0 请登录后投票
   发表时间:2008-09-10  
不错,跟hessian的方式差不多。

我们这边有个客户想使用分布式来做一个项目,我们提供的就是一种这样的一个远程调用的方案。

学习中。。。

0 请登录后投票
   发表时间:2008-09-10  
效率比hessian来的低
0 请登录后投票
   发表时间:2008-09-10  
不错,试了下,挺好玩的。
感觉就是一个webservice,只不过使用了spring的东西罢了,我以前做Xfire,步骤和这个差不多,我说的对么?
还有一个疑问,搂住测试和web服务是在同一个项目里吧,如果别人想访问,这个IRomteService这个接口类怎么暴露给客户端????
0 请登录后投票
   发表时间:2008-09-12  
1、一般在不同项目间需要互相调用方法时就可能会用到
2、我测试的时候是在两个不同的项目中测试的
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics