package edu.bjtu.invoker;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InvokerClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("invoker-client.xml");
IHelloService service =
(IHelloService) context.getBean("helloServiceProxy");
String result = service.doHelloService("ZHAO YUAN");
System.out.println(result);
}
}
httpinvoker的使用实例



HTTPInvoker是Spring框架中的一种远程调用机制,它允许客户端通过HTTP协议来调用服务器端的方法,实现远程服务的透明化访问。这个技术基于HTTP,所以具有较好的穿透性,适用于内网或者对安全性要求不高的环境。在这个实例中,包含了服务端和服务端两个工程,分别模拟了HTTPInvoker的工作流程。 在服务端,你需要配置一个`HttpInvokerServiceExporter`,这个组件将你的业务接口暴露为HTTP服务。定义一个服务接口,比如`HelloService`: ```java public interface HelloService { String sayHello(String name); } ``` 然后,实现这个接口,并通过`@Service`注解标记为Spring Bean: ```java @Service public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name; } } ``` 接着,配置`HttpInvokerServiceExporter`,指定服务接口和实现类: ```xml <bean id="helloServiceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="serviceInterface" value="com.example.HelloService"/> <property name="service" ref="helloServiceImpl"/> </bean> ``` 服务端还需要配置Servlet容器(如Tomcat)来处理HTTP请求,确保`http-invoker-listener`监听器加载HTTPInvoker的服务。 在客户端,你需要创建一个`HttpInvokerRequestExecutor`,用于执行HTTP调用,然后配置`HttpInvokerProxyFactoryBean`,指定远程服务的URL和接口类型: ```xml <bean id="helloServiceProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://serverHost:port/servletPath"/> <property name="serviceInterface" value="com.example.HelloService"/> <property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor"/> </bean> <bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor"/> ``` 现在,你可以像调用本地方法一样调用远程服务: ```java HelloService helloService = (HelloService)applicationContext.getBean("helloServiceProxy"); String result = helloService.sayHello("World"); ``` 在实际项目中,你可能需要处理异常、配置HTTP连接超时、重试策略等。此外,HTTPInvoker还可以与其他Spring特性结合,如AOP、事务管理等,提供更强大的功能。 这个实例中的`httpinvoke`目录可能包含服务端和客户端的源代码、配置文件以及运行脚本。通过分析和运行这些代码,你可以更深入地理解HTTPInvoker的工作原理和使用方式。同时,学习这个实例也能帮助你掌握Spring框架的远程调用能力,提升你在分布式系统开发中的技能。













































































- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整
- xiaodoor2013-08-07试了 代码可以使用 非常感谢
- luyantongqq2013-09-29直接到进入用不了。。
- jayamazing2016-05-24代码可以使用 非常感谢

- 粉丝: 0
- 资源: 5
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基于Matlab的SVM支持向量机算法实现及其在高精度预测中的应用
- targetd-0.10.0-3.el8.x64-86.rpm.tar.gz
- 基于S7-200 PLC与组态王的花式喷泉控制系统设计及其实现
- 203御用语文分享作文
- 基于三菱PLC和MCGS的广场喷泉控制系统设计与实现
- taskd-1.1.0-18.el8.x64-86.rpm.tar.gz
- 基于PLC的音乐喷泉控制系统设计:实现音乐与水景的完美同步
- 启动盘制作工具,制作linux,kylin启动盘,草鸡好用
- C++ 学习资料,基础知识
- 基于S7-200 PLC和组态王的喷泉控制系统设计与实现
- tayga-0.9.2-17.el8.x64-86.rpm.tar.gz
- 锂离子电池挤压模型-几何
- LabVIEW与欧姆龙PLC的CIP通讯:实现高效稳定的标签读写操作
- 智能优化领域的自适应变异麻雀搜索算法AMSSA及其Python实现
- 仿爱站风格带后台完整版
- tbb-2018.2-9.el8.x64-86.rpm.tar.gz


