您好,欢迎访问一九零五行业门户网

rpc框架有哪些

rpc框架有:1、rmi,远程方法调用;2、hessian,基于http的远程方法调用;3、dubbo,淘宝开源的基于tcp的rpc框架。
rpc框架有:
rpc是远程过程调用的简称,广泛应用在大规模分布式应用中,作用是有助于系统的垂直拆分,使系统更易拓展。java中的rpc框架比较多,各有特色,广泛使用的有rmi、hessian、dubbo等。rpc还有一个特点就是能够跨语言,本文只以java语言里的rpc为例。
对于rpc有一个逻辑关系图,以rmi为例:
其他的框架结构也类似,区别在于对象的序列化方法,传输对象的通讯协议,以及注册中心的管理与failover设计(利用zookeeper)。
客户端和服务端可以运行在不同的jvm中,client只需要引入接口,接口的实现以及运行时需要的数据都在server端,rpc的主要依赖技术是序列化、反序列化和传输协议,java里对应的就是对象的序列化、反序列化以及序列化后数据的传输。rmi的序列化和反序列化是java自带的,hessian里的序列化和反序列化是私有的,传输协议则是http,dubbo的序列化可以多种选择,一般使用hessian的序列化协议,传输则是tcp协议,使用了高性能的nio框架netty。对于序列化,我还了解一些,像google的probuffer、jboss marshalling和apache thrift等,之前有写一篇介绍probuffer的博文
1、rmi(远程方法调用)
java自带的远程方法调用工具,不过有一定的局限性,毕竟是java语言最开始时的设计,后来很多框架的原理都基于rmi,rmi的使用如下:
对外接口
<span style="font-size:12px;">public interface iservice extends remote {    public string queryname(string no) throws remoteexception;}</span>
服务实现
import java.rmi.remoteexception;import java.rmi.server.unicastremoteobject;// 服务实现public class serviceimpl extends unicastremoteobject implements iservice {    /**     */    private static final long serialversionuid = 682805210518738166l;    /**     * @throws remoteexception     */    protected serviceimpl() throws remoteexception {        super();    }    /* (non-javadoc)     *     */    @override    public string queryname(string no) throws remoteexception {        // 方法的具体实现        system.out.println(hello + no);        return string.valueof(system.currenttimemillis());    }    }
rmi客户端
import java.rmi.accessexception;import java.rmi.notboundexception;import java.rmi.remoteexception;import java.rmi.registry.locateregistry;import java.rmi.registry.registry;// rmi客户端public class client {    public static void main(string[] args) {        // 注册管理器        registry registry = null;        try {            // 获取服务注册管理器            registry = locateregistry.getregistry(127.0.0.1,8088);            // 列出所有注册的服务            string[] list = registry.list();            for(string s : list){                system.out.println(s);            }        } catch (remoteexception e) {                    }        try {            // 根据命名获取服务            iservice server = (iservice) registry.lookup(vince);            // 调用远程方法            string result = server.queryname(ha ha ha ha);            // 输出调用结果            system.out.println(result from remote :  + result);        } catch (accessexception e) {                    } catch (remoteexception e) {                    } catch (notboundexception e) {                    }    }}
rmi服务端
import java.rmi.remoteexception;import java.rmi.registry.locateregistry;import java.rmi.registry.registry;// rmi服务端public class server {    public static void main(string[] args) {        // 注册管理器        registry registry = null;        try {            // 创建一个服务注册管理器            registry = locateregistry.createregistry(8088);        } catch (remoteexception e) {                    }        try {            // 创建一个服务            serviceimpl server = new serviceimpl();            // 将服务绑定命名            registry.rebind(vince, server);                        system.out.println(bind server);        } catch (remoteexception e) {                    }    }}
服务注册管理器写在了server里,当然也可以抽出来单独作为一个服务,在其他一些框架中,往往用zookeeper充当注册管理角色。
2、hessian(基于http的远程方法调用)
基于http协议传输,在性能方面还不够完美,负载均衡和失效转移依赖于应用的负载均衡器,hessian的使用则与rmi类似,区别在于淡化了registry的角色,通过显示的地址调用,利用hessianproxyfactory根据配置的地址create一个代理对象,另外还要引入hessian的jar包。
3、dubbo(淘宝开源的基于tcp的rpc框架)
基于netty的高性能rpc框架,是阿里巴巴开源的,总体原理如下:
在了解dubbo之前,要先对zookeeper有深入的理解,当理解了zookeeper后,dubbo也就了无秘密了。
dubbo的详细说明在淘宝开源里说的非常详细,在工作中很多生产项目都用了dubbo,过程中也发现了很多需要注意的地方,尤其是那繁多的配置,设置不当都会让人烦脑,最好能再基于现有开源的dubbo再定制优化一下。
相关免费学习推荐:java基础教程
以上就是rpc框架有哪些的详细内容。
其它类似信息

推荐信息