你的位置:首页 > 软件开发 > Java > dubbo/dubbox 增加原生thrift及avro支持

dubbo/dubbox 增加原生thrift及avro支持

发布时间:2016-03-06 15:00:20
(facebook) thrift / (hadoop) avro / (google) probuf(grpc)是近几年来比较抢眼的高效序列化/rpc框架,dubbo框架虽然有thrift的支持,但是依赖的版本较早,只支持0.8.0,而且还对协议做一些扩展,并非原生的thrif ...

(facebook) thrift / (hadoop) avro / (google) probuf(grpc)是近几年来比较抢眼的高效序列化/rpc框架,dubbo框架虽然有thrift的支持,但是依赖的版本较早,只支持0.8.0,而且还对协议做一些扩展,并非原生的thrift协议。

github上虽然也有朋友对dubbo做了扩展支持原生thrift,但是代码实在太多了,只需要一个类即可:

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;/** * 为dubbo-rpc添加"原生thrift"支持 * by 杨俊明(http://yjmyzz.cnblogs.com/) */public class Thrift2Protocol extends AbstractProxyProtocol {  public static final int DEFAULT_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 RpcException {    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)) + processor;      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 Server");          thriftServer.stop();        } catch (Throwable e) {          logger.warn(e.getMessage(), e);        }      }    };  }  @Override  protected <T> T doRefer(Class<T> type, URL url) throws RpcException {    logger.info("type => " + 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)) + client;        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);    }  }}

原标题:dubbo/dubbox 增加原生thrift及avro支持

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录