Dubbo/dubbox added native thrift and Avro support

Source: Internet
Author: User

(Facebook) Thrift/(Hadoop) Avro/(Google) probuf (GRPC) is a more eye-catching efficient serialization/RPC framework in recent years, although Dubbo Framework has thrift support, but the dependent version is earlier, only supports 0.8.0, and also makes some extensions to the protocol, not the native thrift protocol.

On GitHub, though, there are friends who have extended support for Dubbo native thrift, but the code is too many, just need a class:

Thrift2protocal.java:

Package Com.alibaba.dubbo.rpc.protocol.thrift2;import Com.alibaba.dubbo.common.url;import Com.alibaba.dubbo.common.logger.logger;import Com.alibaba.dubbo.common.logger.loggerfactory;import Com.alibaba.dubbo.rpc.rpcexception;import Com.alibaba.dubbo.rpc.protocol.abstractproxyprotocol;import Org.apache.thrift.tprocessor;import Org.apache.thrift.protocol.tcompactprotocol;import Org.apache.thrift.protocol.tprotocol;import Org.apache.thrift.server.tnonblockingserver;import Org.apache.thrift.server.tserver;import Org.apache.thrift.transport.tframedtransport;import Org.apache.thrift.transport.tnonblockingserversocket;import Org.apache.thrift.transport.tsocket;import Org.apache.thrift.transport.ttransport;import java.lang.reflect.constructor;/** * add "native thrift" support for DUBBO-RPC * by 杨俊明 ( http://yjmyzz.cnblogs.com/) */public class Thrift2protocol extends Abstractproxyprotocol {public static final int DEFA    Ult_port = 33208; private static final Logger Logger = Loggerfactory.getlogger (Thrift2protoCol.class);    public int Getdefaultport () {return default_port; } @Override protected <T> Runnable doexport (T impl, class<t> type, url url) throws Rpcexcepti        On {logger.info ("Impl = +" + impl.getclass ());        Logger.info ("type = =" + Type.getname ());        Logger.info ("url = +" + URL);        Tprocessor Tprocessor;        Tnonblockingserver.args Targs = null;        String iFace = "$Iface";        String processor = "$Processor";        String typeName = Type.getname ();        Tnonblockingserversocket Transport; if (Typename.endswith (IFace)) {String processorclsname = typename.substring (0, Typename.indexof (iFace)) + proc            Essor;                try {class<?> clazz = Class.forName (processorclsname);                Constructor Constructor = clazz.getconstructor (type);                    try {tprocessor = (tprocessor) constructor.newinstance (IMPL); Transport =New Tnonblockingserversocket (Url.getport ());                    Targs = new Tnonblockingserver.args (transport);                    Targs.processor (Tprocessor);                    Targs.transportfactory (New Tframedtransport.factory ());                Targs.protocolfactory (New Tcompactprotocol.factory ());                    } catch (Exception e) {logger.error (E.getmessage (), E);                throw new Rpcexception ("Fail to create Thrift server (" + URL + "):" + e.getmessage (), E);                }} catch (Exception e) {logger.error (E.getmessage (), E);            throw new Rpcexception ("Fail to create Thrift server (" + URL + "):" + e.getmessage (), E); }} if (Targs = = null) {Logger.error ("Fail to create Thrift server (" + URL + ") due to null args            ");        throw new Rpcexception ("Fail to create Thrift server (" + URL + ") due to null args"); } final Tserver thriftserver = new Tnonblockingserver (Targs);                New Thread (New Runnable () {public void run () {logger.info ("Start Thrift Server");                Thriftserver.serve ();            Logger.info ("Thrift server started.");        }}). Start (); return new Runnable () {public void run () {try {logger.info ("Close Thrift S                    Erver ");                Thriftserver.stop ();                } catch (Throwable e) {Logger.warn (E.getmessage (), E);    }            }        }; } @Override protected <T> T dorefer (class<t> type, url url) throws rpcexception {Logger.info ("Ty        PE = "+ Type.getname ());        Logger.info ("url = +" + URL);            try {tsocket tsocket;            Ttransport Transport;            Tprotocol Protocol;            T thriftclient = null;            String iFace = "$Iface";            String client = "$Client"; StrinG TypeName = Type.getname (); if (Typename.endswith (IFace)) {String clientclsname = typename.substring (0, Typename.indexof (iFace)) + CLI                Ent                Class<?> clazz = Class.forName (clientclsname);                Constructor Constructor = Clazz.getconstructor (Tprotocol.class);                    try {tsocket = new Tsocket (Url.gethost (), Url.getport ());                    Transport = new Tframedtransport (tsocket);                    protocol = new Tcompactprotocol (transport);                    Thriftclient = (T) constructor.newinstance (protocol);                    Transport.open ();                Logger.info ("Thrift Client opened for service (" + URL + ")");                    } catch (Exception e) {logger.error (E.getmessage (), E);                throw new Rpcexception ("Fail to create Remoting client:" + e.getmessage (), E);        }} return thriftclient; } catch (Exception e) {logger.error (E.getmessage (), E);        throw new Rpcexception ("Fail to create remoting client for service (" + URL + "):" + e.getmessage (), E); }    }}

