Apache Thrift 在 Ubuntu 的简单使用

一. Thrift简介

Apache Thrift是跨语言服务访问的框架。最早由Facebook 开发,贡献给了Apache。

图中前三部分分别是 通过Thrift 脚本文件生成的代码,根据生成代码创建的服务器客户端代码和两端的结果.
协议:
TBinaryProtocol: 二进制编码格式
TCompactProtocol: 使用Variable-Length Quantity (VLQ) 编码对数据进行压缩
TJSONProtocol: 使用JSON的数据编码协议进行数据传输
TSimpleJSONProtocol – 这种节约只提供JSON只写的协议,适用于通过脚本语言解析
TDebugProtocol – 在开发的过程中帮助开发人员调试用的,以文本的形式展现方便阅读

传输层
TSocket- 使用堵塞式I/O进行传输,也是最常见的模式。
TFramedTransport- 使用非阻塞方式,按块的大进行传输,类似于Java中的NIO。
TFileTransport- 按照文件的方式进程传输,虽然这种方式不提供Java的实现,但是实现起来非常简单。
TMemoryTransport- 使用内存I/O,就好比Java中的ByteArrayOutputStream实现。
TZlibTransport- 使用执行zlib压缩,不提供Java的实现。

服务端类型
TSimpleServer - 单线程服务器端使用标准的堵塞式I/O。
TThreadPoolServer - 多线程服务器端使用标准的堵塞式I/O。
TNonblockingServer – 多线程服务器端使用非堵塞式I/O,并且实现了Java中的NIO通道。

二. Thrift 安装配置
1)sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev

2)下载
www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.tar.gz

3)解压后进入根目录
./configure
sudo make
sudo make install

hello.thrift



namespace java com.test

service  HelloWorldService {
  string sayHello(1:string username)
}

bye.thrift

namespace java com.test2

service  GoodByeService {
  string sayBye(1:string username)
}

thrift -r –gen java hello.thrift
thrift -r –gen java bye.thrift

GoodByeImpl.java

package com.abc;
import com.test2.*;
import org.apache.thrift.TException;

public class GoodByeImpl implements GoodByeService.Iface {

    public  GoodByeImpl() {
    }

    @Override
    public String sayBye(String username) throws TException {
        otherServer client = new otherServer();
        String result = client.getClientInfo("Lyn");
        return result + "\n Bye-bye, " + username ;


    }

}

HelloWorldImpl.java


package com.abc;
import com.test.*;
import org.apache.thrift.TException;

public class HelloWorldImpl implements HelloWorldService.Iface {

    public HelloWorldImpl() {
    }

    @Override
    public String sayHello(String username) throws TException {
        return "hello, " + username +":"+ HelloServer.SERVER_PORT ;


    }

}

HelloServer.java


package com.abc;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;

import com.test.HelloWorldService;

public class HelloServer {
    public static final int SERVER_PORT = 9090;

    public void startServer() {
        try {
            System.out.println("HelloWorld TSimpleServer start ....");

            TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());
            TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
            TServer.Args tArgs = new TServer.Args(serverTransport);
            tArgs.processor(tprocessor);
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            TServer server = new TSimpleServer(tArgs);
            server.serve();
        } catch (Exception e) {
            System.out.println("Server start error!!!");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        HelloServer server = new HelloServer();
        server.startServer();
    }

}

otherServer.java

package com.abc;

import javax.print.attribute.standard.MediaSize.Other;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

import com.test.HelloWorldService;
import com.test2.GoodByeService;

public class otherServer {
    public static final int PORT2 = 8090;
    public static final int SERVER_PORT = 9090;
    public static final int TIMEOUT = 30000;

    public void startServer() {
        try {
            System.out.println("Another TSimpleServer start ....");
            TProcessor tprocessor = new GoodByeService.Processor<GoodByeService.Iface>(
                    new GoodByeImpl());
            TServerSocket serverTransport = new TServerSocket(PORT2);
            TServer.Args tArgs = new TServer.Args(serverTransport);
            tArgs.processor(tprocessor);
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            TServer server = new TSimpleServer(tArgs);
            server.serve();

        } catch (Exception e) {
            System.out.println("Another Server start error!!!");
            e.printStackTrace();
        }
    }

