HttpInvoker远程调用实例

一、服务器端:

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);

    }

}

 

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值