Rewrite the parent class Abstractproxyprotocol two abstract methods Doexport and Dorefer, doexport for the external exposure RPC service, in this method to start Thrift Server,dubbo Service The method is called by the provider at startup. When Dorefer is used for Dubbo service consumer Discovery Services, the corresponding rpc-client is obtained.

With this idea in mind, Avro is also easy to integrate:

Avroprotocol.java

Package Com.alibaba.dubbo.rpc.protocol.avro;import Com.alibaba.dubbo.common.url;import Com.alibaba.dubbo.common.logger.logger;import Com.alibaba.dubbo.common.logger.loggerfactory;import Com.alibaba.dubbo.rpc.rpcexception;import Com.alibaba.dubbo.rpc.protocol.abstractproxyprotocol;import Org.apache.avro.ipc.nettyserver;import Org.apache.avro.ipc.nettytransceiver;import Org.apache.avro.ipc.Server; Import Org.apache.avro.ipc.reflect.reflectrequestor;import Org.apache.avro.ipc.reflect.reflectresponder;import java.net.inetsocketaddress;/** * Add AVRO support for DUBBO-RPC * by Yang Junming (http://yjmyzz.cnblogs.com/) */public class Avroprotocol    Extends Abstractproxyprotocol {public static final int default_port = 40881;    private static final Logger Logger = Loggerfactory.getlogger (Avroprotocol.class);    public int Getdefaultport () {return default_port; } @Override protected <T> Runnable doexport (T impl, class<t> type, url url) throws Rpcexcepti on {Logger.infO ("Impl = +" + impl.getclass ());        Logger.info ("type = =" + Type.getname ());        Logger.info ("url = +" + URL); Final Server server = new Nettyserver (new Reflectresponder (Type, impl), New Inetsocketaddress (Url.gethost ()        , Url.getport ()));        Server.start (); return new Runnable () {public void run () {try {logger.info ("Close Avro Ser                    Ver ");                Server.close ();                } catch (Throwable e) {Logger.warn (E.getmessage (), E);    }            }        }; } @Override protected <T> T dorefer (class<t> type, url url) throws rpcexception {Logger.info ("Ty        PE = "+ Type.getname ());        Logger.info ("url = +" + URL);            try {nettytransceiver client = new Nettytransceiver (New Inetsocketaddress (Url.gethost (), Url.getport ()));            T ref = Reflectrequestor.getclient (type, client); Logger.iNFO ("Create Avro Client");        return ref;            } catch (Exception e) {logger.error (E.getmessage (), E);        throw new Rpcexception ("Fail to create remoting client for service (" + URL + "):" + e.getmessage (), E); }    }}

Don't forget to add a file named Com.alibaba.dubbo.rpc.Protocal under Meta-inf/dubbo/internal, as follows:

Avro=com.alibaba.dubbo.rpc.protocol.avro.avroprotocol

Finally, we'll talk about how to pack into a jar in Dubbo:

Dubbo-rpc/pom.xml, add two additional items:

 <  modules  >   ...  <  module  >  dubbo-rpc-avromodule  >   ...  <  module  >  dubbo-rpc-thrift2</ module  >   ...  </ modules  >  

And then in Dubbo/pom.xml:

   <Artifactset>       <includes>    ...           <include>Com.alibaba:dubbo-rpc-api</include>           <include>Com.alibaba:dubbo-rpc-avro</include>          ...           <include>Com.alibaba:dubbo-rpc-thrift2</include>           ...       </includes>   </Artifactset>    

The Dependencies festival should also be added:

<dependency> <groupId>com.alibaba</groupId> <artifactid>dubbo-rpc-thrift2&lt                ;/artifactid> <version>${project.parent.version}</version> <exclusions> <exclusion> <groupId>org.apache.thrift</groupId> <artif actid>libthrift</artifactid> </exclusion> </exclusions> </depende ncy> <dependency> <groupId>com.alibaba</groupId> <artifactid>dubbo -rpc-avro</artifactid> <version>${project.parent.version}</version> <exclusions                    > <exclusion> <groupId>org.apache.avro</groupId>                    <artifactId>avro</artifactId> </exclusion> <exclusion> <groupid>orG.apache.avro</groupid> <artifactId>avro-ipc</artifactId> </exclusi On> </exclusions> </dependency>

The Dubbo-xxx.jar in this package includes the new protocol. As for Google's Protobuf, is currently in the 3.x-beta stage, and so on after the official version, and then see the situation integrated.

The above code has been submitted to Github:https://github.com/yjmyzz/dubbox (version number: 2.8.4a)

Dubbo/dubbox added native thrift and Avro support

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.