zoukankan      html  css  js  c++  java
  • Thrift 个人实战--初次体验Thrift

    前言:
      Thrift作为Facebook开源的RPC框架, 通过IDL中间语言, 并借助代码生成引擎生成各种主流语言的rpc框架服务端/客户端代码. 不过Thrift的实现, 简单使用离实际生产环境还是有一定距离, 本系列将对Thrift作代码解读和框架扩充, 使得它更加贴近生产环境. 本文主要讲解Thrift的初体验, 使得开发者对thrift有个初步的认识.

    Thrift 软件栈
      Thrift对软件栈的定义非常的清晰, 使得各个组件能够松散的耦合, 针对不同的应用场景, 选择不同是方式去搭建服务.
      
    评注:
      Transport: 传输层,定义数据传输方式,可以为TCP/IP传输,内存共享或者文件共享等
      protocol: 协议层, 定义数据传输格式,可以为二进制或者XML等
      Processor: 处理层, 这部分由定义的idl来生成, 封装了协议输入输出流, 并委托给用户实现的handler进行处理.
      Server: 服务层, 整合上述组件, 提供网络模型(单线程/多线程/事件驱动), 最终形成真正的服务.

    Thrift 对语言的支持
      Thrift和Google Protobuf相比, 都提供了可扩展序列化机制, 不但兼容性好而且压缩率高. 两者在这块各有长短, 性能上PB稍有优势. 但在语言的支持度上, Protobuf只支持c++/java/python这三种主流的语言, Thrift则几乎覆盖了大部分的语言, 从这点上来说, Thrift的优势非常的明显.

    Thrift 支持的数据类型
    基本类型:
      bool: 布尔值
      byte: 8位有符号整数
      i16: 16位有符号整数
      i32: 32位有符号整数
      i64: 64位有符号整数
      double: 64位浮点数
      string: UTF-8编码的字符串
      binary: 二进制串
    结构体类型:
      struct: 定义的结构体对象
    容器类型:
      list: 有序元素列表
      set: 无序无重复元素集合
      map: 有序的key/value集合
    异常类型:
      exception: 异常类型
    服务类型:
      service: 具体对应服务的类

    小试牛刀
    1) 定义IDL文件
    #). 编辑hello.thrift

    namespace java com.mmxf.service.demo
    
    service HelloService {
      string hello(1: string name);
    }

    #) thrift -gen java hello.thrift
    生成的代码目录结构如下:

    2). 服务端代码

    public class ServerDemo {
    
      // *) 定义实现服务类
      public static class HelloServiceImpl implements HelloService.Iface {
        @Override
        public String hello(String name) throws TException {
          return "hello " + name;
        }
      }
    
      public static void main(String[] args) throws TTransportException {
    
        // *) 传输层(Transport), 设置监听端口为9000
        TServerSocket serverTransport = new TServerSocket(9000);
    
        // *) 协议层
        Factory protocolFactory = new TBinaryProtocol.Factory(true, true);
    
        // *) 处理层(Processor)
        HelloServiceImpl handler = new HelloServiceImpl();
        HelloService.Processor<HelloServiceImpl> processor = 
            new HelloService.Processor<HelloServiceImpl>(handler);
    
        // *) 服务层(Server)
        TServer server = new TThreadPoolServer(
            new TThreadPoolServer.Args(serverTransport)
                .protocolFactory(protocolFactory)
                .processor(processor)
          );
    
        // *) 启动监听服务
        server.serve();
    
      }
    
    }

    3). 客户端代码

    public class ClientDemo {
    
      public static void main(String[] args) throws TException {
    
        // *) 传输层
        TTransport transport = new TSocket("localhost", 9000);
        transport.open();
    
        // *) 协议层, 与服务端对应
        TProtocol protocol = new TBinaryProtocol(transport);
    
        // *) 创建RPC客户端
        HelloService.Client client = new HelloService.Client(protocol);
    
        // *) 调用服务
        System.out.println(client.hello("lilei"));
    
        // *) 关闭句柄
        transport.close();
    
      }
    
    }

    4). 结果验证

    hello lilei

    5). 配置maven依赖, 使用thrift 0.9.0版本

    <dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.9.0</version>
    </dependency>
    
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.5</version>
    </dependency> 

    后续
      后续讲解服务端的各种网络模型, 以及读写超时控制, 敬请期待.

  • 相关阅读:
    Windows Python+Eclipse环境配置
    infobright系列二:数据迁移
    infobright系列一:源码安装infobright
    autotools归纳
    Atlas系列一:Atlas功能特点FAQ
    C#反射技术概念作用和要点
    .net获取本机公网IP代码
    Java泛型-类型擦除
    现在就使用HTML5的十大原因
    让网页图片变灰色的三种方法
  • 原文地址:https://www.cnblogs.com/mumuxinfei/p/3873709.html
Copyright © 2011-2022 走看看