    public String getClientInfo(String userName) {
        TTransport transport = null;
        String result = "";
        try {
            transport = new TSocket("localhost", SERVER_PORT, TIMEOUT);
            // 协议要和服务端一致
            TProtocol protocol = new TBinaryProtocol(transport);
            // TProtocol protocol = new TCompactProtocol(transport);
            // TProtocol protocol = new TJSONProtocol(transport);
            HelloWorldService.Client client = new HelloWorldService.Client(protocol);
            transport.open();
            result = client.sayHello(userName);
            System.out.println("Another thrift client result =: " + result);

        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
        return result;
    }

    public static void main(String[] args) {

        // HelloClient client = new HelloClient();
        otherServer server = new otherServer();
        // client.startClient("Hello client in other server");
        // client2.startClient("other server");
        server.startServer();

    }

}

HelloClient.java


package com.abc;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

import com.test2.*;

public class HelloClient {

    public static final String SERVER_IP = "localhost";
    public static final int SERVER_PORT =8090;
    public static final int TIMEOUT = 30000;

    public void startClient(String userName) {

        TTransport transport = null;

        try {
            transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
            // 协议要和服务端一致
            TProtocol protocol = new TBinaryProtocol(transport);
            // TProtocol protocol = new TCompactProtocol(transport);
            // TProtocol protocol = new TJSONProtocol(transport);
            GoodByeService.Client client = new GoodByeService.Client(protocol);
            transport.open();
//            String result = client.sayHello(userName);
//            System.out.println("Thrift client result =: " + result);
            String result = client.sayBye(userName);
            System.out.println("Thrift client result =: " + result);
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
    }

    public static void main(String[] args) {
        HelloClient client = new HelloClient();
        client.startClient("Jack");

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache Thrift 是一种高效且跨语言的远程服务调用框架,它可以让你使用不同的编程语言实现客户端和服务器端,并且在它们之间进行通信。下面是使用 Java 快速入门 Apache Thrift 的步骤: 1. 安装 Thrift 首先,你需要从 Apache Thrift 的官方网站(https://thrift.apache.org/)下载并安装 Thrift。 2. 定义接口文件 在 Thrift 中,你需要定义一个接口文件,它描述了你的服务接口和数据类型。下面是一个简单的示例: ``` namespace java com.example service MyService { string sayHello(1: string name) } ``` 上面的代码定义了一个名为 MyService 的服务,它有一个名为 sayHello 的方法,它接受一个名为 name 的字符串参数,并返回一个字符串。 3. 生成代码 使用 Thrift 编译器(thrift)来生成 Java 代码: ``` thrift --gen java MyService.thrift ``` 这将生成一个名为 MyService.java 的 Java 接口文件,以及一些用于序列化和反序列化的辅助类。 4. 实现服务 你需要编写一个实现 MyService 接口的 Java 类,例如: ``` public class MyServiceImpl implements MyService.Iface { public String sayHello(String name) throws TException { return "Hello, " + name + "!"; } } ``` 上面的代码实现了 sayHello 方法,并返回一个带有名称的问候语。 5. 启动服务器 你需要编写一个 Java 服务器,以便可以在其中运行 MyServiceImpl。以下是一个简单的示例: ``` public class MyServer { public static void main(String[] args) throws Exception { TServerTransport serverTransport = new TServerSocket(9090); TServer server = new TSimpleServer(new Args(serverTransport).processor(new MyService.Processor(new MyServiceImpl()))); System.out.println("Starting the server..."); server.serve(); } } ``` 上面的代码创建了一个 TSimpleServer 实例,并将其绑定到本地的 9090 端口。它还将 MyServiceImpl 的实例添加到 MyService.Processor 中,以便服务器可以调用 MyServiceImpl 中实现的方法。 6. 编写客户端 你需要编写一个 Java 客户端,以便可以调用 MyServiceImpl 中实现的方法。以下是一个简单的示例: ``` public class MyClient { public static void main(String[] args) throws Exception { TTransport transport = new TSocket("localhost", 9090); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); MyService.Client client = new MyService.Client(protocol); String result = client.sayHello("World"); System.out.println(result); transport.close(); } } ``` 上面的代码创建了一个 TSocket 实例,并将其连接到本地的 9090 端口。它还创建了一个 MyService.Client 实例,并使用它来调用 MyServiceImpl 中实现的 sayHello 方法。 以上就是使用 Java 快速入门 Apache Thrift 的步骤。你可以通过阅读 Thrift 的官方文档来了解更多信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值