Thrift 入门

前言

  在微服务盛行的互联网时代,各家都在构建自己的微服务体系,而这其中RPC框架也是其中比较重要的部分,笔者之前一直接触使用的是 Dubbo 框架,对Thrift 完全不了解,也是在研究学习的过程中,不断的记录才有了这篇博客。

参考博文:https://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/#ibm-pcon

Thrift 是什么?

Thrift 是一个跨语言的服务开发框架,采用接口描述语言定义并创建服务,所包含的代码生成引擎可以在多种语言中,其传输数据采用二进制格式,相对于xml与Json传输体积更小,对于高并发,大数据量和多语言更有优势。

Thrift 入门示例

学习课程之前先来个Hello World

主要包含:1. Thrift接口定义文件 2. Java 实现类 3.服务端代码示例 4. 客户端代码示例

本文使用Thrift 版本
Thrift version 0.9.3

maven 包引入
<dependency>
    <groupId>org.apache.thrift</groupId>
    <artifactId>libthrift</artifactId>
    <version>0.9.3</version>
</dependency>
Thrift 文件
namespace java com.example.demo.thrift
service  Hello {
  string sayHello(1:string name)
}
编译的Java 文件略,编译命令
thrift -gen java Hello.thrift
接口实现
package com.example.demo.thrift.impl;

import com.example.demo.thrift.Hello;
import org.apache.thrift.TException;

public class HelloThriftImpl implements Hello.Iface {

    @Override
    public String sayHello(String name) throws TException {
        System.out.println(name);
        return "success";
    }
}
服务端代码
package com.example.demo.thrift.server;

import com.example.demo.thrift.Hello;
import com.example.demo.thrift.impl.HelloThriftImpl;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;

public class HelloThriftServer {

    private static int port = 8888;

    public static void main(String[] args) {
        try {
            //设置服务端的端口
            TServerSocket serverTransport = new TServerSocket(port);
            // 设置协议工厂为 TBinaryProtocol.Factory
            TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory();
            // 关联处理器与 Hello 服务的实现
            TProcessor processor = new Hello.Processor(new HelloThriftImpl());

            TThreadPoolServer.Args args1 = new TThreadPoolServer.Args(serverTransport);
            args1.inputProtocolFactory(proFactory);
            args1.processor(processor);
            TServer server = new TThreadPoolServer(args1);
            System.out.println("Start server on port " + port);
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
客户端代码
package com.example.demo.thrift.client;

import com.example.demo.thrift.Hello;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class HelloThriftClient {

    private static int port = 8888;
    public static void main(String[] args) {

        try {
            // 设置调用的服务地址为本地,端口为 8888
            TTransport transport = new TSocket("localhost", port);
            transport.open();
            // 设置传输协议为 TBinaryProtocol
            TProtocol protocol = new TBinaryProtocol(transport);
            Hello.Client client = new Hello.Client(protocol);
            // 调用服务的 helloVoid 方法
            String helloRtn = client.sayHello("hello world");
            System.out.println(helloRtn);
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
代码执行

先启动Server

《Thrift 入门》

《Thrift 入门》

    原文作者:可爱的尖椒肉丝
    原文地址: https://segmentfault.com/a/1190000020495801
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