首页 > 编程知识 正文

rpc远程过程调用,rpc接口响应

时间:2023-05-04 19:06:39 阅读:12412 作者:4605

在实例之前,可以知道一个叫--HttpInvoker的东西。 HTTPInvoker的使用原理

springhttpinvoker :使用http协议穿透防火墙,以便可以使用JAVA系列方法。 但是,只能在spring APP应用程序之间使用。 这意味着调用方和被调用方必须是使用spring框架的APP应用程序。

服务器端:

基于当前的多模块项目开发,我们创建了一个新的客户端模块,为客户端提供rpc远程调用接口。

/** * @description获取所有目录的远程rpc接口* @ author : gaobh * @ date :2018/4/129336033 * @ version 3360 v 1.0 * @ author : lalal * @ date :2018/4/129336038 * @ version : v 1.0 */@ componentpubliccclassgetalllmenurpcimplimplemementents @ return */@获取overridepublicvoidgetallmenent }以上是典型接口及其实现类,现在通过一系列配置,将该接口向外开放,客户端可以使用该rpc接口以下是相关的构成。配置:

1、服务声明:

在Spring配置文件中,声明HttpInvokerServiceExporter类的bean。 总共有三个部分。

服务名称、服务类型、服务实现类

bean id=' getallmenuprc ' class=' com.Gao.Scorpio.service.RPC.getallmenurpcimpl '/bean name=' menu exporter ' class=' org.spring framework.remoting.http invoker.httpinvokerserviceeeexporter ' prty naty om.Gao.Scorpio.RPC.getallmenurpc '/bean 2,服务URL相关:

在web.xml中声明与服务名相同的servlet (当然,此servlet类的Spring是已经提供的HttpRequestHandlerServlet,该男子的作用是直接向同名的bean提交请求)

servlet servlet-namemenuexporter/servlet servlet-class org.spring framework.web.context.support.http request ter服务器servlet-name URL-pattern/remoting/getallmenurpc/URL-pattern/pattern (在本例中,不是在另一个java项目中进行配置调用,而是在本地进行测试wishing具有相同的配置。 )在spring bean配置文件中为类HttpInvokerProxyFactoryBean创建bean,并指定serviceUrl属性为服务器端服务提供的serviceUrl属性

/** * @description远程呼叫测试类* @ author : lalla * @ date :2018/4/129336057 * @ version : v 1.0 */publiclass args ) genericxmlapplicationcontextctx=newgenericxmlapplicationcontext ); CTX.load (class path : RPC-consumer.XML ); ctx.refresh (; getallmenurpcgetallmenurpc=CTX.get bean (remotemenuservice,GetAllMenuRPC.class ); getAllMenuRPC.getAllMenu (; system.out.println(888; }} 客户端:

bean id=' remotemenuservice ' class=' org.spring framework.remoting.http invoker.httpinvokerproxyfactorybean ' properk property name='服务接口' value=' com.Gao.Scorpio.RPC.getallment

一切结束后,可以先运行服务器,然后运行客户端程序查看调用结果。

在这里,我们将使用本服务的测试类进行测试。 在开发过程中,建议用这种方法进行测试和代码调试。 在本地成功后,启动和调试真正的客户端可以节省大量的时间。 需要注意的是,客户端项目要远程调用服务器端代码,还必须在客户端之上部署服务器端client jar包。

正文参考文章: Spring HTTP Invoker使用介绍

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。